1 cs6320 – servlet cookies l. grewe 2 what is a cookie? name-value bindings sent by a server to a...
Post on 22-Dec-2015
217 views
TRANSCRIPT
11
CS6320 – Servlet CS6320 – Servlet CookiesCookies
L. GreweL. Grewe
22
What is a cookie?What is a cookie?
Name-value bindings sent by a server to a Name-value bindings sent by a server to a web browser and then sent back web browser and then sent back unchanged by the browser each time it unchanged by the browser each time it accesses that server.accesses that server.• e.g. login = grewe e.g. login = grewe (name = value)(name = value)
Used for Used for authenticatingauthenticating, tracking, and , tracking, and maintaining specific information about maintaining specific information about users, such as site preferences or the users, such as site preferences or the contents of their contents of their electronic shopping cartselectronic shopping carts. .
33
Servlets and CookiesServlets and Cookies
Java Servlet API provides comfortable Java Servlet API provides comfortable mechanisms to handle cookiesmechanisms to handle cookies
The class The class javax.servlet.http.Cookiejavax.servlet.http.Cookie represents represents a cookiea cookie• Getter methods: Getter methods:
getName()getName(), , getValue()getValue(), , getPath()getPath(), , getDomain()getDomain(), , getMaxAge()getMaxAge(), , getSecure()getSecure()……
• Setter methods: Setter methods: setValue()setValue(),, setPath() setPath(), , setDomain()setDomain(),,
setMaxAge()setMaxAge()……
44
Servlets and Cookies (cont)Servlets and Cookies (cont)
Get the cookies from the service Get the cookies from the service
request:request:Cookie[]Cookie[] HttpServletRequest.getCookies() HttpServletRequest.getCookies()
Add a cookie to the service response:Add a cookie to the service response:HttpServletResponse.addCookie(Cookie cookie)HttpServletResponse.addCookie(Cookie cookie)
55
Webpage that triggers Servlet to Webpage that triggers Servlet to create a Cookiecreate a Cookie
<html>
<head><title>Insert your Name</title></head>
<body> <h1>What is your name?</h1>
<form action="welcomeback" method="get">
<p>
<input type="text" name="username" />
<input type="submit" />
</p>
</form>
</body>
</html>
getname.html
66
An Example (cont)An Example (cont)
public class WelcomeBack extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String user = req.getParameter("username");
if (user == null) { // Find the "username" cookie
Cookie[] cookies = req.getCookies();
for (int i = 0; cookies != null && i < cookies.length; ++i) {
if (cookies[i].getName().equals("username"))
user = cookies[i].getValue();
}
} else res.addCookie(new Cookie("username", user));
WelcomeBack.java
77
An Example (cont)An Example (cont)
if (user == null) // No parameter and no cookie
res.sendRedirect("getname.html");
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html><body><h1>Welcome Back " + user
+ "</h1></body></html>");
}
} WelcomeBack.java