17. URL rewriting in servlet


Hidden Field in Servlet: Previous                                                      Next: Http Session in Servlet

URL rewriting in servlet

URL rewriting:

URL rewriting is a way of appending data at the end of 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 parameter value from url in 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. 1. As data is appended in the URL it is easy to debug.
  2. 2. It is browser independent.

Disadvantages of URL rewriting:

  1. 1. Not secure because data is appended in the URL.
  2. 2. Can’t append 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 java tutorial point
*/
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. 

");
      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.
"); 
      out.println("Click on the below link to see " +
          "the values of Username and Password.
");
      out.println("Click here");
     }else{
      out.print("Wrong username or password. 

");
      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 java tutorial point
*/
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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
 <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>  
</body>
</html>
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.javawithease.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.javawithease.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>

Output:


Enter username: jai and password: 1234


 Click on login button.

Click on the link.



Hidden Field in Servlet: Previous                                                      Next: Http Session in Servlet