URL rewriting in servlet

URL rewriting:

URL rewriting is a way of appending data at the end of a URL. Data is appended in name-value pair form. Multiple parameters can be appended in one URL with name-value pairs.

Syntax:

URL?paramName1=paramValue1& paramName2=paramValue2

How to get the parameter value from url in the servlet?

HttpServletRequest interface’s getParameter() method is used to get parameter value from url in servlet.

Syntax:

String value = request.getParameter(“fieldName”);

Advantages of URL rewriting:

  1. As data is appended in the URL it is easy to debug.
  2. It is browser-independent.

Disadvantages of URL rewriting:

  1. Not secure because data is appended to the URL.
  2. Can’t append a large no. of parameters because URL length is limited.

Session management example using URL rewriting:

SetUrlParameterServlet.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* This class is used to set the parameters in the url.
* @author W3schools360
*/
public class SetUrlParameterServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    //no-argument constructor.
    public SetUrlParameterServlet() {
       
    }

    protected void doPost(HttpServletRequest request, 
    HttpServletResponse response)
                throws ServletException, IOException {
    response.setContentType("text/html"); 
        PrintWriter out = response.getWriter();
        
        //get parameters from request object.
        String userName = request.getParameter("userName").trim();
        String password = request.getParameter("password").trim();
        
        //check for null and empty values.
        if(userName == null || userName.equals("") || 
                password == null || password.equals("")){
            out.print("Please enter both username and " +
                    "password. <br><br>");
            RequestDispatcher requestDispatcher = 
                request.getRequestDispatcher("/login.html");
            requestDispatcher.include(request, response);
        }//Check for valid username and password.
        else if(userName.equals("jai") && password.equals("1234")){
            out.println("Logged in successfully.<br>"); 
            out.println("Click on the below link to see " +
                   "the values of Username and Password.<br>");
            out.println("<a href="GetUrlParameterServlet?userName=&quot;
               +userName+&quot;&password=&quot;+password+&quot;">Click here</a>");
        }else{
            out.print("Wrong username or password. <br><br>");
            RequestDispatcher requestDispatcher = 
                request.getRequestDispatcher("/login.html");
            requestDispatcher.include(request, response);
        }
    }
}

 

GetUrlParameterServlet.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* This class is used to get the parameter values from url.
* @author W3schools360
*/
public class GetUrlParameterServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    //no-argument constructor.
    public GetUrlParameterServlet() {
        
    }

    protected void doGet(HttpServletRequest request, 
	     HttpServletResponse response)
	              throws ServletException, IOException {
	response.setContentType("text/html"); 
    	PrintWriter out = response.getWriter();
    	
    	//get parameters from request object.
    	String userName = request.getParameter("userName").trim();
    	String password = request.getParameter("password").trim();
    	
    	out.println("Username: " + userName + "");
    	out.println("Password: " + password);
    	
    	out.close();
    }
}

login.html

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>


    <form action="SetUrlParameterServlet" method="post">
        Username:<input type="text" name="userName">
        <br><br>
        Password:<input type="password" name="password">
        <br><br> 
        <input type="submit" value="login"> 
    </form>

 

web.xml

<!--?xml version="1.0" encoding="UTF-8"?-->
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
  <servlet>
    <servlet-name>SetUrlParameterServlet</servlet-name>
    <servlet-class>
        com.w3schools.business.SetUrlParameterServlet
    </servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>SetUrlParameterServlet</servlet-name>
    <url-pattern>/SetUrlParameterServlet</url-pattern>
  </servlet-mapping>
  
  <servlet>
    <servlet-name>GetUrlParameterServlet</servlet-name>
    <servlet-class>
        com.w3schools.business.GetUrlParameterServlet
    </servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>GetUrlParameterServlet</servlet-name>
    <url-pattern>/GetUrlParameterServlet</url-pattern>
  </servlet-mapping>
  
   <welcome-file-list>
    <welcome-file>login.html</welcome-file>
  </welcome-file-list>
  
</web-app>