State-Management:
HTTP protocol that is used on Internet to send request to receive response is a stateless protocol. I.e. for each request, new http connection is created between client and server and this connection is closed as soon as last byte of response is received by the client.
Q. Why used stateless protocol?
A. Proper utilization of network
Following four methods:
Cookies:
Cookies are small bits of textual information that a web server sends to a browser and that the browser later returns unchanged when visiting the same web site or domain.
Non persistent: by default are non persistent that is that are lost as soon as communication with the server is stop.
Persistent: persistent cookies are saving in the form of text file by the browser to be reuse when the communication with the server is stop.
Limitation:
Browser dependent method. All the browsers do not’s support cookies for the cookies can be disable from the browser.
Only text information can be persistent with the help of cookies.
Object of javax.servlet.http.Cookie class represent a class.
Public cookie (String name, String vlaue)
Methods:
Public String getName();
Public String getValue();
Public String setmaxAge(time in second);
Public void addCookie();
Public cookie [] getCookies();
Ex:
Cookie ck=new Cookie (“user”,name);
ck.setMaxAge(10000);
response.addCookie(ck);
Hidden Form Field:
It is an invisible text field that is use by the server to maintain state between multiple requests.
<input type=”hidden”> is used to create an invisible text field in a page. Value to be persistent from one request to another is set in the invisible text field. When ever a request is made from the page containing invisible text field value of invisible text fields are send as request parameter.
Limitation:
Hidden Form Field can only be used if there is a form in the page.
Only text information is persisted.
Ex:
Out.println(“<input type=’hiddden’ name=’txtName’ value=”+name”>”);
URL Rewriting
It is used to maintain states between multiple requests in case of hyper links are used to send request.
In case of url rewriting information to be persisted or to be made available in the target resource of the hyperlink is appended as request parameter to the url.
<a href=UrlOfTargetResource?parameterName=dynamicAppendValue&…>Txt</a>
Limitation:
Only in case of hyperlink is used
Ex:
Out.println(“<a href=WelcomeServlet?UserName=”+name+”>Take a Tour</a>”)
In the above three case first of request go to client.
Session
Session object represents a buffer on the server that can be used to maintain state between multiple requests made by client. For each user a session object is provided by the user.
Session is represented by the implementation of javax.servlet.http.HttpSession interface.
Methods:
setAttribute(): used to store an attribute in the session object.
Public void setAttribute(String name, Object value)
Public Object getAttribute(Sting name);
Public Enumeration getAttributeNames();
Public void removeAttribute(String name);
Public Boolean isNew();
Public void setMaxInactiveInterval(long second)
Public void invalidate(); relies the session object
getSession() method of HttpRequestRequest interface is used to create a new session object or to obtain the reference of a existing session object.
HttpSession session=request.getSession(false);
Passing false means the method returns a pre-existing session, or null if there was no session associated with this client.
Passing true same as getSession()
If(session.isNew())
isNew() returns true if the client has not yet responded with this session ID.
If(session==null)
Now we can test for whether there was already a session (the no-arg getSession() would never return null.)
HTTP protocol that is used on Internet to send request to receive response is a stateless protocol. I.e. for each request, new http connection is created between client and server and this connection is closed as soon as last byte of response is received by the client.
Q. Why used stateless protocol?
A. Proper utilization of network
Following four methods:
Cookies:
Cookies are small bits of textual information that a web server sends to a browser and that the browser later returns unchanged when visiting the same web site or domain.
Non persistent: by default are non persistent that is that are lost as soon as communication with the server is stop.
Persistent: persistent cookies are saving in the form of text file by the browser to be reuse when the communication with the server is stop.
Limitation:
Browser dependent method. All the browsers do not’s support cookies for the cookies can be disable from the browser.
Only text information can be persistent with the help of cookies.
Object of javax.servlet.http.Cookie class represent a class.
Public cookie (String name, String vlaue)
Methods:
Public String getName();
Public String getValue();
Public String setmaxAge(time in second);
Public void addCookie();
Public cookie [] getCookies();
Ex:
Cookie ck=new Cookie (“user”,name);
ck.setMaxAge(10000);
response.addCookie(ck);
Hidden Form Field:
It is an invisible text field that is use by the server to maintain state between multiple requests.
<input type=”hidden”> is used to create an invisible text field in a page. Value to be persistent from one request to another is set in the invisible text field. When ever a request is made from the page containing invisible text field value of invisible text fields are send as request parameter.
Limitation:
Hidden Form Field can only be used if there is a form in the page.
Only text information is persisted.
Ex:
Out.println(“<input type=’hiddden’ name=’txtName’ value=”+name”>”);
URL Rewriting
It is used to maintain states between multiple requests in case of hyper links are used to send request.
In case of url rewriting information to be persisted or to be made available in the target resource of the hyperlink is appended as request parameter to the url.
<a href=UrlOfTargetResource?parameterName=dynamicAppendValue&…>Txt</a>
Limitation:
Only in case of hyperlink is used
Ex:
Out.println(“<a href=WelcomeServlet?UserName=”+name+”>Take a Tour</a>”)
In the above three case first of request go to client.
Session
Session object represents a buffer on the server that can be used to maintain state between multiple requests made by client. For each user a session object is provided by the user.
Session is represented by the implementation of javax.servlet.http.HttpSession interface.
Methods:
setAttribute(): used to store an attribute in the session object.
Public void setAttribute(String name, Object value)
Public Object getAttribute(Sting name);
Public Enumeration getAttributeNames();
Public void removeAttribute(String name);
Public Boolean isNew();
Public void setMaxInactiveInterval(long second)
Public void invalidate(); relies the session object
getSession() method of HttpRequestRequest interface is used to create a new session object or to obtain the reference of a existing session object.
HttpSession session=request.getSession(false);
Passing false means the method returns a pre-existing session, or null if there was no session associated with this client.
Passing true same as getSession()
If(session.isNew())
isNew() returns true if the client has not yet responded with this session ID.
If(session==null)
Now we can test for whether there was already a session (the no-arg getSession() would never return null.)
No comments:
Post a Comment