HTML form generator servlet

Example-Html form generator servlet package itso.servjsp.servletapi; import*; import javax.servlet.*; import javax.servlet.htt...

Example-Html form generator servlet
package itso.servjsp.servletapi;
import javax.servlet.*;
import javax.servlet.http.*;
public class HTMLFormGenerator extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
System.out.println("In the init() method of HTMLFormGenerator");
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
performTask(req, res, "POST",
// "/itsoservjsp/servlet/itso.servjsp.servletapi.HTMLFormHandler");
public void performTask(HttpServletRequest req, HttpServletResponse res,
String method, String url) throws ServletException, IOException {
PrintWriter out = res.getWriter();
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("<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>");
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.

Best Online Tutorials | Source codes | Programming Languages: HTML form generator servlet
HTML form generator servlet
Best Online Tutorials | Source codes | Programming Languages
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content