Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS6320 – Servlet Cookies L. Grewe 2 What is a cookie? Name-value bindings sent by a server to a web browser and then sent back unchanged by the browser.

Similar presentations


Presentation on theme: "1 CS6320 – Servlet Cookies L. Grewe 2 What is a cookie? Name-value bindings sent by a server to a web browser and then sent back unchanged by the browser."— Presentation transcript:

1

2 1 CS6320 – Servlet Cookies L. Grewe

3 2 What is a cookie? Name-value bindings sent by a server to a web browser and then sent back unchanged by the browser each time it accesses that server. Name-value bindings sent by a server to a web browser and then sent back unchanged by the browser each time it accesses that server. e.g. login = grewe (name = value)e.g. login = grewe (name = value) Used for authenticating, tracking, and maintaining specific information about users, such as site preferences or the contents of their electronic shopping carts. Used for authenticating, tracking, and maintaining specific information about users, such as site preferences or the contents of their electronic shopping carts.authenticatingelectronic shopping cartsauthenticatingelectronic shopping carts

4 3 Servlets and Cookies Java Servlet API provides comfortable mechanisms to handle cookies Java Servlet API provides comfortable mechanisms to handle cookies The class javax.servlet.http.Cookie represents a cookie The class javax.servlet.http.Cookie represents a cookie Getter methods:Getter methods: getName(), getValue(), getPath(), getDomain(), getMaxAge(), getSecure() … getName(), getValue(), getPath(), getDomain(), getMaxAge(), getSecure() … Setter methods:Setter methods: setValue(), setPath(), setDomain(), setMaxAge() … setValue(), setPath(), setDomain(), setMaxAge() …

5 4 Servlets and Cookies (cont) Get the cookies from the service request: Get the cookies from the service request: Cookie[] HttpServletRequest.getCookies() Add a cookie to the service response: Add a cookie to the service response: HttpServletResponse.addCookie(Cookie cookie)

6 5 Webpage that triggers Servlet to create a Cookie Insert your Name What is your name? getname.html

7 6 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

8 7 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(" Welcome Back " + user + " "); } WelcomeBack.java


Download ppt "1 CS6320 – Servlet Cookies L. Grewe 2 What is a cookie? Name-value bindings sent by a server to a web browser and then sent back unchanged by the browser."

Similar presentations


Ads by Google