Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

URL Rewriting


In URL rewriting, we append a token or identifier to the URL of the next Servlet or the next resource. We can send parameter name/value pairs using the following format:

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

A name and a value is separated using an equal = sign, a parameter name/value pair is separated from another parameter using the ampersand(&). When the user clicks the hyperlink, the parameter name/value pairs will be passed to the server. From a Servlet, we can use getParameter() method to obtain a parameter value.

url

Advantage of URL Rewriting

  1. It will always work whether cookie is disabled or not (browser independent).
  2. Extra form submission is not required on each pages.

Disadvantage of URL Rewriting

  1. It will work only with links.
  2. It can send Only textual information.

Example of using URL Rewriting

In this example, we are maintaning the state of the user using link. For this purpose, we are appending the name of the user in the query string and getting the value from the query string in another page.

index.html

  1. <form action=“servlet1”>
  2. Name:<input type=“text” name=“userName”/><br/>
  3. <input type=“submit” value=“go”/>
  4. </form>

FirstServlet.java

  1. import java.io.*;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. public class FirstServlet extends HttpServlet {
  5. public void doGet(HttpServletRequest request, HttpServletResponse response){
  6.         try{
  7.         response.setContentType(“text/html”);
  8.         PrintWriter out = response.getWriter();
  9.         String n=request.getParameter(“userName”);
  10.         out.print(“Welcome “+n);
  11.         //appending the username in the query string
  12.         out.print(“<a href=’servlet2?uname=”+n+“‘>visit</a>”);
  13.         out.close();
  14.                 }catch(Exception e){System.out.println(e);}
  15.     }
  16. }

SecondServlet.java

  1. import java.io.*;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. public class SecondServlet extends HttpServlet {
  5. public void doGet(HttpServletRequest request, HttpServletResponse response)
  6.         try{
  7.         response.setContentType(“text/html”);
  8.         PrintWriter out = response.getWriter();
  9.         //getting value from the query string
  10.         String n=request.getParameter(“uname”);
  11.         out.print(“Hello “+n);
  12.         out.close();
  13.                 }catch(Exception e){System.out.println(e);}
  14.     }
  15. }

web.xml

  1. <web-app>
  2. <servlet>
  3. <servlet-name>s1</servlet-name>
  4. <servlet-class>FirstServlet</servlet-class>
  5. </servlet>
  6. <servlet-mapping>
  7. <servlet-name>s1</servlet-name>
  8. <url-pattern>/servlet1</url-pattern>
  9. </servlet-mapping>
  10. <servlet>
  11. <servlet-name>s2</servlet-name>
  12. <servlet-class>SecondServlet</servlet-class>
  13. </servlet>
  14. <servlet-mapping>
  15. <servlet-name>s2</servlet-name>
  16. <url-pattern>/servlet2</url-pattern>
  17. </servlet-mapping>
  18. </web-app>

Comments

comments