java类库详细讲解

源代码在线查看: getreqparam.html

软件大小: 5593 K
上传用户: add505
关键词: java
下载地址: 免注册下载 普通下载 VIP

相关代码

				
				
				
				Getting a Request Parameter in a Servlet
				(Java Developers Almanac Example)
				
				
				
				
				
				
								    BODY CODE  {font-family: Courier, Monospace;				           font-size: 11pt}				    TABLE, BODY				          {font-family: Verdana, Arial, Helvetica, sans-serif;				           font-size: 10pt}				    PRE   {font-family: Courier, Monospace;				           font-size: 10pt}				    H3    {font-family: Verdana, Arial, Helvetica, sans-serif;				           font-size: 11pt}				    A.eglink {text-decoration: none}				    A:hover.eglink {text-decoration: underline}				    -->
				
				
				
				
				
				The Java Developers Almanac 1.4
				
				        Order this book from Amazon.
				    
				
				
				
				
				
				
				
				
				
				
				
				Home
				    >
				    List of Packages
				    >
				    javax.servlet
				         [11 examples]
				        
				        >
				        Requests
				             [6 examples]
				            
				
				  
				    e1039.  
				    Getting a Request Parameter in a Servlet
				
				In a GET request, the request parameters are taken from the query
				string (the data following the question mark on the URL).  For
				example, the URL http://hostname.com?p1=v1&p2=v2 contains two
				request parameters - p1 and p2.  In a POST request, the
				request parameters are taken from both query string and the posted
				data which is encoded in the body of the request.  This example
				demonstrates how to get the value of a request parameter in either a
				GET or POST request.
				
				
				
				
				    // See also e1034 The Quintessential Servlet
				    
				    // This method is called by the servlet container to process a GET request.
				    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
				        doGetOrPost(req, resp);
				    }
				    
				    // This method is called by the servlet container to process a POST request.
				    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
				        doGetOrPost(req, resp);
				    }
				    
				    // This method handles both GET and POST requests.
				    private void doGetOrPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
				        // Get the value of a request parameter; the name is case-sensitive
				        String name = "param";
				        String value = req.getParameter(name);
				        if (value == null) {
				            // The request parameter 'param' was not present in the query string
				            // e.g. http://hostname.com?a=b
				        } else if ("".equals(value)) {
				            // The request parameter 'param' was present in the query string but has no value
				            // e.g. http://hostname.com?param=&a=b
				        }
				    
				        // The following generates a page showing all the request parameters
				        PrintWriter out = resp.getWriter();
				        resp.setContentType("text/plain");
				    
				        // Get the values of all request parameters
				        Enumeration enum = req.getParameterNames();
				        for (; enum.hasMoreElements(); ) {
				            // Get the name of the request parameter
				            name = (String)enum.nextElement();
				            out.println(name);
				    
				            // Get the value of the request parameter
				            value = req.getParameter(name);
				    
				            // If the request parameter can appear more than once in the query string, get all values
				            String[] values = req.getParameterValues(name);
				    
				            for (int i=0; i<values.length; i++) {
				                out.println("    "+values[i]);
				            }
				        }
				        out.close();
				    }
				
				
				
				
				             Related Examples
				        
				
				
				
				
				e1040. 
				    Preventing Concurrent Requests to a Servlet
				
				
				
				e1041. 
				    Getting the Requesting URL in a Servlet
				
				
				
				e1042. 
				    Getting a Request Header in a Servlet
				
				
				
				e1043. 
				    Processing a HEAD Request in a Servlet
				
				
				
				e1044. 
				    Getting the Client's Address in a Servlet
				
				
				
				
				
				
				
				
				© 2002 Addison-Wesley.
				
				
				
							

相关资源