×
DocsConsole

DocsConsole

Session Tracking With Hidden Fields

By | | Updated : 2019-11-27 | Viewed : 220 times

Session-Tracking-With-Hidden-Fields

We have one more option called for Hidden fields for tracking the session. In this type Session Management, hidden fields will be in the browser. While sending the new request we can send these field’s value.

Hidden Fields: which contains value can be used for sending with requests and all. So that it can be main the connection between the request.

Create Maven project for SessionTrackingHiddenFieldsApp-Example
Click on File tab 
--> New 
--> Click on Maven Project 
--> Please check on Create Simple Project (Skip architype selection) 
--> Click on Next --> Enter the values com.docsconsole.tutorials.servlet4 as Group Id, SessionTrackingHiddenFieldsApp-Example as Artifact Id 
--> Click on Finish
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.docsconsole.servlet4.tutorials</groupId>
  <artifactId>SessionTrackingHiddenFieldsApp-Example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>SessionTrackingHiddenFieldsApp-Example</name>
  <description>SessionTrackingHiddenFields</description>
  
  
  <properties>
		<maven.war.plugin.version>3.2.2</maven.war.plugin.version>
		<servlets.version>4.0.0</servlets.version>
		<jsp.version>2.3.1</jsp.version>
		<jstl.version>1.2.2</jstl.version>
		<tld.version>1.1.2</tld.version>
		<log4j.version>1.2.17</log4j.version>
		<maven.compiler.source>11</maven.compiler.source>
		<maven.compiler.target>11</maven.compiler.target>
	</properties>

	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${servlets.version}</version>
			<scope>provided</scope>
		</dependency>

		<!-- JSP Dependency -->
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>javax.servlet.jsp-api</artifactId>
			<version>${jsp.version}</version>
			<scope>provided</scope>
		</dependency>

		<!-- JSTL Dependency -->
		<dependency>
			<groupId>javax.servlet.jsp.jstl</groupId>
			<artifactId>javax.servlet.jsp.jstl-api</artifactId>
			<version>${jstl.version}</version>
		</dependency>
		<!-- TagLibs -->
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>${tld.version}</version>
		</dependency>
		<!-- Log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j.version}</version>
		</dependency>
	</dependencies>

	<build>
		<pluginManagement>
			<plugins>

				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-war-plugin</artifactId>
					<version>${maven.war.plugin.version}</version>
					<configuration>
						<warSourceDirectory>src/main/webapp</warSourceDirectory>
						<warName>${project.artifactId}</warName>
						<failOnMissingWebXml>false</failOnMissingWebXml>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>
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_3_1.xsd"
	version="3.1">

	<display-name>SessionTrackingHiddenFieldsApp-Example</display-name>

	<servlet>
		<servlet-name>homeServlet</servlet-name>
		<servlet-class>com.docsconsole.servlet4.tutorials.HomeServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>homeServlet</servlet-name>
		<url-pattern>/homeServlet</url-pattern>
	</servlet-mapping>

	<servlet>
		<servlet-name>cookiesServlet</servlet-name>
		<servlet-class>com.docsconsole.servlet4.tutorials.CookiesServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>cookiesServlet</servlet-name>
		<url-pattern>/sendCookiesServlet</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>home.jsp</welcome-file>
	</welcome-file-list>
</web-app>

Please create required files as shown below.

HomeServlet.java
package com.docsconsole.servlet4.tutorials;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.*;

public class HomeServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		try {

			response.setContentType("text/html");
			PrintWriter out = response.getWriter();

			String userName = request.getParameter("userName");

			// wiil be created hidden field in the html page

			out.print("<div style='padding-left: 50px; padding-top: 50px;'>");
			out.print("<span style='color: #00bcd4;'>Welcome:</span>" + userName);
			out.print("<form action='sendCookiesServlet'>");
			out.print("<input type='hidden' name='userName' value='" + userName
					+ "'><input type='submit' value='Send Hidden Field' style='margin-top: 10px;background: #8083ef;'>");
			out.print("</form></div>");
			out.close();

		} catch (Exception e) {
			System.out.println(e);
		}
	}

}
CookiesServlet.java
package com.docsconsole.servlet4.tutorials;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.*;

public class CookiesServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		try {
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();

			// Getting the value from the hidden field
			String userName = request.getParameter("userName");
			out.print("<div style='padding-left: 50px; padding-top: 50px;'><span style='color: #00bcd4;'>Hello:</span>"
					+ userName + "</div>");

			out.close();
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}
home.jsp
<!DOCTYPE html>
<html>
<head></head>
<body>
	<div style="padding-left: 50px; padding-top: 50px;">
		<h2>Session Tracking Hidden Fields Example</h2>
		<form action="homeServlet">  
			<span style="color: #2c2828;">Name:</span><input type="text" name="userName"/><br/>  
			<input type="submit" value="Send User Name" style="margin-top: 11px;background-color: #8083ef;">  
		</form>
	</div>
</body>
</html>
Session-Tracking-With-Hidden-Fields-architecture

After deployment war, you can see below given results in the browser.

Session-Tracking-With-Hidden-Fields-Result1
Session-Tracking-With-Hidden-Fields-Result2
Session-Tracking-With-Hidden-Fields-Result3

Download Source Code SessionTrackingHiddenFieldsApp-Example.zip

Leave A Reply