首页 /  技术专区  /  Servlet 宽屏模式 >

Request转发 & 接收前台传输参数

HttpServletRequest

HttpServletRequest代表客户端的请求,用户通过Http协议访问服务器,HTTP请求中的所有信息会被封装到HttpServletRequest,通过这个HttpServletRequest的方法,获得客户端的所有信息;|


获取前端请求的参数、请求转发

1.新建一个web项目,导入jsp和servlet的依赖。

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.3</version>
    <scope>provided</scope>
</dependency>

2.修改index.jsp页面,新增登录成功页面。

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>

    <h1>登录</h1>
    
    <div>
        <form action = "${pageContext.request.contextPath}/login" method = "post">
            用户名:<input type = "text" name = "userName" /> </br>
            密码:<input type = "password" name = "password" /> </br>
            爱好:
            <input type = "checkbox" name = "hobbies" value = "女孩">女孩
            <input type = "checkbox" name = "hobbies" value = "代码">代码
            <input type = "checkbox" name = "hobbies" value = "唱歌">唱歌
            <input type = "checkbox" name = "hobbies" value = "电影">电影
            
            </br>
            <input type = "submit">
        </form>
    </div>
</body>
</html>

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录成功</title>
</head>
<body>
    <h1>登录成功</h1>
</body>
</html>

3.新增LoginServlet类,并在web.xml里面注册配置

LoginServlet

package com.allen.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

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("hobbies");

        System.out.println(userName);
        System.out.println(password);
        System.out.println(Arrays.toString(hobbies));

        //请求转发
        req.getRequestDispatcher("/success.jsp").forward(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true">

  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.allen.servlet.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>

</web-app>

4.启动tomcat测试

输入参数,点击提交:

image.png

页面成功跳转:

image.png

接下来我们看一下后台有没有接收到数据:

image.png



上一篇: Response重定向
下一篇: Cookie
头像
0/200
图片验证码