Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
JSP
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Cookies in JSP

                          

In world wide web the protocol we use is a http protocol. The Http protocol is a stateless protocol means that it can't keep a state i.e. it can't persist values. To maintain a session we used the concept of cookies.

When cookie based session management is used, a token is generated which contains user's information, is sent to the browser by the server. The cookie is sent back to the server when the user sends a new request. By this cookie, the server is able to identify the user. In this way the session is maintained. Cookie is nothing but a name- value pair, which is stored on the client machine. By default the cookie is implemented in most of the browsers. If we want then we can also disable the cookie. For security reasons, cookie based session management uses two types of cookies.

1) Non- secure session cookie: This cookie can flow between the browser and server under the SSL or non- SSL connection.

2) Secure authentication cookie: It is used to authenticate the data. This cookie flow over SSL. This type of authenication cookie are used where the information security is very important.

The code of the program is given below:

<%@ page language="java"  %>
<html>
<head>
<title>Cookie Input Form</title>
</head>
<body>
<form method="get" action="SettingCookie.jsp">
<p><b>Enter Your Name: </b><input type="text" name="username"><br>
<input type="submit" name = "submit" value="Submit">

</form>

</body>

 

<%@ page language="java"%>
<%
String username=request.getParameter("username");
if(username==null) username="";
Cookie cookie = new Cookie ("username",username);
cookie.setMaxAge(60 * 60);
response.addCookie(cookie);
%>
<html>
<head>
<title>Cookie Saved</title>
</head>
<body>
<p><a href="GettingCookie.jsp">Click here to see the cookie value</a><p>

</body>

 

<%@ page language="java"  %>
<%
String cookieName = "username";
Cookie cookies [] = request.getCookies ();
Cookie myCookie = null;
if (cookies != null)
{
for (int i = 0; i < cookies.length; i++) 
{
if (cookies [i].getName().equals (cookieName))
{
myCookie = cookies[i];
break;
}
}
}
%>
<html>
<head>
<title>Show Saved Cookie</title>
</head>
<body>

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

The output of the program is given below:

 

Download this example:

 


 

                          

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
Display Data from Data
struts2
XML messaging, Part 2
how to generate report
changing url
தà¯?à¯?ய�தà¯?à
update data in databse
SMTP
retreiving data from a
Swing Jlabel images
Combattons la programm
link tag in jsp
horizontal lines graph
struts framework archi
java string
Two-dimensional array
FileInputStream excel
Socket tcp using C
CSV File
php - stored procedure
CurrentTimeBean.java
Java Tip 85: Fun and g
Hibernate API
simple applet program
Ñ?Ð??Ñ?Ð????Ñ?Ð??Ñ?Ð??
tree
Photoshop Effects Ligh
Photoshop Photo Effect
pst.setInt()
assignments for oop
xml parsing with servl
What are the defined J
How to insert multiple
programming environmen
substring()
jxls
shopping cart
Photoshop Web Layouts
ajax application in ja
how to export zeroes t
java code packet sendi
HOW TO UPLOAD IMAGE FR
GMT
components in JSP
introduction to struts
apachi
login jsp
pass parameters
Session Tracking
â?¨â??Ñ??????â?¨â??Ñ??
standard deviation arr
<jsp:include
hibernate performance
J2ee
global forwards
syntaxs for j2me api's
DAO class
jsp drop down menu
Photoshop Effects Psyc
Ñ?оиÑ?п??Ñ?одÑ?п
config.xml
EJB 2.0 tutorials
Hospital UML diagrams
how we uplod a file fr
java combo box
Convert a String into
head first EJB
struts tomcat
compare two arraylist
EJB 2.0
10. Write a program th
breakÑ?â?£?Ñ?â???Ñ?â??
b)Count the number of
Photoshop Photo Retouc
read xml using java
Photoshop Effects Shin
servlet example progra
how to retrieve image
login authentication
Photoshop Textures and
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.