目录
核心方法 | 说明 |
init | 首次创建出HttpServlet实例会调用一次 |
destroy | 在HttpServlet实例不使用时调用一次 |
service | 收到HTTP请求的时候调用头一次 |
doGet | 收到GET请求的时候service调用doGet |
doPost | 收到POST请求的时候service来调用 |
doPut / doDelete | 收到这些请求的时候service来调用 |
Tomcat的关闭: 1.杀进程, 类似于拔电源 比如点击idea中的红色方框、cmd直接点×、任务管理器结束任务...... 2.通过8005端口给Tomcat发送一个关闭操作, 类似于走正常的程序关闭电脑 这个时候Tomcat就会正常关机就可以调用到destroy方法
初始阶段实例化的时候,调用一次 init 结束销毁之前,调用一次 destroy 每次收到请求,调用 service
方法 | 说明 |
String getProtocol() | 返回请求协议的名称和版本号 |
String getMethod() | 返回请求的HTTP方法 |
String getPrequestURI() | 返回URL中的一部分 |
String getContextPath() | 返回ContextPath |
String getQueryString() | 返回查询字符串 |
Enumeration getParameterNames() | |
String getParameter(String name) | 返回请求参数的值,参数不存在返回null |
String[ ] getParameterValues(String name) | 返回请求参数的所有值 |
Enumeration getHeaderNames() | 返回包含请求中所有header的key的枚举 |
String getCharacterEncoding() | 返回请求中使用的字符编码 |
String getContentType() | 返回请求中使用的文本类型和字符编码 |
int getContentLength() | 返回请求主体的长度,字节为单位 |
String getHeader(String name) | 返回指定请求头的值 |
InputStream getInputStream() | 用来读取query string和body的内容 |
代码如下:
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.util.Enumeration;
-
- @WebServlet("/showRequest")
- public class showRequest extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //resp是响应的对象,setContentType给响应的ContentType设置了值html
- //声明响应body是html结构的数据
- resp.setContentType("text/html");
-
- StringBuilder stringBuilder = new StringBuilder();
-
- //getProtocol返回请求协议的版本
- stringBuilder.append(req.getProtocol());
- //要将\n 替换成
否则html无法识别 - // stringBuilder.append("\n");
- stringBuilder.append("
"); -
- //getMethod返回请求中http方法的名称
- stringBuilder.append(req.getMethod());
- stringBuilder.append("
"); -
- //getRequestURI返回请求的uri
- stringBuilder.append(req.getRequestURI());
- stringBuilder.append("
"); -
- //getRequeatURL返回请求的url
- stringBuilder.append(req.getRequestURL());
- stringBuilder.append("
"); -
- //返回Context Path
- stringBuilder.append(req.getContextPath());
- stringBuilder.append("
"); -
- //返回查询字符串
- stringBuilder.append(req.getQueryString());
- stringBuilder.append("
"); -
-
- //拼接header中的内容
- //getHeaderNames返回一个包含请求中所有的键值对key的值
- Enumeration
headerNames = req.getHeaderNames(); - while(headerNames.hasMoreElements()){
- String name = headerNames.nextElement();
- //getHeader以字符串的形式返回key对应的value值
- String value = req.getHeader(name);
- stringBuilder.append(name+ ": " + value);
- stringBuilder.append("
"); - }
-
- resp.getWriter().write(stringBuilder.toString());
- }
- }
代码如下:
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- @WebServlet("/getParameter")
- public class getParameterServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //获取query string中的键值对
- //取值的时候,如果键值对不存在返回的是 null
- //如果键存在,值不存在那么返回 “ ”
- String studentId = req.getParameter("studentId");
- String studentName = req.getParameter("studentName");
-
- System.out.println(studentId);
- System.out.println(studentName);
-
- //如果不加这行代码,浏览器就会显示出 ???,这里是 = ,不能是 -
- resp.setContentType("text/html; charset=utf8");
-
- resp.getWriter().write(studentId + " , " + studentName);
- }
- }
分为俩种情况!
请求这里使用这个方法: req.setCharacterEncoding("utf8"); 响应建议使用第一种, 因为不止要设置字符集还要设置格式 resp.setContentType("text/html; charset = utf8"); //resp.setCharacterEncoding("utf8");
后端代码如下:
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- @WebServlet("/getParameter")
- public class getParameterServlet extends HttpServlet {
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //如果不加上这段代码,req就会随机解析,因此页面上显示的就是乱码
- req.setCharacterEncoding("utf8");
-
- String studentId = req.getParameter("studentId");
- String studentName = req.getParameter("studentName");
-
- System.out.println(studentId);
- System.out.println(studentName);
-
- resp.setContentType("text/html; charset = utf8");
- resp.getWriter().write(studentId + ", " + studentName);
- }
- }
后端代码如下:
- import com.fasterxml.jackson.databind.ObjectMapper;
-
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
-
- class Student{
- //1.这个student类的属性必须是public或者带有 public 的getter 和 setter
- // 否则jackson无法访问这个对象的属性
- //2.这个类必须要有无参版本的构造方法
- public int studentId;
- public String studentName;
- public String studentSex;
- }
-
- @WebServlet("/json")
- public class JsonServlet extends HttpServlet {
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //此处假设请求中的body是json格式的
- //{studentId:66,studentName:"王大锤"}
- //jackson 一个类ObjectMapper 和俩个方法
- //一个方法是readValue,把json格式的数据转换成 java 对象
- //另外一个方法是writeValueAsString,把java对象转换成json格式的字符串
-
- ObjectMapper objectMapper = new ObjectMapper();
- //readValue的第一个参数可以是字符串,也可以是输入流
- //第二个参数是类的对象
- Student s = objectMapper.readValue(req.getInputStream(),Student.class);
- System.out.println(s.studentId);
- System.out.println(s.studentName);
- System.out.println(s.studentSex);
-
- // resp.setContentType("text/html; charset = utf8");
- // resp.getWriter().write(s.studentId + ", " + s.studentName +", " + s.studentSex);
-
- // //响应的数据是html格式的
- // resp.setContentType("text/html; charset = utf8");
- // resp.getWriter().write(objectMapper.writeValueAsString(s));
-
-
- //响应的数据是json格式的
- resp.setContentType("application/json; charset = utf8");
- resp.getWriter().write(objectMapper.writeValueAsString(s));
-
- }
- }
方法 | 说明 |
void setStatus() | 设置状态码 |
void setHeader(String name,String value) | 设置一个header字段,如果已经存在,会覆盖 |
void addHeader(String name,String value) | 设置一个header字段,如果存在,会再创建一个 |
void setContentType() | 设置发送给客户端的响应的内容类型+字符编码 |
void setCharacterEncoding() | 设置发送给客户端的响应的字符编码 |
void sendRedirect(String location) | 重定向 |
PrintWriter getWriter() | 用于往body中写入文本格式数据 |
OutputStream getOutputStream() | 用于往body中写入二进制数据 |
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- @WebServlet("/status")
- public class StatusServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //约定一下浏览器的query string传一个参数过来
- //127.0.0.1:8080/servlet/status?type=1
- //如果type=1返回200,如果为2返回404
- String type = req.getParameter("type");
-
- if(type.equals("1")){
- resp.setStatus(200);
- }else if(type.equals("2")){
- resp.setStatus(404);
- }else if(type.equals("3")){{
- resp.setStatus(500);
- }}else{
- resp.setStatus(504);
- }
- }
- }
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
-
- @WebServlet("/autoRefresh")
- public class AutoRefreshServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //直接返回响应就行
- resp.setHeader("refresh","3");
- resp.getWriter().write(System.currentTimeMillis()+"");
- }
- }
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- @WebServlet("/redirect")
- public class RedirectServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //进行重定向,收到请求,跳转到哔哩哔哩
- //第一种写法
- //resp.setStatus(302);
- //resp.setHeader("Location","http://www.bilibili.com");
-
- //第二种写法
- resp.sendRedirect("http://www.bilibili.com");
- }
- }
使用顺序表的版本:
- import com.fasterxml.jackson.databind.ObjectMapper;
-
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.util.ArrayList;
-
- @WebServlet("/message")
- public class MessageServlet extends HttpServlet {
- public ObjectMapper objectMapper = new ObjectMapper();
- public ArrayList
arrayList = new ArrayList<>(); -
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- resp.setContentType("text/json; charset=utf8");
- resp.getWriter().write(objectMapper.writeValueAsString(arrayList));
- }
-
- //浏览器提交数据
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //获取到body中的数据并且解析
- Message message = objectMapper.readValue(req.getInputStream(),Message.class);
- //将得到的数据保存
- arrayList.add(message);
- resp.setStatus(200);
-
- System.out.println("提交数据成功,form:" + message.getFrom() +
- " to: " + message.getTo() + " Message: " + message.getMessage());
- }
- }
- class Message {
- private String From;
- private String To;
- private String Message;
- public String getFrom() {
- return From;
- }
- public void setFrom(String from) {
- From = from;
- }
- public String getTo() {
- return To;
- }
- public void setTo(String to) {
- To = to;
- }
- public String getMessage() {
- return Message;
- }
- public void setMessage(String message) {
- Message = message;
- }
- }
使用数据库的版本:
- //使用这个类来封装DataSource
- public class DBUtil {
- private static volatile DataSource dataSource = null;
-
- public static DataSource getDataSource(){
- if(dataSource == null){
- synchronized (DBUtil.class){
- if(dataSource == null){
- dataSource = new MysqlDataSource();
- ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java105?characterEncoding=utf8&useSSL=false");
- ((MysqlDataSource)dataSource).setUser("root");
- ((MysqlDataSource)dataSource).setPassword("111111");
- }
- }
- }
- return dataSource;
- }
-
- private DBUtil() {}
- }
-
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
-
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.sql.DataSource;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
-
- @WebServlet("/message")
- public class MessageServlet extends HttpServlet {
- // 这个对象在多个方法中都需要使用
- private ObjectMapper objectMapper = new ObjectMapper();
-
- // 负责让页面获取到数据
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- // 显式声明当前的响应数据格式 不要让客户端去猜!!!
- resp.setContentType("application/json; charset=utf8");
- // 把 messageList 转成 json 字符串, 并且返回给页面就行了.
- List
messageList = null; - try {
- messageList = load();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- resp.getWriter().write(objectMapper.writeValueAsString(messageList));
- }
-
- // 提交数据
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- // 获取到 body 中的数据并解析
- Message message = objectMapper.readValue(req.getInputStream(), Message.class);
- // 把这个 message 保存一下. 简单的办法就是保存在内存中.
- // messageList.add(message);
- try {
- save(message);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- resp.setStatus(200);
-
- System.out.println("提交数据成功: from=" + message.getFrom()
- + ", to=" + message.getTo() + ", message=" + message.getMessage());
- }
-
- private List
load() throws SQLException { - // 从数据库查询数据
-
- // 1. 先有一个数据源
- DataSource dataSource = DBUtil.getDataSource();
-
- // 2. 建立连接
- Connection connection = dataSource.getConnection();
-
- // 3. 构造 SQL
- String sql = "select * from message";
- PreparedStatement statement = connection.prepareStatement(sql);
-
- // 4. 执行 SQL
- ResultSet resultSet = statement.executeQuery();
-
- // 5. 遍历结果集合
- List
messageList = new ArrayList<>(); - while (resultSet.next()) {
- Message message = new Message();
- message.setFrom(resultSet.getString("from"));
- message.setTo(resultSet.getString("to"));
- message.setMessage(resultSet.getString("message"));
- messageList.add(message);
- }
-
- // 6. 关闭连接
- statement.close();
- connection.close();
- return messageList;
- }
-
- private void save(Message message) throws SQLException {
- // 把数据保存到数据库中
-
- // 1. 先有一个数据源
- DataSource dataSource = DBUtil.getDataSource();
-
- // 2. 建立连接
- Connection connection = dataSource.getConnection();
-
- // 3. 构造 SQL
- String sql = "insert into message values(?, ?, ?)";
- PreparedStatement statement = connection.prepareStatement(sql);
- statement.setString(1, message.getFrom());
- statement.setString(2, message.getTo());
- statement.setString(3, message.getMessage());
-
- // 4. 执行 SQL
- int ret = statement.executeUpdate();
- System.out.println("ret = " + ret);
-
- // 5. 关闭连接
- statement.close();
- connection.close();
- }
- }
如果对您有帮助的话,
不要忘记点赞+关注哦,蟹蟹
如果对您有帮助的话,
不要忘记点赞+关注哦,蟹蟹
如果对您有帮助的话,
不要忘记点赞+关注哦,蟹蟹