2.1 HttpServletRequest类

您的 servlet 必须根据从请求中收到的信息来完成大部分工作。该对象负责它HttpServletRequest,容器将传递给您的 servlet(传递给一个service或多个方法doGet()doPost()

这个对象有很多方法,因为它只是存储请求数据,你可以通过它与容器进行交互。

方法可分为两大类:

  • 用户授权相关方法
  • 使用查询数据的方法

用户授权方式我会以表格的形式给出,我们就不分析了。事实上,它们很少用于授权用户。所有流行的框架都使用自己的、更高级的授权方法。

我应该列出它们,但同样,我还没有看到有人使用它们。

方法 描述
1个 authenticate(HttpServletResponse) 执行响应认证
2个 changeSessionId() 强制更改会话 ID
3个 getAuthType() 返回使用的身份验证类型:ASIC_AUTH、FORM_AUTH、CLIENT_CERT_AUTH、DIGEST_AUTH
4个 getRemoteUser() 返回用户登录
5个 getRequestedSessionId() 返回客户端的 SessionID
6个 getSession() 返回一个 HttpSession 对象
7 getUserPrincipal() 返回一个 java.security.Principal 对象
8个 login(username, password) 执行用户登录
9 logout() 注销用户会话

2.2 请求数据

第二组方法更有趣。我们在请求中有什么样的数据?

  • http方法
  • 网址
  • 选项
  • 标题

让我们看看有哪些方法可用于处理它们:

方法 描述
1个 getMethod() 返回 HTTP 方法:GET、POST、DELETE、...
2个 getRequestURI() 返回请求 URI: http: //codegym.cc/my/data
3个 getRequestURL() 返回请求 URL: http: //codegym.cc/my/data
4个 getQueryString() 返回查询,即 ? 之后的所有内容
5个 getParameterMap() 返回查询参数列表
6个 getParameter(String name) 通过名称返回参数的值
7 getContentType() 返回 MimeType 请求正文
8个 getReader() 阅读器以文本形式阅读请求正文
9 getInputStream() InputStream 将请求体读取为 byte[]
10 getSession() 返回一个 HttpSession 对象
十一 getCookies() 返回一个 Cookie[] 对象数组
12 getHeaderNames() 返回标题列表,仅名称
13 getHeader(String name) 按名称返回标头值
14 getServletPath() 返回引用 servlet 的 URL 部分
15 getContextPath() 返回指定请求内容的 URI 部分

这甚至不是所有的方法......

虽然在我们研究了 HTTP 协议并学习了如何使用 HttpClient 之后,这里的一切都或多或少是熟悉的,不是吗?

让我们编写一个可以向其传递文本和颜色的 servlet,它将返回一个 HTML 页面,其中包含以指定颜色书写的文本。你觉得这个主意怎么样?

让我们从编写我们的 servlet 开始:


public class ColorTextServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
          //write your code here
    }
}

现在我们需要获取用户从 URI 传递的文本和颜色:


public class ColorTextServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {

        // Getting the parameter “text” and “color” from request
        String text= request.getParameter("text");
        String color = request.getParameter("color");
 
    }
}

最后,您需要将文本输出为 HTML。我们将在下一讲中介绍这一点,但在这里我会给出一点提示:

public class ColorTextServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {

        // Get the "text" and "color" parameters from the request
        String text = request.getParameter("text");
        String color = request.getParameter("color");


        // Print the HTML as a response to the browser
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out =  response.getWriter();
        try {
            out.println("<html>");
            out.println("<head> <title> ColorTextServlet </title> </head>");
            out.println("<body>");
            out.println("<h1 style="color:"+color+">"+text+"</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }
}