Master java skills

URL Rewriting

In URL rewriting technique, a token or identifier is appended to the URL of the next http request. A parameter name and value pairs can be sent in the below format. Multiple name/value pairs can be separated using &

url?name1=value1&name2=value2&??

A name and its value is separated using an equal = sign, whereas a parameter name/value pair is separated from another one using the ampersand(&). When the url is hit, the parameter name/value pairs are passed to the server. In a Servlet, we can use request.getParameter() method to get a parameter value.

URL Rewriting example

In the below example, we are going to append the name of the user in the query string and getting the value from the query string in another page.

login.html

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

ServletOne.java

import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;  
  
  
public class ServletOne extends HttpServlet {  
  
public void doGet(HttpServletRequest request, HttpServletResponse response){  
        try{  
            response.setContentType("text/html");  
            PrintWriter out = response.getWriter();  
          
            String userName = request.getParameter("userName");  
            out.print("Welcome " + userName + "<br>");  
  
            //appending the username in the query string
            //username can be get in the next servlet by 
            //using its key uname  
            out.print("<a href='welcome?uname=" + userName +"'>Click Here</a>");  
                  
            out.close();  
  
        }catch(Exception e){System.out.println(e);}  
    }  
}

ServletTwo.java

import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletTwo 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("uname");  
            out.print("Hello " + userName);  
  
            out.close();  
  
        }
        catch(Exception e){
            System.out.println(e);
        }  
    }
}

web.xml

<web-app>  
  
    <servlet>  
        <servlet-class>ServletOne</servlet-class>  
        <servlet-name>servletOne</servlet-name>  
    </servlet>  
  
    <servlet-mapping>  
        <servlet-name>servletOne</servlet-name>  
        <url-pattern>/login</url-pattern>  
    </servlet-mapping>  
  
    <servlet>  
        <servlet-class>ServletTwo</servlet-class> 
        <servlet-name>servletTwo</servlet-name>  
    </servlet>  
  
    <servlet-mapping>  
        <servlet-name>servletTwo</servlet-name>  
        <url-pattern>/welcome</url-pattern>  
    </servlet-mapping>  
  
</web-app>

Deploy the application, run the server and test.

url – http://localhost:8080/my-first-servlet-app/login.html

Pros and cons of url rewriting

  1. As opposed to another session technique Cookies, it is not dependent on the fact whether the user has enabled cookies or not.
  2. Form submission is not required on each page
  3. But since sensitive information is sent via url, it is not a safe method to send user data
  4. It can send only textual information