工作以来公司项目从开始的Spring+SpringMVC到现在直接使用Springboot,框架越用越爽,但凡涉及到框架内部的东西自己始终是一知半解。只会简单的使用,并不知道它内部的实现,很多时候程序报错,只要涉及框架永远都是百度一下,不知为何报错以及为何要如此处理。看着大神们的各种解决方案,自己始终不得其理,遂先从web基础走起
一、Servlet的概述
Servlet是用Java编写的服务端程序,其作用在于Servlet作为中间层连接着
浏览器或其他HTTP客户端请求
和HTTP服务器上的数据库或应用程序
。就浏览器而言,如果把Servlet比作桥梁
那么servlet一端连接浏览器,一端连接服务器,他的作用就是保证浏览器和服务器之间的通信
维基百科
Servlet在整个web程序中的位置
菜鸟教程
二、Servlet生命周期
servlet中 javax.servlet.Servlet 接口的 提供了三个方法
init() 初始化时调用
service() 用来处理客户端发送过来的请求
destory() servlet被销毁调用
代码中一窥就近
1、创建一个web项目
Web项目目录
2、在src新增一个包,包下新增一个类继承HttpServlet,类中代码如下
public class ServletController extends HttpServlet {
private static final long serialVersionUID = 8721524830738176364L;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
System.out.println("init-初始化");
}
@Override
public void service(HttpServletRequest request, HttpServletResponse arg1)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("执行了service方法;");
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("执行了doPost方法;");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行了doGet方法");
PrintWriter out = resp.getWriter();
resp.setContentType("text/html;charset=ISO-8859-1");
out.println("<h1>你好GET方法</h1>");;
}
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("执行了destroy方法;");
}
}
web.xml中配置如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>servletdemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- GET请求 -->
<servlet>
<!-- servlet名称 -->
<servlet-name>servletDemo</servlet-name>
<!-- 名称对应的类 -->
<servlet-class>com.ServletController</servlet-class>
</servlet>
<servlet-mapping>
<!-- 这里的servlet名称和<servlet>中的名保持一致 -->
<servlet-name>servletDemo</servlet-name>
<!-- 请求的时url -->
<url-pattern>/servlet/hellowServlet</url-pattern>
</servlet-mapping>
<!-- GET请求 -->
<!-- POST请求 -->
<servlet>
<servlet-name>servletPost</servlet-name>
<servlet-class>com.ServletController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletPost</servlet-name>
<url-pattern>/servlet/postServlet</url-pattern>
</servlet-mapping>
<!-- POST请求 -->
</web-app>
inde.jsp中的内容
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Servlet</h1>
<!-- a标签默认的GET请求 -->
<a href="servlet/hellowServlet">GET请求</a>
<!-- 表单提交 -->
<form action="servlet/postServlet" method="post">
<p>客户名称: <input type="text" name="CustomerName" style="width: 300px" /></p>
<p>客户电话: <input type="text" name="CustomerTel" style="width: 300px" /></p>
<input type="submit" value="POST请求" />
</form>
</body>
</html>
配置文件说明:
web.xml图解.png
请求地址:本机地址:tomact端口/项目名 ===>http://localhost:8089/servletdemo
ps
:Tomcat默认端口是8080,我这里改了所以是8089,因为web.xml中的<welcome-file-list></welcome-file-list>的配置默认是依次往下找,所以根据上述地址能找到index.jsp
通过代码运行可知
第一次运行时先执行init()然后执行了 service()
第二执行时只执行了service()方法
当我通过stop关闭tomcat时执行了destory()方法
当service()方法存在
时,无论是执行GET请求或者POST请求时,都是执行service()
当service()方法注释
时,执行GET请求时调用的时doGet(),执行POST请求时执行doPost()
ps:关于service()方法和doGet()及doPost()方法之间的关系我这里做一个简单的说明
https://docs.oracle.com/javaee/7/api/toc.htm
这里大概的意思是:在接收HTTP请求时,service()方法会将请求分派到该类的定义的doXXX方法
当重写了service()方法后,请求执行的是我们重写的service()方法,所以当执行到service()并未做分派,反之,则亦然,所以才会出现上述关于service()方法的情况