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:
- As data is appended in the URL it is easy to debug.
- It is browser independent.
Disadvantages of URL rewriting:
- Not secure because data is appended in the URL.
- 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 w3spoint */ 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 w3spoint */ 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.w3spoint.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.w3spoint.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.
Download this example.
Next Topic: HttpSession in servlet with example.
Previous Topic: Hidden field in servlet with example.