1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc)

JSP Tutorials

JSP Cookies

Cookies are short pieces of data sent by web servers to the client browser. The cookies are saved to clients hard disk in the form of small text file. Cookies helps the web servers to identify web users, by this way server tracks the user. Cookies pay very important role in the session tracking.

In JSP cookie are the object of the class javax.servlet.http.Cookie. This class is used to creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management.

Cookie objects have the following methods:

getComment() :
Returns the comment describing the purpose of this cookie, or null if no such comment has been defined.

getMaxAge() :
Returns the maximum specified age of the cookie.

getName() :
Returns the name of the cookie.

getPath() :
Returns the prefix of all URLs for which this cookie is targeted.

getValue():
Returns the value of the cookie.

setComment(String) :
If a web browser presents this cookie to a user, the cookie's purpose will be described using this comment.

setMaxAge(int) : ( int seconds)
Sets the maximum age of the cookie.
The cookie will expire after that mentioned seconds have passed.
Negative values indicate that cookie is not stored persistently, and will be deleted when the user web browser exits.
A zero value causes the cookie to be deleted.

setPath(String):
This cookie should be presented only with requests beginning with this URL.
For Example :
cookie.setPath("http://localhost:8080/test/tutorial/");
Then the cookie will be available for all with in "/test/tutorial/" and not avilable for "/test/jdbc/".


setValue(String):
Sets the value of the cookie. Values with various special characters (white space, brackets and parentheses, the equals sign, comma, double quote, slashes, question marks, the "at" sign, colon, and semicolon) should be avoided. Empty values may not behave the same way on all browsers.


Example Using Cookies :

page1.jsp
------------------------------------------
<html>
<head>
<title>Cookie save Form</title>
</head>
<body>
<form method="post" action="savecookie.jsp">
<p><b>Enter Your Name: </b><input type="text" name="name"><br>
<input type="submit" value="Submit">

</form>

</body>
</html>

In the savecookie.jsp
------------------------------------------------

<%@ page language="java" %>
<%@ page import="java.util.*"%>
<%
String name=request.getParameter("name");
//Set Cookie
Date now = new Date();
String timestamp = now.toString();
Cookie cookie = new Cookie ("name",name);
cookie.setMaxAge(30 * 24 * 60 * 60); //Set Cookies for 30 days
response.addCookie(cookie);

%>

<html>
<head>
<title>Cookie Saved</title>
</head>
<body>
<p><a href="showcookie.jsp">Page to view the cookie value</a><p>

</body>
</html>


In the showcookie.jsp ( This is the JSP to display the cookies)
--------------------------------------------
<%@ page language="java" %>
<%
String cookie_name = "name";
Cookie cookies [] = request.getCookies ();
Cookie myCookie = null;
if (cookies != null)
{
for (int i = 0; i < cookies.length; i++)
{
if (cookies [i].getName().equals (cookie_name))
// we have added the cookie with name "name"
{
myCookie = cookies[i];
break;
}
}
}
%>
<html>
<head>
<title>Show Cookie</title>
</head>
<body>


<%
if (myCookie == null) {
%>
No Cookie found with the name <%=cookie_name%>
<%
} else {
%>
<p>Cooke Value: <%=myCookie.getValue()%>.
<%
}
%>
</body>
</html>

This will display the cookie value .

Reviews and Comments


PMP, CAPM, PMI is a registered certification mark of the Project Management Institute, Inc

Copyright © www.techfaq360.com 2016


About US | Contact US | Privacy Policy | Terms and Conditions  | Website disclaimer  | Cancellation and Refund Policy  | Shipping & Delivery Policy