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

1    package net.server.servlets; 
2     
3    /* $Id: RequestParamExample.java,v 1.3 1999/04/15 04:21:33 duncan Exp $ 
4     * 
5     */ 
6     
7    import javax.servlet.ServletException; 
8    import javax.servlet.http.HttpServlet; 
9    import javax.servlet.http.HttpServletRequest; 
10   import javax.servlet.http.HttpServletResponse; 
11   import java.io.IOException; 
12   import java.io.PrintWriter; 
13   import java.util.ResourceBundle; 
14    
15    
16   /** 
17    * Example servlet showing request headers 
18    * 
19    * @author James Duncan Davidson <duncan@eng.sun.com> 
20    */ 
21    
22   public class RequestParamExample extends HttpServlet { 
23    
24    
25       ResourceBundle rb = ResourceBundle.getBundle("LocalStrings"); 
26    
27       public void doGet(HttpServletRequest request, 
28                         HttpServletResponse response) 
29               throws IOException, ServletException { 
30           response.setContentType("text/html"); 
31    
32           PrintWriter out = response.getWriter(); 
33           out.println("<html>"); 
34           out.println("<body>"); 
35           out.println("<head>"); 
36    
37           String title = rb.getString("requestparams.title"); 
38           out.println("<title>" + title + "</title>"); 
39           out.println("</head>"); 
40           out.println("<body bgcolor=\"white\">"); 
41    
42           // img stuff not req'd for source code html showing 
43    
44           // all links relative 
45    
46           out.println("<a href=\"../servlets/reqparams.html\">"); 
47           out.println("<img src=\"../images/code.gif\" height=24 " + 
48                   "width=24 align=right border=0 alt=\"view code\"></a>"); 
49           out.println("<a href=\"../servlets/index.html\">"); 
50           out.println("<img src=\"../images/return.gif\" height=24 " + 
51                   "width=24 align=right border=0 alt=\"return\"></a>"); 
52    
53           out.println("<h3>" + title + "</h3>"); 
54           String firstName = request.getParameter("firstname"); 
55           String lastName = request.getParameter("lastname"); 
56           out.println(rb.getString("requestparams.params-in-req") + "<br>"); 
57           if (firstName != null || lastName != null) { 
58               out.println(rb.getString("requestparams.firstname")); 
59               out.println(" = " + firstName + "<br>"); 
60               out.println(rb.getString("requestparams.lastname")); 
61               out.println(" = " + lastName); 
62           } else { 
63               out.println(rb.getString("requestparams.no-params")); 
64           } 
65           out.println("<P>"); 
66           out.print("<form action=\""); 
67           out.print("RequestParamExample\" "); 
68           out.println("method=POST>"); 
69           out.println(rb.getString("requestparams.firstname")); 
70           out.println("<input type=text size=20 name=firstname>"); 
71           out.println("<br>"); 
72           out.println(rb.getString("requestparams.lastname")); 
73           out.println("<input type=text size=20 name=lastname>"); 
74           out.println("<br>"); 
75           out.println("<input type=submit>"); 
76           out.println("</form>"); 
77    
78           out.println("</body>"); 
79           out.println("</html>"); 
80       } 
81    
82       public void doPost(HttpServletRequest request, 
83                          HttpServletResponse response) 
84               throws IOException, ServletException { 
85           doGet(request, response); 
86       } 
87    
88   } 
89