How can I enable session tracking for JSP pages if the browser has disabled cookies?
JSP Interview Questions and Answers
(Continued from previous question...)
22. How can I enable session tracking for JSP pages if the browser has disabled cookies?
We know that session tracking uses cookies by default to associate a session identifier with a unique user. If the browser does not support cookies, or if cookies are disabled, you can still enable session tracking using URL rewriting. URL rewriting essentially includes the session ID within the link itself as a name/value pair.
However, for this to be effective, you need to append the session ID for each and every link that is part of your servlet response. Adding the session ID to a link is greatly simplified by means of of a couple of methods: response.encodeURL() associates a session ID with a given URL, and if you are using redirection, response.encodeRedirectURL() can be used by giving the redirected URL as input.
Both encodeURL() and encodeRedirectedURL() first determine whether cookies are supported by the browser; if so, the input URL is returned unchanged since the session ID will be persisted as a cookie.
Consider the following example, in which two JSP files, say hello1.jsp and hello2.jsp, interact with each other.
Basically, we create a new session within hello1.jsp and place an object within this session. The user can then traverse to hello2.jsp by clicking on the link present within the page.Within hello2.jsp, we simply extract the object that was earlier placed in the session and display its contents. Notice that we invoke the encodeURL() within hello1.jsp on the link used to invoke hello2.jsp; if cookies are disabled,
the session ID is automatically appended to the URL, allowing hello2.jsp to still retrieve the session object. Try this example first with cookies enabled. Then disable cookie support, restart the brower, and try again. Each time you should see the maintenance of the session across pages.
Do note that to get this example to work with cookies disabled at the browser, your JSP engine has to support URL rewriting.
hello1.jsp
hello2.jsp
hello2.jsp
<%
Integer i= (Integer )session.getValue("num");
out.println("Num value in session is "+i.intValue());
(Continued on next question...)
Other Interview Questions
- What is a JSP and what is it used for?
- What is difference between custom JSP tags and beans?
- What are the two kinds of comments in JSP and what's the difference between them.
- What is JSP technology?
- What is JSP page?
- What are the implicit objects?
- How many JSP scripting elements and what are they?
- Why are JSP pages the preferred API for creating a web-based client program?
- Is JSP technology extensible?
- Can we use the constructor, instead of init(), to initialize servlet?
- How can a servlet refresh automatically if some new data has entered the database?
- The code in a finally clause will never fail to execute, right?
- How many messaging models do JMS provide for and what are they?
- What information is needed to create a TCP Socket?
- What Class.forName will do while loading drivers?
- How to Retrieve Warnings?
- How many JSP scripting elements are there and what are they?
- In the Servlet 2.4 specification SingleThreadModel has been deprecated, why?
- What are stored procedures? How is it useful?
- How do I include static files within a JSP page?
- Why does JComponent have add() and remove() methods but Component does not?
- How can I enable session tracking for JSP pages if the browser has disabled cookies?
- How do I prevent the output of my JSP or Servlet pages from being cached by the browser?
- How do you restrict page errors display in the JSP page?
- What JSP lifecycle methods can I override?
- How do I perform browser redirection from a JSP page?
- How does JSP handle run-time exceptions?
- How do I use comments within a JSP page?
- Is it possible to share an HttpSession between a JSP and EJB? What happens when I change a value in the HttpSession from inside an EJB?
- How can I implement a thread-safe JSP page?
- How can I declare methods within my JSP page?
- Can I stop JSP execution while in the midst of processing a request?
- Can a JSP page process HTML FORM data?
- Is there a way to reference the "this" variable within a JSP page?
- How do you pass control from one JSP page to another?
- Is there a way I can set the inactivity lease period on a per-session basis?
- How does a servlet communicate with a JSP page?
- How do I include static files within a JSP page?
- Can you make use of a ServletOutputStream object from within a JSP page?
- Can a JSP page instantiate a serialized bean?
- What is JSP?
- How do I mix JSP and SSI #include?
- How do I mix JSP and SSI #include? What is the difference between include directive & jsp:include action?
- How do you prevent the Creation of a Session in a JSP Page and why? What is the difference between include directive & jsp:include action?
- How do I use a scriptlet to initialize a newly instantiated bean?
- How can I set a cookie and delete a cookie from within a JSP page?
- How do you connect to the database from JSP?
- What is the page directive is used to prevent a JSP page from automatically creating a session?
- How do you delete a Cookie within a JSP?
- Can we implement an interface in a JSP?
- What is the difference between ServletContext and PageContext?
- What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?
- How to pass information from JSP to included JSP?
- How is JSP include directive different from JSP include action.
- Can we override the jspInit(), _jspService() and jspDestroy() methods?
- Why is _jspService() method starting with an '_' while other life cycle methods do not?
- Explain the life cycle of JSP?
- What happens when a page is statically included in another JSP page?
- A JSP page, include.jsp, has a instance variable "int a", now this page is statically included in another JSP page, index.jsp, which has a instance variable "int a" declared. What happens when the index.jsp page is requested by the client?
- Can you override jspInit() method? If yes, In which cases?
- What is the difference between directive include and jsp include?
- What is the difference between RequestDispatcher and sendRedirect?
- How does JSP handle runtime exceptions?
- How can my application get to know when a HttpSession is removed?
|