文章目录
Writing Service Methods
Getting Information from Requests
A request contains data passed between a client and the servlet. All requests implement the ServletRequest
interface. This interface defines methods for accessing the following information:
- Parameters, which are typically used to convey information between clients and servlets
- Object-valued attributes, which are typically used to pass information between the web container and a servlet or between collaborating servlets
- Information about the protocol used to communicate the request and about the client and server involved in the request
- Information relevant to localization
You can also retrieve an input stream from the request and manually parse the data. To read character data, use the BufferedReader
object returned by the request’s getReader
method. To read binary data, use the ServletInputStream
returned by getInputStream
.
实践
环境
操作系统:
Windows 10 x64
集成开发环境:
Eclipse IDE for Enterprise Java and Web Developers (includes Incubating components)
Version: 2021-09 (4.21.0)
Build id: 20210910-1417
服务器:
apache-tomcat-9.0.55
新建项目
新建 Dynamic Web Project:
从 GET 请求中获取用户提交的参数
新建一个 HelloServlet
类,继承自 HttpServlet
:
- 使用
@WebServlet
注解,指定urlPatterns
属性; - 重写
doGet
方法,用于处理GET
请求:- 使用
getParameter
方法可以获取单值的属性; - 使用
getParameterValues
方法可以获取多值的属性。
- 使用
package com.mk.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = "/hello")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getMethod();
System.out.println("Request Method: " + method);
String queryString = request.getQueryString();
System.out.println("Query String: " + queryString);
String username = request.getParameter("username");
System.out.println("Username: " + username);
String[] courses = request.getParameterValues("course");
if(courses != null) {
for (String course : courses) {
System.out.println("Course: " + course);
}
}
}
}
src/main/webapp/index.html 文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>首页</title>
</head>
<body>
<!-- action="hello" 对应 HelloServlet 类上的注解 @WebServlet 的属性 urlPatterns = "/hello" -->
<!-- method="get" 是指以 GET 请求方式提交用户数据 -->
<form action="hello" method="get">
<input type="text" name="username" placeholder="用户名" />
<br>
<input type="checkbox" name="course" id="chinese" value="chinese" />
<label for="chinese">语文</label>
<br>
<input type="checkbox" name="course" id="math" value="math" />
<label for="math">数学</label>
<br>
<input type="checkbox" name="course" id="english" value="english" />
<label for="english">英语</label>
<br>
<input type="submit" value="提交" />
</form>
</body>
</html>
测试
启动 Apache Tomcat 服务器,服务器启动成功之后,访问 http://localhost:8080/hello-servlet/index.html,填写表单信息,然后点击提交按钮,提交成功之后,浏览器会跳转到 http://localhost:8080/hello-servlet/hello?username=mk&course=chinese&course=math,控制台输出:
注意:浏览器是否会跳转到 http://localhost:8080/hello-servlet/hello?username=mk&course=chinese&course=math,取决于提交的表单信息。
从 POST 请求中获取用户提交的参数
如果你的表单是以 POST 请求方式提交用户数据(第 9 行):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>首页</title>
</head>
<body>
<!-- method="post" 是指以 POST 请求方式提交用户数据 -->
<form action="hello" method="post">
...
</form>
</body>
</html>
那么,你应该在 Servlet 中重写 doPost
方法。否则,当你提交表单信息时,Apache Tomcat 服务器将返回:
提示:关于 405 响应代码,参考 405 Method Not Allowed。
重写 doPost
方法,用于处理 POST
请求(第 21 ~ 40 行):
package com.mk.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = "/hello")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ...
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getMethod();
System.out.println("Request Method: " + method);
String queryString = request.getQueryString();
System.out.println("Query String: " + queryString);
String username = request.getParameter("username");
System.out.println("Username: " + username);
String[] courses = request.getParameterValues("course");
if(courses != null) {
for (String course : courses) {
System.out.println("Course: " + course);
}
}
}
}
测试
重启 Apache Tomcat 服务器,然后访问 http://localhost:8080/hello-servlet/index.html,填写表单信息并提交,观察控制台输出: