Posts Tagged ‘getSession()’

Define URL Rewriting Frequently Asked Interview Question Answers

What Is URL Rewriting

 

As we know that session needs to be maintained because http is a stateless protocol.Due to which connection is lost or session is lost after every request response cycle between the client and the server.In order to maintain or track the session the server needs to get the additional information of the session id so that the session object pertaining to the client or server can be retrieved at the server side and the session can be managed.The session can be managed by either  cookies,hidden parameter or URL rewriting.

URL Rewriting is used in the cases where client browser does not accept cookies.In such case the server does not get the session id thus it is unable to relate he session object with the client or user.In order to maintain the session some extra piece of code needs to be added so that even if the cookies are not accepted at the browser level still the session can be maintained.URL Rewriting is something to fall upon as a precautionary measure.The code to get session and URL Rewriting is demonstrated below :

request.getSession();
out.println("<a href = "\"+response.encodeURL()+""\>Click Here</a>");

Here the anchor tag is used for URL Rewriting.Since the extra measure is used to take care that if the client browser does not accept cookies the session id is attached to the URL so that the session can be maintained.In URL Rewriting the session id is passed as an extra information with the URL that means rewriting of the URL is done.URL Rewriting can also be done with redirection as below :

response.sendRedirect(response.encodeRedirectURL("URL-Pattern of the servlet"));

The first time the server  tries with both cookies and URL Rewriting ,if it finds that the cookies are not coming it checks for session id as a part of URL Rewriting and finds that it has to fall upon URL Rewriting if the code has provisioned that.

 

Number of View :1965

Tags: , , , , , , , , ,

No Comments


Types Of Attributes In Servlet Servlet Scopes And Attributes Saving Data In A Servlet Attributes Interview Question

Types Of Attributes In Servlet

 

An attribute is an object used to pass some value from one servlet to another.An attribute in a servlet can be set at various scopes depending upon the visibility in the web application.As we know that there are three scopes at servlet level in the application.Theses scopes are :

1.Request

2.Session

3.Application

An attribute can be set on either of the scopes depending on the situation we want access the attribute value either at request,session or application level.

When we want the value of an attribute o be accessed at request level we use request attributes and likewise for session and application.

Types of attributes in servlet can be explained on the basis of scope it belongs to :

1.Request Attributes

2.Session attributes

3.Application Attributes

 

1.Request Attributes can be set or get as demonstrated below in the code :

req.setAttribute("name","value");
req.getAttribute("name");

2.Session Attributes  can be set or get as shown below :

req.getSession(false).setAttribute("name","value");
req.getSession(false).getAttribute("name");

3.Application Attributes can be set or get as below :

getServletContext().setAttribute("name","value");
getServletContext().getAttribute("name");

Now comes the question where to use what?

Request Attributes : If we want to retain or communicate  a value across servlets where request does not change,we use request attributes.This can be the situation where include or forward is happening between the servlets in a web application.In such cases where we want to communicate a value across servlets we can use request attributes which when set from one servlet can be get in other servlets.

Session Attributes : In cases where user specific data has to be retained or communicated, we use session attributes.We use session attributes when we do not want a user’s data to be interfered by other users.It should be noted that these attributes can be retrieved  even when redirect is happening provided the session is maintained .

 

Application Attributes : In cases where some value has to be set at application level and all the entities in the web application can read that value we use application attributes.

 

 

 

Number of View :1500

Tags: , , , , , , , , , ,

No Comments



SetPageWidth
Content Protected Using Blog Protector By: PcDrome.

Mahol Dot Org is Stephen Fry proof thanks to caching by WP Super Cache