Example-Html form generator servlet package itso.servjsp.servletapi; import java.io.*; import javax.servlet.*; import javax.servlet.htt...
Example-Html form generator servlet
package itso.servjsp.servletapi;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HTMLFormGenerator extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
System.out.println("In the init() method of HTMLFormGenerator");
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
performTask(req, res, "POST",
"itso.servjsp.servletapi.HTMLFormHandler");
// "/itsoservjsp/servlet/itso.servjsp.servletapi.HTMLFormHandler");
}
public void performTask(HttpServletRequest req, HttpServletResponse res,
String method, String url) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><TITLE>HTMLFormGenerator</TITLE><BODY>");
out.println("<H2>Servlet API Example - HTMLCreatingServlet</H2><HR>");
out.println("<FORM METHOD=\"" + method + "\" ACTION=\"" + url + "\">");
out.println("<H2>Tell us something about yourself: </H2>");
out.println("<B>Enter your name: </B>");
out.println("<INPUT TYPE=TEXT NAME=firstname><BR>");
out.println("<B>Select your title: </B>");
out.println("<SELECT NAME=title>");
out.println("<OPTION VALUE=\"Web Developer\">Web Developer");
out.println("<OPTION VALUE=\"Web Architect\">Web Architect");
out.println("<OPTION VALUE=\"Other\">Other");
out.println("</SELECT><BR>");
out.println("<B>Which tools do you have experience with: </B><BR>");
out.println("<INPUT TYPE=checkbox NAME=\"tools\"
VALUE=\"WebSphere Application Server\">WebSphere Application Server<BR>");
out.println("<INPUT TYPE=checkbox NAME=\"tools\"
VALUE=\"WebSphere Studio\">WebSphere Studio<BR>");
out.println("<INPUT TYPE=checkbox NAME=\"tools\"
VALUE=\"VisualAge for Java\">VisualAge for Java<BR>");
out.println("<INPUT TYPE=checkbox NAME=\"tools\"
VALUE=\"IBM Http Web Server\">IBM Http Web Server<BR>");
out.println("<INPUT TYPE=checkbox NAME=\"tools\" VALUE=\"DB2 UDB\">DB2 UDB<BR>");
out.println("<INPUT TYPE=\"SUBMIT\" NAME=\"SENDPOST\" NAME=\"SENDPOST\">");
out.println("</FORM>");
out.println("</BODY><HTML>");
out.close();
System.out.println("In the doGet method");
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HTMLFormGenerator extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
System.out.println("In the init() method of HTMLFormGenerator");
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
performTask(req, res, "POST",
"itso.servjsp.servletapi.HTMLFormHandler");
// "/itsoservjsp/servlet/itso.servjsp.servletapi.HTMLFormHandler");
}
public void performTask(HttpServletRequest req, HttpServletResponse res,
String method, String url) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><TITLE>HTMLFormGenerator</TITLE><BODY>");
out.println("<H2>Servlet API Example - HTMLCreatingServlet</H2><HR>");
out.println("<FORM METHOD=\"" + method + "\" ACTION=\"" + url + "\">");
out.println("<H2>Tell us something about yourself: </H2>");
out.println("<B>Enter your name: </B>");
out.println("<INPUT TYPE=TEXT NAME=firstname><BR>");
out.println("<B>Select your title: </B>");
out.println("<SELECT NAME=title>");
out.println("<OPTION VALUE=\"Web Developer\">Web Developer");
out.println("<OPTION VALUE=\"Web Architect\">Web Architect");
out.println("<OPTION VALUE=\"Other\">Other");
out.println("</SELECT><BR>");
out.println("<B>Which tools do you have experience with: </B><BR>");
out.println("<INPUT TYPE=checkbox NAME=\"tools\"
VALUE=\"WebSphere Application Server\">WebSphere Application Server<BR>");
out.println("<INPUT TYPE=checkbox NAME=\"tools\"
VALUE=\"WebSphere Studio\">WebSphere Studio<BR>");
out.println("<INPUT TYPE=checkbox NAME=\"tools\"
VALUE=\"VisualAge for Java\">VisualAge for Java<BR>");
out.println("<INPUT TYPE=checkbox NAME=\"tools\"
VALUE=\"IBM Http Web Server\">IBM Http Web Server<BR>");
out.println("<INPUT TYPE=checkbox NAME=\"tools\" VALUE=\"DB2 UDB\">DB2 UDB<BR>");
out.println("<INPUT TYPE=\"SUBMIT\" NAME=\"SENDPOST\" NAME=\"SENDPOST\">");
out.println("</FORM>");
out.println("</BODY><HTML>");
out.close();
System.out.println("In the doGet method");
}
}
init method
This servlet implements the init method. The init method only prints a message to standard output and call the super-class constructor. As we mentioned before, the init method is called only once, when the servlet is loaded. This message, therefore, should only be printed to the Web server’s console or log once (wherever standard output is defined), regardless of how many times the servlet is actually invoked.
doGet method
We decided that this servlet is always called through a GET request, we have chosen to implement the doGet method, instead of the more generic service method. We developed a performTask method to which we pass a method posting type and a target URL.
Response object
The HTML page that this servlet generates is a bit more complex than the previous example. It actually builds an HTML form that can be used in the future to call other servlets. This is not the same as a servlet calling a servlet, which is a server-side process, Here, we are just using one servlet to generate the HTML back to the browser, so we can call our other example servlets, and we do not have to create separate HTML files for each servlet. Notice that this servlet has many out.println statements. This is just the HTML that is written back to the browser. Despite the size of this servlet, it is still only doing one simple thing, writing HTML output.