The Java Servlet Code

The Java Servlet which takes the input from the webpage and sends it to the shell program "display" is called "DisplayLCD.class"

The Servlet is passed the name of the user and the two lines of text input. It writes the two lines of input to the file called "messageinput" and then executes the "display" shell command, which will read the input from the file. A history of the last 10 messages is stored in a file called "messagehistory.txt"

The source code for DisplayLCD.class is here: DisplayLCD.java

// DisplayLCD.java
// Author: Dan Burke
// copy "servlet-api.jar" from /common/lib/ in your jakarta installation
// to /webapps/ROOT/WEB-INF/classes/
// Place this source file "DisplayLCD.java: in your 
// /webapps/ROOT/WEB-INF/classes/ in your jakarta installation
// compile with javac -classpath "servlet-api.jar" DisplayLCD.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DisplayLCD extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
    
		String command = "display";

		Runtime runtime = Runtime.getRuntime();
		Process process = null;

		// Get the text input from the web page.
		String line1 = request.getParameter("line1");
		String line2 = request.getParameter("line2");
		String name = request.getParameter("yourname");
		
		String history[][] = new String[2][10];
				
		int x = 0;

 		if(line1 != null){
 
		try {

			// Open an output stream to a file to write the
			// text input from the webpage to a file
        		BufferedWriter outwriter = new BufferedWriter(new FileWriter("displayinput"));
			
			x = line1.length();
			
			// Pad the first line with space characters if
			// it's length is less than 16 characters.
			while(x < 16){
				line1 = line1 + " ";
				x = line1.length();
			}
			
			// Write the first line to the file.
			outwriter.write(line1);
		
			x = line2.length();
			if (x != 0){

				// Pad the second line with space
				// characters if it's length is less
				// than 16 characters.
				while (x < 16){
					line2 = line2 + " ";
					x = line2.length();
				}

				// Write the second line to the output
				// file.
				outwriter.write(line2);
        		}

			// Close the output stream to the file.
			outwriter.close();
			

			// Read the history file
			BufferedReader historyreader = new BufferedReader(new FileReader("messagehistory.txt"));
			for (int i = 0; i < 10; i++){
				history[0][i] = historyreader.readLine();
				history[1][i] = historyreader.readLine();
			}
			historyreader.close();
			BufferedWriter historywriter = new BufferedWriter(new FileWriter("messagehistory.txt"));
			
			x = name.length();

			// Write the name of the user to the history
			// file.
			if (x != 0){
				historywriter.write(name + "\n");
			} else {
				historywriter.write("Anonymous\n");
			}
			
			// Write line 1 to the history file
			if (line1.length() != 0){
				historywriter.write(line1);
			}
			else {
				historywriter.write("                ");
			}
			
			// Write line 2 to the history file
			if (line2.length() != 0){
				historywriter.write(line2);
			} else {
				historywriter.write("                ");
			}
			
			historywriter.write("\n");
			
			// Write the previous 9 history entries
			for (int i = 0; i < 9; i++){
				historywriter.write(history[0][i] + "\n");
				historywriter.write(history[1][i] + "\n");
			}
			historywriter.close();

    		} catch (IOException e) {
			System.out.println("Error");
    		}

    		} else {
      			out.println("No text entered.");
    		}

    		

		try{
			// Execute the shell program to write to the LCD
			// Display
			process = runtime.exec(command);

			// Busy work to wait for the text to be written.
			for (int y = 0; y < 60000; y++){
				System.out.println("Busy");
			}

			// Display the results web page.
			BufferedReader in = new BufferedReader(new FileReader("displayresult.htm"));
			String filetext = in.readLine();
			while (filetext != null){
				out.print(filetext);
				filetext = in.readLine();
			}
			in.close();
			out.close();
		} catch (Exception e){
			out.println("Couldn't exec utility");
		}
	}
}

Back to the index