Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Java Servlet :Example of Hidden form Field
Previous Home Next

This is a browser independent approach of maintaining state between requests in a web applicaion.In this approach information to be persistent is stored in invisible text fields which are added to the response page.When a request is submitted from the response page value of invisible text fields is submitted as request parameters.

index.html

<html>
	<head>
     <title>My Second Web Application</title>
	</head>
	<body>
	 <form method="get" action="application">
	 <b>Name<input type="text" name="txtName">
	 <br><input type="submit" value="Login"></form>
	</body>
</html>

web.xml

<web-app>
	<servlet>
	 <servlet-name>s1</servlet-name>
	 <servlet-class>WelcomeServlet</servlet-class>
	</servlet>
	 <servlet-mapping>
	 <servlet-name>s1</servlet-name>
	<url-pattern>application</url-pattern>
	</servlet-mapping>
	<servlet>
	 <servlet-name>s2</servlet-name>
	 <servlet-class>TourServlet</servlet-class>
	</servlet>
	<servlet-mapping>
	 <servlet-name>s2</servlet-name>
	 <url-pattern>toursevlet</url-pattern>
	</servlet-mapping>
</web-app>

TourServlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class TourServlet extends HttpServlet
{
 public void doGet(HttpServletRequest req,
   HttpServletResponse res)throws ServletException,IOException
{
 String name=req.getParameter("userName");
 res.setContentType("text/html");
 PrintWriter out=res.getWriter();
 out.println("Sorry,"+name);
 out.println("<br> Site is down for routine maintainance,pls visit again later...");
 out.close();
}
}

WelcomeServlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class WelcomeServlet extends HttpServlet
{
 public void doGet(HttpServletRequest req,
   HttpServletResponse res)throws ServletException,IOException
{
 String name=req.getParameter("txtName");
 res.setContentType("text/html");
 PrintWriter out=res.getWriter();
 out.println("<br><form action=toursevlet>");
 out.println("<br><input type=hidden name=userName value=\""+name+"\">");
 out.println("<br><input type=submit  value=\"Take a tour\">");
 out.println("<br></form>");
 out.close();
}
}
Previous Home Next