In this article, we are going to understand how to maintain a session by setting a hidden field within a form.
Hidden field is a field in HTML form which is used to store a user information or a session data, to maintain its session and differentiate it from other users.
This hidden field is not displayed to the user and that's why it is called hidden. It can only be seen through view-source option of a webpage.
Through hidden field we add a session data such as to the request going to be made to the next Servlet.
The next Servlet reads the hidden field passed to it associated with the request and
send back an appropriate response to the client.
Syntax of Hidden field
<input type = "hidden" name = "user" value = "max" >
We have created a hidden form field, with a variable name user and its value max and have associated it with form data within the request
passed to the calling Servlet.
This hidden field is used to identify a user, maintain a session and send back an appropriate response.
Creating a webpage which calls the Servlet
We are creating a webpage which asks the user to enter his/her name and press the submit button,
which when clicked calls the Servlet with the <url-pattern> Serv1, specified in the deployment descriptor file(web.xml),
passing this Servlet the parameter named username using post method.
Webpage1.jsp
<html>
<head>
<title> URL Rewriting Demo </title>
</head>
<body>
Please enter your details :
<form action = "Serv1" method = "post" >
Name : <input type = "text" name = "username"/>
<input type = "submit" name = "submit"/>
</form>
</body>
</html>
Using Hidden Fields with Servlet
We are creating a Servlet extending the HttpServlet abstract class. HttpServlet class extends GenericServlet abstract class.
This class named MyServlet1.java, implements doPost() method of HttpServlet class to handle post request made by the client after entering the name on Webpage1.jsp.
This Servlet reads the parameter named username passed to it and sets the hidden field named UName within a
form and pass it to the next Servlet class, MyServlet2.java.
MyServlet1.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet1 extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("username");
out.println("<b>Welcome "+ name + ","+ "</b>" );
out.println("<br/> <br/> <br/>");
out.println("Do you want to know what's the smallest planet in our universe? ");
out.println("<form action = 'Serv2' method = 'post' >");
out.println("<input type = 'hidden' name = 'Uname' value='"+name+"'>");
out.println("<input type = 'submit' value = 'yes' />");
out.println("</form>");
}
}
Another Servlet, called by the first servlet to give an answer to a question asked from the user by the first servlet.
MyServlet2.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet2 extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("Uname");
out.println("<b> Welcome "+ name + ","+ "</b>" );
out.println("<br/> <br/> <br/>");
out.println(" <b> Answer - Mercury </b>");
}
}
Advertisement
Directory Structure of Servlet files
The diagram above depicts how to arrange the Servlet files in a specific directory structure, as per Java Servlet Specification-
Project Folder - We have created a project folder named HiddenFields within
the webapps folder of Tomcat installation folder. This folder contains the Servlet class(.java) file and WEB-INF folder.
WEB-INF - Within your project folder, you create another folder named WEB-INF and this folder should contain the deployment descriptor file(web.xml) of your Java Servlet program.
classes - Within the WEB-INF folder you should create a folder named classes. This folder should contain the compiled(.class) form of your Java Servlet class.
Creating the Deployment Descriptor file
As per the Java Servlet specifications, every web application based on Servlet must have a Deployment Descriptor file(an XML file) named web.xml.
So, let's create one -
In deployment descriptor file, <servlet> has two child tags <servlet-name> and <servlet-class> :
<servlet-name> tag is used to specify a unique name for our Servlet class.
<servlet-class> tag is used to specify the full qualified name of the Servlet class .
Note
The child tag <servlet-name> of <servlet> tag is matched with the <servlet-name> child tag of
<servlet-mapping>. The
<url-pattern> child tag is used to specify the URL to access Servlet, Hence -
We have named this URL Welcome. to access the first servlet, MyServlet1.java
We have named this URL Rides to access the second servlet, MyServlet2.java.
Setting the classpath
Much of the support for developing the web applications based on the Java Servlet technology does not come with the core Java.
Hence, in order to compile the Servlet programs, we have to set the classpath to a jar file named
servlet-api.jar.
This jar file provides all the classes that are required for the Servlet programming and it comes within the lib Folder of
Tomcat installation folder.
For example, in our case we have installed Tomcat Web Server within the C: Drive, hence the path to our lib folder containing the servlet-api.jar is - C:\apache-tomcat-9.0.2\lib
There are two ways to set the classpath -
You could set the classpath by entering this command at the Command Prompt, but this only temporarily sets the classpath to the path of servlet-api.jar file. If you restart your system, you will have to set the classpath again.
set classpath=C:\apache-tomcat-9.0.2\lib\servlet-api.jar
In order to set the classpath permanently, you need to set your classpath system variables. A simple tutorial on how to permanently set the classpath system variables, you may read
How to permanently set the classpath system variable
Compiling the Servlet class
After setting the classpath, you need to compile the both of the Servlet class by entering the command at the folder where you've stored the Servlet class file.
javac -d WEB-INF/classes MyServlet1.java
javac -d WEB-INF/classes MyServlet2.java
Executing the Servlet
First we execute the webpage containing the form, asking the user to enter the username and press the submit button.
On pressing the submit button, the username parameter is passed to the Servlet with a <url-pattern> Serv1 specified in the web.xml.
This call to this Servlet is made using post method, hence the parameter is not displayed in URL of calling Servlet.
Using the value of this parameter, we set a hidden field with the name UName within a form and ask a question with it.
When the user clicks the yes button, a Servlet with the <url-pattern> Serv2(as specified in web.xml)
is called and the hidden field like UName is also passed
to this Servlet with the request. Using this hidden field Uname parameter, a personalised message is displayed to the user with the
answer to the question.