URL Rewriting in Servlet
Advertisements
URL Rewriting in Servlet
URL Rewriting track the conversion in server based on unique session ID value.
When use URL Rewriting ?
If the client has disabled cookie in the browser then coockie are not work for session management. In that case you can use URL rewriting technique for session managment. URL rewriting will always work.
Advantage of URL Rewriting
- It will always work whether cookie is disabled or not (browser independent).
- Extra form submission is not required on each pages
Dis-advantage of URL Rewriting
- Generate more network traffic.
- It will work only with links.
- It can send Only textual information.
- Less secqure because query string in session id displace on address bar.
Example of session tracking by using URL Rewriting
index.html
<form action="servlet1"> Name:<input type="text" name="userName"/> <br/> <input type="submit" value="continue"/> </form>
FirstServlet.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response){ try{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); String n=request.getParameter("userName"); out.print("Welcome "+n); HttpSession session=request.getSession(); session.setAttribute("uname",n); out.print("<a href='servlet2'>visit</a>"); out.close(); }catch(Exception e){System.out.println(e);} } }
SecondServlet.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SecondServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) try{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); //getting value from the query string String n=request.getParameter("uname"); out.print("Hello "+n); out.close(); } catch(Exception e){System.out.println(e);} } }
web.xml
<web-app> <servlet> <servlet-name>s1</servlet-name> <servlet-class>FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>s1</servlet-name> <url-pattern>/servlet1</url-pattern> </servlet-mapping> <servlet> <servlet-name>s2</servlet-name> <servlet-class>SecondServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>s2</servlet-name> <url-pattern>/servlet2</url-pattern> </servlet-mapping> </web-app>
Google Advertisment