Url rewriting is a process of appending or modifying any url structure while loading a page.
The request made by client is always a new request and the server can not identify whether the current request is send by a new client or the previous same client. Due to This property of HTTP protocol and Web Servers are called stateless. But many times we should know who is client in the processing request.
In any social networking site during login to till logout the server should know who is client so that server can manage all the request according to the user need.
This problem is solved by Session in Servlet.
Session : Session is a state between client and server and it contain multiple request and response between client and server. As we know that HTTP and Web Server both are stateless, the only way to maintain a session is when some unique information about the session (session id) is passed between server and client in every request and response.
Following are some ways by which we can provide unique id in request and response :
If your browser does not support cookies, URL rewriting provides you with another session tracking alternative. URL rewriting is a method in which the requested URL is modified to include a session ID. There are several ways to perform URL rewriting.
Here we are discussing session management technique in which we pass control from one servlet to another. The Syntax for passing control from one servlet to another is as follows
Note : Generally we write web.xml file for request dispatcher but in this example we use annotation so their is no need of creating web.xml file.
Syntax of annotation is :
Example of Session tracking using URL rewriting using annotation
Explanation(Follow the diagram)
When you deploy your project in eclipse the first page which is loaded in the HTML form whose form action is first servlet so the control will go to servlet1. In this case, we name servlet1 as FirstServlet where the username is printed. In FirstServlet we provide url where we transfer the control to servlet2 using url rewriting. In our case we name servlet2 as SecondServlet.
Advantage of URL Rewriting :
- It doesn’t depend upon cookies and will work whether cookies are enabled or disabled
- Extra form submission is not required on all pages
Disadvantage of URL Rewriting
It will work only with links ad can send only text
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference between Java Servlet and CGI
- Difference between ServletConfig and ServletContext in Java Servlet
- Servlet Collaboration In Java Using RequestDispatcher and HttpServletResponse
- Javax.servlet.http.Cookie class in Java
- Java Servlet and JDBC Example | Insert data in MySQL
- Hidden Form Field using Annotation | Java Servlet
- Difference between Servlet and JSP
- Starting with first Servlet Application
- Life Cycle of a Servlet
- The HttpSession Interface in Servlet
- How to add Image to MySql database using Servlet and JDBC
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.