1. 程式人生 > >Servlet處理客戶端HTTP請求

Servlet處理客戶端HTTP請求

本篇部落格將在上一篇部落格《Servlet第一個示例》的基礎上繼續介紹,Servlet如何處理客戶端的請求,獲得客戶端的請求訊息。

首先我們新建一個靜態頁面index.html,用於向Servlet提交請求。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Servlet</title>
</head>
<body>
    <form action="Test" method="POST">
        <table
>
<tr> <td>使用者編號</td> <td><input type="text" name="userId"></td> </tr> <tr> <td>使用者名稱稱</td> <td><input type="text" name="userName">
</td> </tr> <tr> <td>使用者性別</td> <td><select name="userSex"><option value="1"></option> <option value="2"></option></select></td> </tr
>
<tr> <td>興趣愛好</td> <td><input type="checkbox" name="interest1">興趣1&nbsp;&nbsp;<input type="checkbox" name="interest2">興趣2</td> </tr> <tr> <td colspan="2"><input type="submit"></td> </tr> </table> </form> </body> </html>

然後,我們需要修改Test中的方法,因為頁面採用POST方式提交,所以在Test Servlet中,我們僅對Post的請求做處理。

package com.gujin.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Test extends HttpServlet
{
   private static final long serialVersionUID = 1L;

   protected void doPost(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException
   {
      request.setCharacterEncoding("UTF-8");
      // 獲得請求頭資訊
      System.out.println("======HEAD INFO======");
      Enumeration enumeration = request.getHeaderNames();
      while (enumeration.hasMoreElements())
      {
         String name = (String) enumeration.nextElement();
         Enumeration headers = request.getHeaders(name);
         while (headers.hasMoreElements())
         {
            System.out.println(name + ":" + headers.nextElement());
         }
      }

      // 獲得請求引數
      System.out.println("======REQUEST PARAM======");
      // 獲得指定引數
      // request.getParameter("userId");
      // 獲得所有請求引數名稱
      // request.getParameterNames();
      // 獲得請求引數集合
      // request.getParameterMap();

      Enumeration names = request.getParameterNames();
      while (names.hasMoreElements())
      {
         String name = (String) names.nextElement();
         System.out.println(name + ":" + request.getParameter(name));
      }
      PrintWriter writer = response.getWriter();
      writer.print("Hello Servlet");
      writer.flush();
      writer.close();
   }
}

這裡寫圖片描述

輸入一些內容後提交,我們會看到服務端的後臺會輸出如下內容(內容可能會不一樣):
======HEAD INFO======
accept:image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, a
pplication/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, /
referer:http://localhost/Servlet/
accept-language:zh-CN
user-agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 3.0.04506.30; .NET CLR 2.0
.50727; hwvcloud4; hwcloud4)
content-type:application/x-www-form-urlencoded
accept-encoding:gzip, deflate
host:localhost
content-length:77
connection:Keep-Alive
cache-control:no-cache
======REQUEST PARAM======
userId:jianggujin
userSex:1
interest2:on
userName:蔣固金

這樣我麼就可以根據客戶端請求的內容做相應的業務處理。