baseServlet的工具类
程序员文章站
2022-05-08 13:16:13
...
功能过多.servlet管理不方便,为了减少servlet的数量,便于管理维护,可以使用工具类来做:
baseServlet的工具类:
package com.wangshi.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;
/**
* @author wanghaihcuan
*baseServlet的工具类(继承httpServlet)
*没有的话去父类里面找,一级一级找,找到为止
*/
public class BaseServlet extends HttpServlet {
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("这里是 service.......");
//获取客户端 提交 到服务器端的 method 对应的值
String method = request.getParameter("method");
//定义变量,存放功能执行完毕之后要转发的路径
String path = null;
//获取当前字节码对象(servletDemo02.class 在内存中的对象)
Class clazz = this.getClass();
try {
//获取clazz上名称为method的方法
Method method1 = clazz.getMethod(method,HttpServletRequest.class ,HttpServletResponse.class );
if(null != method1){
//调用找到的方法
path=(String) method1.invoke(this, request,response);
}
if(null != path){
//服务端的转发
request.getRequestDispatcher(path).forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
servlet1.0版本:
package com.wangshi.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
/**servlet 1.0版本
* @author wanghaichaun
*客户端向服务器提交表单产生一个键值对method, 目的是减少servlet数量
*如果 功能过多,不易维护,可用反射来做
*/
public class ServletDemo01 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//要转发的路径[因为要跳转的是路径,只不过是跳转不同,所以抽取出来]
String path = null;
//获取客户端提交到服务器的method 对应的值
String method = request.getParameter("method");
//通过判断method中不同的内容来决定 本次功能
if("addStu".equals(method)){
path=addStu(request, response);
//System.out.println("添加学生......");
}else if ("delStu".equals(method)) {
path=delStu(request, response);
//System.out.println("删除学生......");
}else if ("checkStu".equals(method)) {
path=checkStu(request, response);
//System.out.println("检查学生......");
}
//判断它是否为空,服务端的 统一的 转发
if( null != path){
request.getRequestDispatcher(path).forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected String addStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("添加学生......");
return "/test.html";
}
protected String delStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("删除学生......");
return "/test.html";
}
protected String checkStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
System.out.println("检查学生......");
response.getWriter().println("我在看着你看着你");
return null;
}
}
servlet2.0版本:反射
package com.wangshi.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;
/**
* @author wanghaichuan
*使用 反射 来减少servlet数量
*/
public class ServletDemo02 extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取客户端 提交 到服务器端的 method 对应的值
String method = request.getParameter("method");
//定义变量,存放功能执行完毕之后要转发的路径
String path = null;
//获取当前字节码对象(servletDemo02.class 在内存中的对象)
Class clazz = this.getClass();
try {
//获取clazz上名称为method的方法
Method method1 = clazz.getMethod(method,HttpServletRequest.class ,HttpServletResponse.class );
if(null != method1){
//调用找到的方法
path=(String) method1.invoke(this, request,response);
}
if(null != path){
//服务端的转发
request.getRequestDispatcher(path).forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
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.setCharacterEncoding("utf-8");
response.getWriter().println("呦呦呦,等你哦");
return null;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
jsp页面提交方式:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script >
function fn(){//post方法数据,登录验证检查
$.post("/BaseServlet/ServletDemo03",{"method":"checkStu","user":"tom"},function(data){
alert(data);
}/* ,"json" */);
}
</script>
</head>
<body>
<!-- 从客户端发起请求到服务端的功能,如下 -->
<!-- 用表单提交 -->
<form action="/BaseServlet/ServletDemo03?method=addStu" method="post">
用户名:<input type="text" name="username" /><br>
密码:<input type="password" name="password" /><br>
<input type="submit" value="提交"/>
</form><br>
<!-- 链接提交界面 -->
<a href="/BaseServlet/ServletDemo03?method=delStu">删除学生</a><br>
<!-- ajax按钮提交 -->
<button onclick="fn()">按钮</button>
</body>
</html>
上一篇: 使用BaseServlet提高Servlet的代码复用性
下一篇: 先进的医疗方法