• September 25, 2014
  • Servlet
  • Comments Off on URL rewriting 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 javawithease
*/
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="
    		   +userName+"&password="+password+"'>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 javawithease
*/
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 + "<br/><br/>");
    	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:

servlet example 12 login
 
Enter username: jai and password: 1234
servlet example 12 value
 
Click on login button.
servlet example 12 link
 
Click on the link.
servlet example 12 show value
 
Download this example.
 
Next Topic: HttpSession in servlet with example.
Previous Topic: Hidden field in servlet with example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.

 


Comments are closed.