欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java HttpServlet 之 HttpServletRequest请求

程序员文章站 2024-02-28 12:45:52
...

1.获取方法 post 或者 get 或者其他

String method = req.getMethod();
resp.getWriter().write("<h3>1.请求方法:</h3>"+method);

2.获取请求参数

        Enumeration<String> parameterNames = req.getParameterNames();
        resp.getWriter().write("<h5>2.请求参数:</h5>");
        if(!parameterNames.hasMoreElements())
        {
            resp.getWriter().write("无 <br />");
        }
        while (parameterNames.hasMoreElements())
        {
            String name = parameterNames.nextElement();
            String[] vals = req.getParameterValues(name);
            resp.getWriter().write(name+":");
            for (String val: vals) {
                resp.getWriter().write(val+" ");
            }
            resp.getWriter().write("<br />");

        }
        resp.getWriter().write("或者 <br />");
        Map<String,String[]> stringMap=req.getParameterMap();
        for (Map.Entry<String,String[]> entry:stringMap.entrySet())
        {
            resp.getWriter().write(entry.getKey()+":");
            for(String str:entry.getValue())
            {
                resp.getWriter().write(str+" ");
            }
            resp.getWriter().write("<br />");
        }


3.获取请求头

 
        Enumeration<String> headerNames = req.getHeaderNames();
        resp.getWriter().write("<h5>3.请求头:</h5>");
        if(!headerNames.hasMoreElements())
        {
            resp.getWriter().write("无 <br />");
        }

        while(headerNames.hasMoreElements())
        {
            String name=headerNames.nextElement();
            String val=req.getHeader(name);
            resp.getWriter().write(name+":"+val+"<br />");
        }


4.获取请求资源

        resp.getWriter().write("<h5>4.请求资源:</h5>");
        String requestURI = req.getRequestURI();
        resp.getWriter().write("requestURI"+":"+requestURI+"<br />");
        StringBuffer requestURL = req.getRequestURL();
        resp.getWriter().write("requestURL"+":"+requestURL+"<br />");
        String contextPath = req.getContextPath();
        resp.getWriter().write("contextPath(web应用名称)"+":"+contextPath+"<br />");
        String queryString = req.getQueryString();
        resp.getWriter().write("queryString(GET后的参数列表)"+":"+queryString+"<br />");



5.获取客户机的参数

 
        resp.getWriter().write("<h5>5.客户端参数:</h5>");
        String remoteUser = req.getRemoteUser();
        resp.getWriter().write("remoteUser"+":"+remoteUser+"<br />");
        String remoteHost = req.getRemoteHost();
        resp.getWriter().write("remoteHost"+":"+remoteHost+"<br />");
        String remoteAddr = req.getRemoteAddr();
        resp.getWriter().write("remoteAddr"+":"+remoteAddr+"<br />");
        int remotePort = req.getRemotePort();
        resp.getWriter().write("remotePort"+":"+remotePort+"<br />");



6.获取request域对象

        //设置域对象 (一次请求)
        req.setAttribute("姓名",new String("神奇的我"));
        req.setAttribute("数字",new Integer(23));
        req.setAttribute("时间",new Date());
        //输出域对象
        resp.getWriter().write("<h5>6.request域对象(存储数据的区域对象):</h5>");
        Enumeration<String> attributeNames = req.getAttributeNames();
        while (attributeNames.hasMoreElements())
        {
            String name=attributeNames.nextElement();
            Object val = req.getAttribute(name);
            resp.getWriter().write(name+":"+val.toString()+"<br />");
        }
        //移除域对象
        /* 先不移除 看看 在转发之后 还能不能获取到
        Enumeration<String> attributeNames2 = req.getAttributeNames();
        while (attributeNames2.hasMoreElements())
        {
            String name=attributeNames2.nextElement();
            req.removeAttribute(name);
        }
        */


7.request请求转发

        //获取转发
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/demo2");
        //执行
        //include方法 包含在原来地方
        //requestDispatcher.include(req,resp);
        //forward方法 重新开
        requestDispatcher.forward(req,resp);


测试工程war包:百度云:https://pan.baidu.com/s/1pL9PQ6B  密码:atkv