在Java Web学习的初期,开发的小项目几乎都是JSP+Servlet+JDBC,长期开发下来,会发现当业务逻辑设计的接口一多的时候,充当控制器的Servlet也会越来越多,但是处理的业务逻辑相对单一。后来学习Struts2或者SpringMVC,发现它们处理起来优雅得多,但是配置起来也比纯的Servlet要繁琐,对于经常做小项目的我来说有点大材小用了,于是我根据前人的经验抽离了一个BaseServlet,用反射的机制来处理请求,这样处理业务逻辑的Servlet要相对简单的多,废话不说,把自己使用的一套拿出来晒晒。
BaseServlet
由于服务器端经常是用JSON与Android和iOS客户端进行交互,所以这里返回的就是JSON数据
//这个抽象类,BaseServlet类不需要在web.xml中进行配置
public abstract class BaseServlet extends HttpServlet {
// final 防子类复写
public final void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public final void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1、获得执行的方法名
String methodName = request.getParameter("method");
// 默认方法
if (methodName == null) {
methodName = "execute";
}
System.out.println("BaseServlet : " + this + " , " + methodName);
try {
// 2、通过反射获得当前运行类中指定方法,形式参数
Method executeMethod = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
// 3、反射执行方法
String result = (String)executeMethod.invoke(this, request, response);
// 4、将json数据返回
response.getWriter().write(result);
} catch (NoSuchMethodException e) {
throw new RuntimeException("请求的方法[" + methodName + "]不存在");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("服务器异常", e);
}
}
/**
* 此方法用于复写,方便子类编程,默认执行方法
*/
public void execute(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
真正处理业务逻辑的Servlet
需要做的就是继承上面的BaseServlet,然后根据业务需求写自己的方法即可,乍一看像SpringMVC,但是要注意这里的方法名和返回值,方法名决定了请求时的method参数的值,返回值由于是JSON,所以用的是String。
public class UserServlet extends BaseServlet {
public String users(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArrayList<User> users = new ArrayList<User>();
for (int i = 0; i < 10; i++) {
User user = new User(i, "zhangsan" + i, i + 10, "wukong" + i);
users.add(user);
}
Gson gson = new Gson();
return gson.toJson(users);
}
}
==========================================================
//自定义的一个PO
public class User {
private int id;
private String name;
private int age;
private String nickname;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public User(int id, String name, int age, String nickname) {
this.id = id;
this.name = name;
this.age = age;
this.nickname = nickname;
}
}
部署Servlet
将自己的业务UserServlet部署到web.xml,如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>test.app.api.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/UserServlet</url-pattern>
</servlet-mapping>
</web-app>
访问Servlet
部署并启动tomcat以后,打开浏览器访问:http://localhost/AppTestAPI/UserServlet?method=users
结果如下:
网友评论