Previous | Home | Next |
Answer: HTTP protocol.
Answer: There are four ways to track session:
Method 1: By URL rewriting
Method 2: Using Session object
Getting Session form HttpServletRequest object
HttpSession session = request.getSession(true);
Get a Value from the session
session.getValue(session.getId());
Adding values to session
cart = new Cart(); session.putValue(session.getId(), cart);
At the end of the session, we can inactivate the session by using the following command
session.invalidate();
Method 3: Using cookies
Method 4: Using hidden fields
Answer: Servelt can invoke other Web resources in two ways: indirect and direct.
Indirect Way: Servlet will return the resultant HTML to the browser which will point to another Servlet (Web resource)
Direct Way: We can call another Web resource (Servelt / Jsp) from Servelt program itself, by using RequestDispatcher object.
You can get this object using getRequestDispatcher("URL") method. You can get this object from either a request or a Context.
Example:
RequestDispatcher dispatcher = request.getRequestDispatcher ("/jspsample.jsp"); if (dispatcher != null) dispatcher.forward(request, response); }
Answer: Using include method of a RequestDispatcher object.Included WebComponent (Servlet / Jsp) cannot set headers or call any method (for example, setCookie) that affects the headers of the response.
Example:
RequestDispatcher dispatcher=getServletContext().getRequestDispatcher ("/banner"); if (dispatcher != null) dispatcher.include(request, response); }
Q.15 What is the difference between the getRequestDispatcher(String path) ServletRequest interface and ServletContext interface?
Answer: The getRequestDispatcher(String path) method of ServletRequest interface accepts parameter the path to the resource to be included or forwarded to, which can be relative to the request of the calling servlet. If the path begins with a "/" it is interpreted as relative to the current context root.
The getRequestDispatcher(String path) method of ServletContext interface cannot accepts relative paths. All path must sart with a "/" and are interpreted as relative to curent context root. If the resource is not available, or if the server has not implemented a RequestDispatcher object for that type of resource, getRequestDispatcher will return null. Your servlet should be prepared to deal with this condition.
Answer: Using ServletContext, We can access data from its environment. Servlet context is common to all Servlets so all Servlets share the information through ServeltContext.
Answer: We need to use iText. A open source library for java. Please refer sourceforge site for sample servlet examples.
Q.18 What is the difference between using getSession(true) and getSession(false) methods?
Answer:
- getSession(true): This method will check whether already a session is existing for the user. If a session is existing, it will return that session object, Otherwise it will create new session object and return taht object.
- getSession(false): This method will check existence of session. If session exists, then it returns the reference of that session object, if not, this methods will return null.
Previous | Home | Next |