原理:
从客户端向服务端发起请求,每次都要传递额外的键值对的数据
method = ””
服务端获取到method对应的内容之后,通过判断不同的内容调用不同的功能
覆写service()方法
以后的Servlet要继承BaseServlet类,而非HttpServlet类-
Servlet生命周期
- Servlet 通过调用 init () 方法进行初始化。它在第一次创建 Servlet 时被调用,在后续每次用户请求时不再调用。
- Servlet 调用 service() 方法来处理客户端的请求。每次服务器接收到一个 Servlet 请求时,服务器会产生一个新的线程并调用服务。service() 方法检查 HTTP 请求类型(GET、POST、PUT、DELETE 等),并在适当的时候调用 doGet、doPost、doPut,doDelete 等方法。
- Servlet 通过调用 destroy() 方法终止(结束)。只会被调用一次,在 Servlet 生命周期结束时被调用。destroy() 方法可以让您的 Servlet 关闭数据库连接、停止后台线程、把 Cookie 列表或点击计数器写入到磁盘,并执行其他类似的清理活动。在调用 destroy() 方法之后,servlet 对象被标记为垃圾回收。
- 最后,Servlet 是由 JVM 的垃圾回收器进行垃圾回收的。
BaseServlet.java
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BaseServlet extends HttpServlet {
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("service.....");
//获取客户端提交到服务端的method对应的值
String md = request.getParameter("method");
//定义变量,存放功能执行完毕之后要转发的路径
String path = null;
//获取到当前字节码对象(ServletDemo.class在内存中对象)
Class clazz = this.getClass();
try {
//获取clazz上名称为md方法
Method method = clazz.getMethod(md, HttpServletRequest.class, HttpServletResponse.class);
if(null != method){
//调用找到的方法
path = (String)method.invoke(this, request, response);
}
if(null != path){
//服务端的转发
request.getRequestDispatcher(path).forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
- StuServlet.java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.nwpu.servlet.baseservlet.BaseServlet;
@SuppressWarnings("serial")
public class StuServlet extends BaseServlet {
String path = "/test.jsp";
public String addStu(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("添加学生");
return path;
}
public String deleteStu(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("删除学生");
return path;
}
public String updateStu(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("更新信息");
return path;
}
public String selectStu(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("查询学生");
path = null;
response.getWriter().println("find sth");
return path;
}
}
- index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
out.println(basepath); // http://localhost:8080/BaseServlet/
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index page</title>
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
function fun() {
$.get("<%=path%>/StuServlet", {
"method" : "selectStu",
"user" : "tom"
}, function(data) {
alert(data);
});
}
</script>
</head>
<body>
<!-- 1. 通过表单向服务端发起请求 -->
<form action="<%=path%>/StuServlet?method=addStu" method="post">
<button>form表单post提交</button>
</form>
<br />
<!-- 2. 通过链接向服务端发起请求 -->
<a href="<%=path%>/StuServlet?method=deleteStu">超链接</a>
<br />
<!-- 3. Ajax向服务端发起请求 -->
<button onclick="fun()">Ajax方式</button>
</body>
</html>
- test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test page</title>
</head>
<body>
test.jsp
</body>
</html>