URL Rewriting in Servlet

URL Rewriting in Servlet


Posted in : Servlet Posted on : November 30, 2010 at 6:20 PM Comments : [ 0 ]

This section contains the details about URL Rewriting in Servlet .

URL Rewriting in Servlet

URL rewriting is a session management technique that manages user session by modifying a URL. Usually, this technique is used when information that is to be transferred is not very critical because the URL can be intercepted easily during transfer. Given below the example of URL rewriting :

UrlRewriting.html

<HTML>
<TITLE>ONLINE SHOPPING PORTAL</TITLE>
<BODY>
<FORM ACTION =
"http://localhost:8080/ankit/servlet/RewriteServletURL" METHOD = POST align=CENTER>
Username: <INPUT TYPE = TEXT NAME = "user" align=CENTER><BR>
<INPUT TYPE = SUBMIT VALUE = "Login" align=CENTER>
</FORM>
</BODY>
</HTML>

RewriteServletURL.java

/* Import the required packages. */
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class RewriteServletURL extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse
res) throws ServletException, IOException
{
/* Retrieve the parameters bound to user, password and
login from the request object. */
String username = req.getParameter("user");
PrintWriter pw = res.getWriter();
/* Verify the login status */
res.setContentType("text/html");
pw.println("Hello! <a href=\"http://localhost:8080/ankit/servlet/SecondServlet?uname="
+ username + "\"> click here </a>to proceed");
}
}

SecondServlet.java

/* Import the required packages. */
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/* Define the SecondServlet that extends HttpServlet. */
public class SecondServlet extends HttpServlet
{
/* Override the doGet() method of HttpServlet. */
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse
res) throws ServletException, IOException
{
/* Retrieve the parameters bound to user name through
hidden form field from the request object. */
String uname = req.getParameter("uname");
PrintWriter pw = res.getWriter();
pw.println("Hello! "+uname);
}
}

When you run this application, it will display the following the message :

Download Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics