Servlet5 - request

HttpServletRequest对象代表客户端的请求


Servlet5 - request_第1张图片
HTTP协议之请求
Servlet5 - request_第2张图片
继承关系

通过Request对象进行的常用操作

  • 获取客户机信息
  • 获取请求头信息
  • 获取请求参数
  • 利用请求域传递对象(request域对象)
  • 重定向和转发的区别
获取客户机信息
Servlet5 - request_第3张图片
获取客户机信息

获取请求头信息

Servlet5 - request_第4张图片
获取请求头信息
  • referer 网页来源
  • user-agent 浏览器类型
    • MSIE IE浏览器
    • Firefox 火狐浏览器
    • Chrome google浏览器
/**
 * 获取客户机的内容 和请求头内容
 * @author limaoquan
 *
 */
public class RequestServlet1 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取IP
        String ip = request.getRemoteAddr();
        String method = request.getMethod();
        String path = request.getContextPath();
        
        System.out.println("IP地址" + ip);
        System.out.println("虚拟路径是 "+ path);
        System.out.println("请求方式"+ method);
        
        //获取请求头
        String referer=request.getHeader("referer");//网页来源(防盗链)
        String agent = request.getHeader("user-agent");//判断浏览器(文件下载)
        //遍历所有请求头
        Enumeration names = request.getHeaderNames();
        while(names.hasMoreElements()){
            String name = names.nextElement();
            System.out.println(name + ":" + request.getHeader(name));
        }
        System.out.println("------------");
        System.out.println("您使用浏览器:"+ request.getHeader("user-agent"));
        
        //判断referer是否存在和有效
        if(referer!=null&&referer.startsWith("http://localhost/day")){
            //不属于盗链
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().println("机密信息");
        }else{
            //盗链
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().println("您的请求盗链");
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

获取请求参数(重要)

Servlet5 - request_第5张图片
获取请求参数


Insert title here



输入姓名
输入密码
选择性别
选择爱好 篮球 足球 排球
选择城市
Servlet5 - request_第6张图片
html界面
public class RegServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /**
         * request获取中文的乱码
         *  post请求
         *      setCharacterEncoding(String env) 设置request缓冲区编码
         *  
         *  get请求
         *      
         */
        //设置request缓冲区编码
        request.setCharacterEncoding("UTF-8");
        //获取内容,做其他操作
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        
        //获取爱好,有过个值
        String[] loves = request.getParameterValues("love");
        String city = request.getParameter("city");
        
        System.out.println("用户名:" + username);
        System.out.println("密码:" + password);
        System.out.println("性别:" + sex);
        System.out.println("爱好:" + Arrays.toString(loves));
        System.out.println("城市:" + city);
        System.out.println("==========================");
        //获取map集合
        Map map = request.getParameterMap();
        //循环遍历
        Set keys = map.keySet();
        for(String key:keys){
            String[] values = map.get(key);
            System.out.println(Arrays.toString(values));
        }
        
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
乱码问题解决
  • POST请求乱码 :
    request.setCharacterEncoding("utf-8");
  • GET请求乱码
  • 解决方案一:修改server.xml (尽力不要用方案一)

    必须有修改tomcat服务器配置文件权限

  • 解决方案二:逆向编解码(推荐用)
    username = URLEncoder.encode(username, "ISO8859-1");
    username = URLDecoder.decode(username, "utf-8");
    简化
    username = new String(username.getBytes("ISO8859-1"),"utf-8");

重定向和转发

域对象 ServletContext 与 request 的区别:

  • ServletContext: 服务器启动,为每个web应用只创建一个ServletContext对象,所有应用共享
  • request 只有一次请求的范围

两者方法相似:

  • setAttribute();
  • getAttribute();
  • removeAttribute();
public class RequestServlet2 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //request域设置内容
        request.setAttribute("name", "Mango");
        
        //转发(路径服务器端的绝对路径) 转发可以共享request域
        request.getRequestDispatcher("/request3").forward(request, response);
        
        //完成重定向(客户端路径) 重定向无法共享request域
        //response.sendRedirect("/day10/request3");
                
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}




public class RequestServlet3 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取
        String value = (String)request.getAttribute("name");
        
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("访问到 了 3 " + value);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}
Servlet5 - request_第7张图片
转发和重定向的区别

转发

  RequestDispatcher rd = request.getRequestDispatcher("/request3");
  rd.forward(request, response);

你可能感兴趣的:(Servlet5 - request)