Using URL Rewriting for Session Management

If the client has disabled cookies in the program then session management utilizing cookie wont work. All things considered URL Rewriting can be utilized as a reinforcement. URL modifying will dependably work.

In URL changing, a token (parameter) is included toward the end of the URL. The token comprise of name/worth pair seperated by an equal(=) sign.

Example

At the point when the User taps on the URL having parameters, the solicitation goes to the Web Container with additional piece of data toward the end of URL. The Web Container will bring the additional part of the asked for URL and use it for session administration. The getParameter() strategy is utilized to get the parameter esteem at the server side.

Advantage of URL Rewriting

  • It will dependably work whether treat is debilitated or not (program free).
  • Extra structure accommodation is not required on every pages.

Disadvantage of URL Rewriting

  • It will work just with connections.
  • It can send Only printed data.

image

index.html

<form action="servlet1">
Name:<input type="text" name="userName"/><br/>
<input type="submit" value="go"/>
</form>

FirstServlet.java

import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 


public class FirstServlet extends HttpServlet { 

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

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

String n=request.getParameter("userName"); 
out.print("Welcome "+n); 

//appending the username in the query string 
out.print("Click here"); 

out.close(); 

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

} 

SecondServlet.java

import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 

public class SecondServlet 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 n=request.getParameter("uname"); 
out.print("Hello "+n); 

out.close(); 

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


} 

Web.xml

		<web-app>

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

<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>

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

<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>

</web-app>