< Prev
Next >



Session management by Hidden Fields



In this article, we are going to understand how to maintain a session by setting hidden field in 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>");

}

}





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-






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 -


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0"
  metadata-complete="true">

  <display-name>Welcome tomcat</display-name>
  <description>
     Welcome tomcat
  </description>

<servlet>
 	<servlet-name>FirstServlet</servlet-name>
	<servlet-class>MyServlet1</servlet-class>
</servlet>

<servlet>
 	<servlet-name>SecondServlet</servlet-name>
	<servlet-class>MyServlet2</servlet-class>
</servlet>



<servlet-mapping>
	<servlet-name>FirstServlet</servlet-name>
	<url-pattern>/Serv1</url-pattern>
</servlet-mapping>


<servlet-mapping>
	<servlet-name>SecondServlet</servlet-name>
	<url-pattern>/Serv2</url-pattern>
</servlet-mapping>


</web-app>


In deployment descriptor file, <servlet> has two child tags <servlet-name> and <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 -




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 -




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.






Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< URL Rewriting
Cookies in Servlet >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2