/Users/lyon/j4p/src/net/server/servlets/CookieExample.java

1    package net.server.servlets; 
2     
3    /* $Id: CookieExample.java,v 1.3 1999/04/15 04:21:32 duncan Exp $ 
4     * 
5     */ 
6     
7    import javax.servlet.ServletException; 
8    import javax.servlet.http.Cookie; 
9    import javax.servlet.http.HttpServlet; 
10   import javax.servlet.http.HttpServletRequest; 
11   import javax.servlet.http.HttpServletResponse; 
12   import java.io.IOException; 
13   import java.io.PrintWriter; 
14   import java.util.ResourceBundle; 
15    
16   /** 
17    * Example servlet showing request headers 
18    * 
19    * @author James Duncan Davidson <duncan@eng.sun.com> 
20    */ 
21    
22   public class CookieExample extends HttpServlet { 
23    
24       ResourceBundle rb = ResourceBundle.getBundle("LocalStrings"); 
25    
26       public void doGet(HttpServletRequest request, 
27                         HttpServletResponse response) 
28               throws IOException, ServletException { 
29           response.setContentType("text/html"); 
30    
31           PrintWriter out = response.getWriter(); 
32           out.println("<html>"); 
33           out.println("<body bgcolor=\"white\">"); 
34           out.println("<head>"); 
35    
36           String title = rb.getString("cookies.title"); 
37           out.println("<title>" + title + "</title>"); 
38           out.println("</head>"); 
39           out.println("<body>"); 
40    
41           // relative links 
42    
43           out.println("<a href=\"../servlets/cookies.html\">"); 
44           out.println("<img src=\"../images/code.gif\" height=24 " + 
45                   "width=24 align=right border=0 alt=\"view code\"></a>"); 
46           out.println("<a href=\"../servlets/index.html\">"); 
47           out.println("<img src=\"../images/return.gif\" height=24 " + 
48                   "width=24 align=right border=0 alt=\"return\"></a>"); 
49    
50           out.println("<h3>" + title + "</h3>"); 
51    
52           Cookie[] cookies = request.getCookies(); 
53           if (cookies.length > 0) { 
54               out.println(rb.getString("cookies.cookies") + "<br>"); 
55               for (int i = 0; i < cookies.length; i++) { 
56                   Cookie cookie = cookies[i]; 
57                   out.print("Cookie Name: " + cookie.getName() + "<br>"); 
58                   out.println("  Cookie Value: " + cookie.getValue() + 
59                           "<br><br>"); 
60               } 
61           } else { 
62               out.println(rb.getString("cookies.no-cookies")); 
63           } 
64    
65           String cookieName = request.getParameter("cookiename"); 
66           String cookieValue = request.getParameter("cookievalue"); 
67           if (cookieName != null && cookieValue != null) { 
68               Cookie cookie = new Cookie(cookieName, cookieValue); 
69               response.addCookie(cookie); 
70               out.println("<P>"); 
71               out.println(rb.getString("cookies.set") + "<br>"); 
72               out.print(rb.getString("cookies.name") + "  " + cookieName + 
73                       "<br>"); 
74               out.print(rb.getString("cookies.value") + "  " + cookieValue); 
75           } 
76    
77           out.println("<P>"); 
78           out.println(rb.getString("cookies.make-cookie") + "<br>"); 
79           out.print("<form action=\""); 
80           out.println("CookieExample\" method=POST>"); 
81           out.print(rb.getString("cookies.name") + "  "); 
82           out.println("<input type=text length=20 name=cookiename><br>"); 
83           out.print(rb.getString("cookies.value") + "  "); 
84           out.println("<input type=text length=20 name=cookievalue><br>"); 
85           out.println("<input type=submit></form>"); 
86    
87    
88           out.println("</body>"); 
89           out.println("</html>"); 
90       } 
91    
92       public void doPost(HttpServletRequest request, 
93                          HttpServletResponse response) 
94               throws IOException, ServletException { 
95           doGet(request, response); 
96       } 
97    
98   } 
99    
100   
101