Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Servlet that displays a form when the doGet method is invoked. The form

ID: 3542282 • Letter: C

Question

Create a Servlet that displays a form when the doGet method is invoked. The form will contain a post action that directs the form post back to the same servlet, which in the doPost method will append the form data to a random access file. After the form data has been appended to the file, respond back with the complete contents of the data file. The form must contain a minimum of three input fields. The grade for this assignment will be based both on the functionality of the servlet and the appearance of the form post results. Name your servlet <yourName>FormPost and name the random access data file c: emp<yourname>week4.dat. You must locate the file in the c: emp directory to receive credit. This is so I can run and grade it on my computer without modifying your source code. Attach your servlet source code file and random access data file to this assignment in one zipped file.


**Please dont post an answer that is obviously incorrect, and no I wont email you to pay for 50 dollars.**

Explanation / Answer

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

  

/** Simple Hello World Servlet

*/

public class HelloServlet extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

PrintWriter out = response.getWriter( );

response.setContentType("text/html");

out.println("<H1>Hello from a Servlet</h2>");

out.println("<P>This servlet ran at ");

out.println(new Date().toString( ));

out.println("<P>Courtesy of HelloServlet.java 1.2 ");

}

}

The program will give output resembling Figure 18-1.


Figure 18-1. Hello from a servlet

  


You can do much more with servlets. Suppose you wanted to print a dictionary--a list of terms and their meanings--from within a servlet. The code would be pretty much as it was in Figure 18-1, except that you'd need a doGet( ) method instead of a doPost( ) method. Example 18-2 is the code for TermsServlet.


Example 18-2: TermsServlet.java


/** A Servlet to list the dictionary terms.

*/

public class TermsServlet extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

PrintWriter out = resp.getWriter( );

out.println("<HTML>");

out.println("<TITLE>Ian Darwin's Computer Terms and Acronyms</TITLE>");

out.println("<BODY>");

out.println("<H1>Ian Darwin's Computer Terms and Acronyms</h2>");

out.println("<TABLE BORDER=2>");

out.println("<TR><TH>Term<TH>Meaning</TR>");

  

// This part of the Servlet generates a list of lines like

// <TR> <TD>JSP <TD>Java Server Pages, a neat tool for ...

TermsAccessor tax = new TermsAccessor("terms.txt");

Iterator e = tax.iterator( );

while (e.hasNext( )) {

Term t = (Term)e.next( );

out.print("<TR><TD>");

out.print(t.term);

out.print("<TD>");

out.print(t.definition);

out.println("</TR>");

}

out.println("</TABLE>");

out.println("<HR></HR>");

out.println("<A href="servlet/TermsServletPDF</p> <p>">Printer-friendly (Acrobat PDF) version</A>");

out.println("<HR></HR>");

out.println("<A href="mailto:compquest@darwinsys.com/subject=Question</p> <p>">Ask about another term</A>");

out.println("<HR></HR>");

out.println("<A href="index.html">Back to HS</A> <A href="../</p> <p>">Back to DarwinSys</A>");

out.println("<HR></HR>");

out.println("<H6>Produced by $Id: TermsServlet.java,v 1.1 2000/04/06

ian Exp $");

out.print(" using ");

out.print(tax.ident);

out.println("</H6>");

}

}