URL Rewriting

URL rewriting is one of the session management technique that manages user session by modifying a URL. Here we append a token or identifier to the URL of the next Servlet or the next resource. 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. We can send parameter name/value pairs as:

URL name1=value1 & name2=value2

A name and a value is separated using an equal = sign, a parameter name/value pair is separated from another parameter using the ampersand(&). When the user clicks the hyperlink, the parameter name/value pairs will be passed to the server.

When the User clicks on the URL having parameters, the request goes to the Web Container with extra bit of information at the end of URL. The Web Container will fetch the extra part of the requested URL and use it for session management. The getParameter() method is used to get the parameter value at the server side. Let's see a demo program for better clarity:

index.html
<form action="MyServlet1" method="post">
Name:<input type="text" name="uname" />
<input type="submit" value="ok">
</form>
MyServlet1.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet1 extends HttpServlet
{
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException
    {
        response.setContentType("text/html"); 
        PrintWriter pw = response.getWriter();
        String n=request.getParameter("uname");
        pw.print("Welcome "+n);
  
        //appending the username in the query string
        pw.print("go");
                  
        pw.close();
   
    }
}
MyServlet2.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet2 extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException
    {
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        //getting value from the query string 

        String n=request.getParameter("uname");
        pw.print("Hello "+n); 
        pw.close();
  
    }
}
web.xml
<web-app>
    <servlet>
        <servlet-name>abc</servlet-name> 
        <servlet-class>MyServlet1</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>abc</servlet-name>
        <url-pattern>/MyServlet1</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>xyz</servlet-name>
        <ervlet-class>MyServlet2</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>xyz</servlet-name>
        <url-pattern>/MyServlet2</url-pattern>
    </servlet-mapping>

</web-app>

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext