Request

一、试一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.saxon.servlet;

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

public class requestDemo extends HttpServlet {
@Override
protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter ("username");
String password = req.getParameter ("password");
String path = this.getServletContext ().getContextPath ();
resp.sendRedirect (path+"/success.html");//选择重定向的去处
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<html>
<head>
<meta charset="UTF-8">

<title>test</title>
</head>
<body>
<h2>Hello World!</h2>
<%-- pageContext.request.contextPath 当前目录/url pattern --%>
<form action="${pageContext.request.contextPath}/re" method="post">
<p><label>username:<input type="text" name="username"></label></p>
<p><label>password:<input type="password" name="password"></label></p>
<input type="submit">
</form>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>successful</h1>
</body>
</html>

通过代码实现密码输入,成功跳转成功。

二、获得前端信息

1.接收前端信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.saxon.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;

//ISO_8859_1 默认编码
public class requestDemo extends HttpServlet {
@Override
protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding ("utf-8");//解决乱码问题
String username = req.getParameter ("username");
String[] hobbies = req.getParameterValues ("hobbies");
System.out.println (Arrays.toString (hobbies));
String path = this.getServletContext ().getContextPath ();
resp.sendRedirect (path + "/new.html");//302
//req.getRequestDispatcher ("/sucess.jsp").forward (req,resp);
resp.setCharacterEncoding ("utf-8");
}

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

}

2.提交页

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ page contentType="text/html;charset=UTF-8" language="java" %><%解决界面乱码问题%>
<html>
<body>
<h2>Hello World!</h2>
<%--pageContext.request.contextPath 当前目录/url pattern--%>
<form action="${pageContext.request.contextPath}/re" method="post" >
<p><label>username:<input type="text" name="username"></label></p>
<p><label>password:<input type="password" name="password"></label></p>
<p>爱好:
<input type="checkbox" name="hobbies" value="篮球" required>篮球
<input type="checkbox" name="hobbies" value="游戏">游戏
<input type="checkbox" name="hobbies" value="休息">休息
<input type="checkbox" name="hobbies" value="唱歌">唱歌
</p>
<input type="submit">
</form>
</body>
</html>

3.成功反馈,跳出这个界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%--
Created by IntelliJ IDEA.
User: saxon
Date: 2020/7/26
Time: 20:44
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<h1>successful</h1>
</body>
</html>

关于乱码问题:

1.首先是Tomcat 的问题,如果在界面加载控制台出现乱码,就改logging.properties(java.util.logging.ConsoleHandler.encoding = GBK)最后一个有关编码的UTF-8改成GBK;

2.如果是接收的时候出现数据的乱码,就用request.setCharacterEncoding (“utf-8”);就可以了

3.如果实在不行就上网上搜,我是被这个搞崩溃了。然后在改一下配置,花了好久,在我准备卸载的时候,她就好了;