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 Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Servlet Collaboration In Java Using RequestDispatcher and HttpServletResponse
- Hidden Form Field using Annotation | Java Servlet
- How to add Image to MySql database using Servlet and JDBC
- Javax.servlet.http.Cookie class in Java
- Java Servlet and JDBC Example | Insert data in MySQL
- Difference between ServletConfig and ServletContext in Java Servlet
- Difference between Java Servlet and CGI
- Starting with first Servlet Application
- The HttpSession Interface in Servlet
- Life Cycle of a Servlet
- Difference between Servlet and JSP
- URL Encoding/Decoding using Base64 in Java
- Reading from a URL using URLConnection Class
- URL class in Java with Examples
- Check if URL is valid or not in Java
- Finding IP address of a URL in Java
- URL sameFile() method in Java with Examples
- URL getDefaultPort() method in Java with Examples
- URL toURI() method in Java with Examples
- URL getHost() method in Java with Examples