Nearby lessons

19 of 34

Servlet - Session Management (URL Rewriting)

📌 What You Will Learn
  • Understand Demo Program for session management by url rewriting
  • See complete working code examples

Session Management (URL Rewriting) is an essential part of the Java Servlet technology. This lesson explains Demo Program for session management by url rewriting, SessionServlet1.java and SessionServlet2.java with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid.

Demo Program for session management by url rewriting

login.html:

Demo Program for session management by url rewriting

Example02
JCode Cell
1 
2<html>
3<body>
4<form action="/session5/test1">
5<h1>Enter session information<br>
6Name:<input type="text" name="uname"><br>
7<input type="submit"/></h1>
8</form>
9</body>
10</html>
11

SessionServlet1.java

Example03
JCode Cell
1 
2import java.io.*;
3import javax.servlet.*;
4import javax.servlet.http.*;
5import javax.servlet.annotation.*;
6@WebServlet("/test1")
7public class SessionServlet1 extends HttpServlet
8{
9public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
10{
11PrintWriter out=res.getWriter();
12String name=req.getParameter("uname");
13out.println("<h1>Welcome to Durga Software Solutions</h1>");
14out.println("<a href=/session5/test2?name="+name+">Click here to get user name</a>");
15}
16}
17

SessionServlet2.java

session5

|-login.html

|-WEB-INF

|-classes

|-SessionServlet1.class

|-SessionServlet2.class

Advantages of URL Rewriting:

There is no chance of disabling url rewriting technique. Hence it will work always.

Limitations of URL Rewriting:

1.It is very difficult to rewrite all urls to append session information.Hence it is the most painful

technique.

2.URL Rewriting will work only for dynamic documents.

Session Management by using Hidden Form Fields

It is not official technique from SUN Micro Systems.It is just Programmer's trick to remember client

information.

In case of Hidden Form Field a hidden (invisible) textfield is used for maintaining the state of an

user.

In such case, we store the information in the hidden field, which is required for future purpose.

We can declare hidden form field as follows..

<input type="hidden" name="uname" value="chitu">

Advantage of Hidden Form Field

It will always work whether cookie is disabled or not.

Disadvantage of Hidden Form Field:

  • It is maintained at server side.
  • Extra form submission is required on each page.
  • Session information should be text data
Example04
JCode Cell
1 
2import java.io.*;
3import javax.servlet.*;
4import javax.servlet.http.*;
5import javax.servlet.annotation.*;
6@WebServlet("/test2")
7public class SessionServlet2 extends HttpServlet
8{
9public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
10{
11PrintWriter out=res.getWriter();
12String name = req.getParameter("name");
13out.println("<h1>Hi "+name+" Good Morning...");
14}
15}
16
📝 Key Takeaways
  • Key ideas of Servlet - Session Management (URL Rewriting) explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3