• HttpServletRequest的使用


      当HTTP转发给Web容器处理时,Web容器会收集相关信息,并产生HttpServletRequest对象,使用这个对象可以取得所有HTTP请求中的信息,可以在Servlet中进行处理,也可以转发给其他的Servlet/Jsp处理。

    1. 请求信息的取得

      getQueryString()方法可以取得HTTP请求的查询字符串。
      getParameter()方法能指定请求参数名称来取得相应的值,如取得下面请求参数为name 的值:

    http://localhost:8080/WebApp1/hello.do?name=acmagic;

             获取到的name 的值就为acmagic,如果请求中没有参数的名称,则返回null.
       如果窗口上一个参数名称有多个值,此时可以用getParameterValues()方法取得一个String数组。例如:

    http://localhost:8080/WebApp1/hello.do?choose=sans&choose=lily&choose=jack
    String[] names = request.getParameterValues("choose");


             如果要知道有多少请求参数,可以使用getParameterNames()方法,他会返回一个Enumeration对象,其中包括所有请求参数的名称。例如:


    Enumeration<String> e = request.getParameterNames();
    while (e.hasMoreElements()) {
        String param = (String) e.nextElement();
        out.println(param + "<br>");
    }



            如果要知道浏览器请求的标头,可以用getHeader()方法,也可以使用getHeaderNames()获取浏览器请求的所有标头值。  
           完整代码:

    package com.web.http;

    import java.awt.print.Printable;
    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 GetRequestMessage extends HttpServlet {

        /**
         *
         */
        private static final long serialVersionUID = 1L;

        protected void getRequestMessage(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            // 传送给浏览器utf-8编码的文字
            response.setContentType("text/html;charset=utf-8");
            //请求字符编码,如果有中文才不会乱码
            request.setCharacterEncoding("utf-8");
            PrintWriter output = response.getWriter();
            output.println("<html>");
            output.println("<head>");
            output.println("<title>getRequestMessage</title>");
            output.println("</head>");
            output.println("<body>");
            // 取得应用程序的路径
            output.println("<h1>This WebApp at :" + request.getContextPath() + "</h1>");
            // 取得请求字符串
            output.println("<p>Request string: " + request.getQueryString() + "</p>");
            // 取得参数中对应的值
            output.println("<p>name = " + request.getParameter("name") + "</p>");
            // 一个参数对应几个值
            String[] chooses = request.getParameterValues("choose");
            output.print("<p>chooses are: " + chooses[1] + "</p>");
            // 请求参数的个数
            Enumeration<String> e = request.getParameterNames();
            while (e.hasMoreElements()) {
                String param = (String) e.nextElement();
                output.println(param + ":" + request.getParameter(param) + "<br><br>");
            }

            // 获取所有标头名
            Enumeration<String> h = request.getHeaderNames();
            while (h.hasMoreElements()) {
                String head = (String) h.nextElement();
                output.println(head + ": " + request.getHeader(head) + "<br>");
            }

            output.println("<body>");
            output.println("</html?");
            output.close();
        }

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            getRequestMessage(request, response);
        }

        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            getRequestMessage(request, response);
        }

    }</font></font>



    运行结果如下:



    (未完。。。)

    更多相关API请访问官方文档:http://docs.oracle.com/javaee/6/api/

     

  • 相关阅读:
    鼠标移入和鼠标移出的提示,和样式的转换
    HTML5——新特性,拖放
    关于订阅发布模式
    titanium环境配置
    Matlab与C混编的介绍
    一个相对健壮的node 静态http服务器
    阻赛非阻塞同步异步
    最近在做的事以及一些安排
    说一说js中__proto__和prototype以及原型继承的那些事
    PHP写的爬虫,爬指定网站页面上的各种图片
  • 原文地址:https://www.cnblogs.com/A0926/p/5968593.html
Copyright © 2020-2023  润新知