通用的servlet
1 通用的servlet实现的原理
当页面向servlet发起请求时携带一个参数,我们的所有servlet继承自通用的BaseServlet,通用的servlet在取得参数后根据参数值执行子类servlet中的对应方法。
2 实例
2.1 目录结构
2.2 BaseServlet.java
package com.zhujunwei.servlet;
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;
/**
* 接收要请求servlet所携带的方法名称,根据方法名称到相应的servlet去执行方法
* @author zhujunwei
* 2019年2月27日 下午11:18:13
*/
public class BaseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// localhost:8080/ServletDemo?method=addStu
String method = req.getParameter("method");
if (null == method || "".equals(method) || method.trim().equals("")) {
method = "execute";
}
// 注意:此处的this代表的是子类的对象
// 子类对象字节码对象
Class<? extends BaseServlet> clazz = this.getClass();
try {
// 查找子类对象对应的字节码中的名称为method的方法.这个方法的参数类型是:HttpServletRequest.class,HttpServletResponse.class
Method md = clazz.getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
if(null!=md){
String jspPath = (String) md.invoke(this, req, resp);
if (null != jspPath) {
req.getRequestDispatcher(jspPath).forward(req, resp);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 默认方法
public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
return null;
}
}
2.3 ServletDemo.java
package com.zhujunwei.servlet ;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/ServletDemo")
public class ServletDemo extends BaseServlet {
private static final long serialVersionUID = 1L;
public ServletDemo(){}
public String addStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("添加学生");
return "test.html" ;
}
public String delStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("删除学生");
return "test.html" ;
}
public String checkStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("检查学生");
response.getWriter().println("zzz");
return null ;
}
}
2.4 index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="js/jquery-3.3.1.min.js" type="text/javascript"></script>
</head>
<body>
<form action="ServletDemo?method=addStu" method="post">
用户<input type="text" name="username" />
<button>提交</button>
</form>
<a href="ServletDemo?method=delStu">删除学生</a><br>
<button onclick="fu()">按钮</button>
</body>
<script>
function fu() {
$.post("ServletDemo", {
"method" : "checkStu",
"user" : "tom"
}, function(data) {
alert(data);
});
}
</script>
</html>
2.5 test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
test
</body>
</html>
2.6 运行结果
可以看到,可以根据不同的方法名称执行不同的方法。
(有时候那个删除不执行,我也不知道为啥,以后解决。)
---------------
我每一次回头,都感觉自己不够努力,所以我不再回头。
---------------