1.获取请求参数通用方式:不论get还是post请求方式都可以使用下列方法来获取请求参数

    1.String getParameter(String name):根据参数名称获取参数值  username=zhangsan&password=123

    2.String[] getParameterValues(String name):根据参数名称获取参数值的数组  hobby=xx&hobby=xxxx (多用于复选框)

    3.Enumeration<String> getParameterNames():获取所有请求参数名称

    4.Map<String,String[]> getParameterMap():获取所有参数的Map集合

    中文乱码情况:

        get方式:tomcat 8 已经将get方式乱码问题解决了

        post方式:会乱码

            解决方法:在获取参数前,设置request的编码  request.setCharacterEncoding("utf-8");

定义一个注册页面:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>注册页面</title> 
</head> 
<body> 
    <form action="/Tomcat_servlet/RequestDemo4" method="post"> 
        <input type="text" placeholder="请输入用户名" name="username"><br> 
        <input type="text" placeholder="请输入密码" name="password"><br> 
 
        <input type="checkbox" name="hobby" value="game">游戏 
        <input type="checkbox" name="hobby" value="study">学习 
        <input type="checkbox" name="hobby" value="speak">说话 
        <br> 
 
        <input type="submit" value="提交"> 
    </form> 
</body> 
</html>

定义一个类:

package cn.itcast.web.request; 
 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.util.Enumeration; 
import java.util.Map; 
import java.util.Set; 
 
@WebServlet("/RequestDemo4") 
public class RequestDemo4 extends HttpServlet { 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {         
        //1.设置流的编码 
        request.setCharacterEncoding("utf-8"); 
 
        //post 获取请求参数 
 
        //根据参数名称获取参数值的数组 
        String username = request.getParameter("username"); 
        /*System.out.println("post"); 
        System.out.println(username);*/ 
 
        //根据参数名称获取参数值的数组 
        String[] hobbies = request.getParameterValues("hobby"); 
        /*for (String hobby : hobbies) { 
            System.out.println(hobby); 
        }*/ 
 
        //获取所有请求参数名称 
        Enumeration<String> parameterNames = request.getParameterNames(); 
        /*while (parameterNames.hasMoreElements()){ 
            String name = parameterNames.nextElement(); 
            System.out.println(name); 
            String value = request.getParameter(name); 
            System.out.println(value); 
            System.out.println("----------"); 
        }*/ 
 
        //获取所有参数的Map集合 
        Map<String, String[]> parameterMap = request.getParameterMap(); 
        //遍历 
        Set<String> keySet = parameterMap.keySet(); 
        for (String name : keySet) { 
            //根据键获取值 
            String[] values = parameterMap.get(name); 
            System.out.println(name); 
            for (String value : values) { 
                System.out.println(value); 
            } 
            System.out.println("----------"); 
        } 
    } 
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        //get 获取请求参数 
 
        //根据参数名称获取参数值 
        /*String username = request.getParameter("username"); 
        System.out.println("get"); 
        System.out.println(username);*/ 
        this.doPost(request,response); 
    } 
} 

2.请求转发:一种在服务器内部的资源跳转方式

    1.步骤:

        1.通过request对象获取请求转发的对象:RequestDispatcher getRequestDispatcher(String path)

        2.通过RequestDispatcher对象来进行转发: forward(request,response);

    2.特点:

        1.浏览器地址栏路径不发生变化

        2.只能转发到当前服务器内部资源中

        3.转发是一次请求

定义一个类:

package cn.itcast.web.request; 
 
import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.io.IOException; 
 
@WebServlet("/RequestDemo6") 
public class RequestDemo6 extends HttpServlet { 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        System.out.println("Demo666被访问了...."); 
        //转发到demo7中 
 
       /* RequestDispatcher requestDispatcher = request.getRequestDispatcher("/RequestDemo7"); 
        requestDispatcher.forward(request,response);*/ 
 
       request.getRequestDispatcher("/RequestDemo7").forward(request,response); 
    } 
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
 
        this.doPost(request,response); 
    } 
} 

定义另一个类:

package cn.itcast.web.request; 
 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.io.IOException; 
 
@WebServlet("/RequestDemo7") 
public class RequestDemo7 extends HttpServlet { 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        System.out.println("Demo777被访问了...."); 
} 
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
 
        this.doPost(request,response); 
    } 
} 

3.共享数据

    域对象:一个有作用范围的对象,可以在范围内共享数据

    request域:代表一次的请求范围,一般用于请求转发的多个资源中共享数据

    方法:

        1.void setAttribute(String name,Object obj):存储数据

        2.Object getAttribute(String name):通过键获取值

        3.void removeAttribute(String name):通过键移除键值对

定义一个类:

package cn.itcast.web.request; 
 
import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.io.IOException; 
 
@WebServlet("/RequestDemo6") 
public class RequestDemo6 extends HttpServlet { 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        System.out.println("Demo666被访问了...."); 
        //转发到demo7中 
         
       //储存数据到request域中 
        request.setAttribute("msg","hello"); 
 
       request.getRequestDispatcher("/RequestDemo7").forward(request,response); 
    } 
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
 
        this.doPost(request,response); 
    } 
} 

定义另一个类:

package cn.itcast.web.request; 
 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.io.IOException; 
 
@WebServlet("/RequestDemo7") 
public class RequestDemo7 extends HttpServlet { 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        //获取数据 
        Object msg = request.getAttribute("msg"); 
        System.out.println(msg); 
 
        System.out.println("Demo777被访问了...."); 
} 
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
 
        this.doPost(request,response); 
    } 
} 

4.获取ServletContext

    ServletContext getServletContext()

定义一个类:

package cn.itcast.web.request; 
 
import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.io.IOException; 
 
@WebServlet("/RequestDemo8") 
public class RequestDemo8 extends HttpServlet { 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
 
        ServletContext servletContext = request.getServletContext(); 
        System.out.println(servletContext); 
    } 
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
 
        this.doPost(request,response); 
    } 
} 

发布评论
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

Java>Request原理及获取请求数据的方法讲解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。