×
DocsConsole

DocsConsole

Session Tracking With URL Rewriting

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

Session Tracking With URL Rewriting

By using URL Rewriting to request header we can maintain the user state i.e., Session. Generally, when client will not allow adding the cookies then URL Rewriting will be used for session tracking. Session will be appended to each request for associating the request to the session.

Create Maven project for SessionTrackingURLRewritingApp-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, SessionTrackingURLRewritingApp-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>SessionTrackingURLRewritingApp-Example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>SessionTrackingURLRewritingApp-Example</name>
	<description>SessionTrackingURLRewritingApp-Example</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>

Please create below given files with given code.

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>SessionTrackingURLReWritingApp-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>URLRewritingServlet</servlet-name>
		<servlet-class>com.docsconsole.servlet4.tutorials.URLRewritingServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>URLRewritingServlet</servlet-name>
		<url-pattern>/SendURLRewritingValue</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>home.jsp</welcome-file>
	</welcome-file-list>
</web-app>
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");

			// userName will be appended here
			out.print("<div style='padding-left: 50px; padding-top: 50px;'>");
			out.print("<span style='color: #00bcd4;'>Welcome:</span>" + userName);
			out.print("<a href='SendURLRewritingValue?userName=" + userName + "' style='padding-left: 10px; font-size: 20px;'>Send URLRewriting Value</a>");
			out.print("</div>");
			out.close();

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

}
URLRewritingServlet.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 URLRewritingServlet extends HttpServlet {

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

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

			// getting value from the query string
			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>SessionTrackingURLReWritingApp-Example</h2>
		<form action="homeServlet">
			<span style="color: #2c2828;">Name:</span>
			<input type="text" name="userName" style="margin-left: 20px;"/><br/> 
			<input type="submit" value="Send UserName" style="background-color: #8083ef;margin-top: 10px;">
		</form>
	</div>
</body>
</html>

The below given will show the how project architecture should be.

Session-Tracking-With-URL-Rewriting-architecture

You can find below given outputs when you execute the program.

Session-Tracking-With-URL-Rewriting-Result1
Session-Tracking-With-URL-Rewriting-Result2
Session-Tracking-With-URL-Rewriting-Result3

Download Source Code SessionTrackingURLReWritingApp-Example.zip

Leave A Reply