获取前端传递的参数和请求转发

HttpServletRequest(请求)

1、获取前端传递的参数和请求转发

req.getParameter();
req.getParameterValues();
public class LoginServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String[] hobbies = req.getParameterValues("hobby");
        //通过请求转发
        req.getRequestDispatcher("success.jsp").forward(req,resp);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    登录


登录

用户名:
密码:
爱好: 女孩 代码 唱歌 电影
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


登录成功

  <servlet>
    <servlet-name>loginservlet-name>
    <servlet-class>com.maple.servlet.LoginServletservlet-class>
  servlet>
  <servlet-mapping>
    <servlet-name>loginservlet-name>
    <url-pattern>/loginurl-pattern>
  servlet-mapping>

你可能感兴趣的:(获取前端传递的参数和请求转发)