Session Using URLRewriting
URLRewriting can be used in place where we don't want to use cookies. It is used to maintain the session. Whenever the browser sends a request then it is always interpreted as a new request because http protocol is a stateless protocol as it is not persistent. Whenever we want that out request object to stay alive till we decide to end the request object then, there we use the concept of session tracking. In session tracking firstly a session object is created when the first request goes to the server. Then server creates a token which will be used to maintain the session. The token is transmitted to the client by the response object and gets stored on the client machine. By default the server creates a cookie and the cookie get stored on the client machine.
URLRewriting can be used where we the cookies are disabled. Its a good practice to use URL Rewriting. In URLRewriting a string is appended
Code of the program is given below:
<html> <head> <title>How to use encodeURL in jsp</title> </head> <body> <form method = "post" action = "EncodeURLProgram.jsp"> <font size = 6>Enter your name : <input type = "text" name = "name"></font><br><br> <font size = 6>Enter your password : <input type="password" name = "pwd" ></font><br><br> <input type = "submit" name = "submit" value = "submit" > </form> </body> </html>
<%@ page session ="true" %> <% String name = request.getParameter("name"); String password = request.getParameter("pwd"); if(name.equals("Williams") && password.equals("abcde")) { session.setAttribute("username",name); String string = response.encode URL("NextPageAfterFirst.jsp?name=+name+&password=+password"); %> <font size = 6><p>Please click here to go forward : </p></font> <font size = 8><a href ='<%= string %>'>WelcomeEncodeURL.jsp</a></font> <%} else { String string = response.encode URL("encodeURL.jsp?name=+name+&password=+password");%> <font size = 6><p>You have entered a wrong value : Click here to go back : </p></font> <font size = 6><a href ='<%= string %>'>encodeURL.jsp</a></font> <% } %>
<html> <head> <title>Welcome in In the program of URL rewriting</title> </head> <body> <font size = 6>Hello</font> <%= session.getAttribute("username") %> </body> </html>
The output of the program is given below:
When the value are correct:
On clicking the WelcomeEncodeURL.jsp the result will be.
If the value is incorrect:
The output will be again the encodeURL.jsp