HttpServletRequest
HttpServletRequest代表客户端的请求,用户通过Http协议访问服务器,HTTP请求中的所有信息会被封装到HttpServletRequest,通过这个HttpServletRequest的方法,获得客户端的所有信息。
获取前端传递的参数,请求转发
String getParameter(String var1);
String[] getParameterValues(String var1);
LoginServlet.java:
1 package com.liyu.servlet; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.http.HttpServlet; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import java.io.IOException; 8 import java.util.Arrays; 9 10 public class LoginServlet extends HttpServlet { 11 @Override 12 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 13 req.setCharacterEncoding("utf-8"); 14 resp.setCharacterEncoding("utf-8"); 15 16 String username = req.getParameter("username"); 17 String password = req.getParameter("password"); 18 String[] hobbies= req.getParameterValues("hobbies"); 19 20 System.out.println("=================================="); 21 //后台接受中文乱码问题 22 System.out.println(username); 23 System.out.println(password); 24 System.out.println(Arrays.toString(hobbies)); 25 System.out.println("=================================="); 26 27 //通过请求转发 28 req.getRequestDispatcher("/success.jsp").forward(req,resp); 29 } 30 31 @Override 32 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 33 doGet(req,resp); 34 } 35 }
index.jsp:
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 <html> 3 <head> 4 <title>登录</title> 5 </head> 6 <body> 7 8 <h1>登录</h1> 9 10 <div style="text-align: center"> 11 <%--这里表单表示的意思是以post的方式提交表单,提交到我们的login请求--%> 12 <form action="${pageContext.request.contextPath}/login" method="post"> 13 用户名:<input type="text" name="username"><br> 14 密码:<input type="password" name="password"><br> 15 爱好: 16 <input type="checkbox" name="hobbies" value="女孩">女孩 17 <input type="checkbox" name="hobbies" value="代码">代码 18 <input type="checkbox" name="hobbies" value="唱歌">唱歌 19 <input type="checkbox" name="hobbies" value="电影">电影 20 <br> 21 <input type="submit"> 22 </form> 23 </div> 24 </body> 25 </html>
Success.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>成功</title>
</head>
<body>
<h1>
登录成功
</h1>
</body>
</html>
w
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 5 http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 6 version="4.0" 7 metadata-complete="true"> 8 <display-name>Archetype Created Web Application</display-name> 9 10 <servlet> 11 <servlet-name>LoginServlet</servlet-name> 12 <servlet-class>com.liyu.servlet.LoginServlet</servlet-class> 13 </servlet> 14 15 <servlet-mapping> 16 <servlet-name>LoginServlet</servlet-name> 17 <url-pattern>/login</url-pattern> 18 </servlet-mapping> 19 </web-app>
eb.xml:
运行测试:
重定向和请求转发的区别:
相同点:页面都会实现跳转
不同点:请求转发 url不会产生变化 307
重定向 url会发生变化 302