URL
rewriting.
Ø In
this URL Rewriting session tracking, the data of every individual client is
given back to the client browser in the form anchoring message (hyper link)
along with some parameters and values. So that the client browser will display
the anchoring message by hiding the parameters and values.
Ø When
the same client click the anchoring message then the browser will automatically
send those parameters and values to the application.
Ø It
will always work whether cookie is disabled or not (browser independent).
Ø Extra
form submission is not required on each pages.
Ø It
will work only with links.
Ø It
can send Only textual information.
The
following example demonstrate how to use the URL Rewriting session tracking in
servlet application.
In
this example, we are maintaining the state of the user using link. For this
purpose, we are appending the name of the user in the query string and getting
the value from the query string in another page.
index.html
=======
<form
action="servlet1">
Name:<input
type="text" name="sname"/><br/>
<input
type="submit" value="submit"/>
</form>
web.xml
======
<?xml
version="1.0" encoding="UTF-8"?>
<web-app
version="3.1" 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">
<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>
The
URL pattern of this class must be /s1, this will receive the request with one
parameter "sname" and Submit as get Request.
FirstServlet.java
===========
import
java.io.*;
import
javax.servlet.ServletException;
import
javax.servlet.http.*;
public
class FirstServlet extends HttpServlet {
public
void doGet(HttpServletRequest request, HttpServletResponse response) throws
IOException,ServletException{
response.setContentType("text/html");
PrintWriter out =
response.getWriter();
String n=request.getParameter("sname");
out.print("Welcome "+n);
out.println("<br /><br
/>");
out.print("<a
href='servlet2?uname="+n+"'>Please click here for your
details</a>");
out.close();
}
}
The
URL pttern of this class must be /s2, this will receive the request with one
parameter "uname" when we click on anchoring message
SecondServlet.java
=============
import
java.io.*;
import
javax.servlet.ServletException;
import
javax.servlet.http.*;
public
class SecondServlet extends HttpServlet {
@Override
public
void doGet(HttpServletRequest request, HttpServletResponse response) throws
IOException,ServletException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//getting value from the query
string
String
name=request.getParameter("uname");
out.print("Hello
"+name);
out.close();
}
}
No comments:
Post a Comment