我们在对JavaWEB工程进行开发的时候,我们经常会遇到这样一个问题,在jsp中发送到Servlet的每一个请求都要写一个对应的Servlet,这样会造成一个工程完成下来需要写几十个Servlet,那么怎么可以做到多个请求共用一个Servlet呢?
下面介绍两种方法(第一种比较难理解一点,第二种相对容易):
一、利用反射原理(根据获取到的url-pattern,截取出相应的方法名,进而调用相应的方法)
1.配置web.xml文件,
StaffServlet
cn.edu.lingnan.servlet.StaffServlet
StaffServlet
*.do
所有以 .do 结尾的请求都会调用StaffServlet
2.编写Servlet(例子:StaffServlet)
public class StaffServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取能够与“url-pattern”中匹配的路径
String method = request.getServletPath();
//(此时处理的请求是查询 query.do)
System.out.println("request.getServletPath()获取的值为: " + method);//输出 /query.do
//2.通过字符串截取,把方法名 query 截取出来
method = method.substring(1, method.length()-3);
System.out.println("截取后的值为: "+ method);
Method m = null;
try {
//3.获取当前类中名字为 method 的方法
m = this.getClass().getDeclaredMethod(method,
HttpServletRequest.class, HttpServletResponse.class);
//4.调用 method 方法
m.invoke(this, request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("query方法被调用");
}
private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("delete方法被调用");
}
}
3. jsp 中发送请求调用servlet
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
这是一个查询请求,调用Servlet中的query方法
这是一个删除请求,调用Servlet中的delete方法
点查询和删除的超链接后台输出结果如下:
如还需要添加其他操作请求,只需在Servlet中添加相应名字的方法,如更新请求 jsp中 update.do, servlet中添加如下方法:
private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
其他请求也是如上操作即可,注意:url-pattern 中的扩展名可以是任意字符,不一定要是 .do , 例如也可以是 .doStaff , 但是改了扩展名了要注意修改截取的字符串的位置,如修改由.do 修改为 .doStaff 时,doPost中
method = method.substring(1, method.length()-3);需修改为(.do 为三个字符)
method = method.substring(1, method.length()-8);(.doStaff 为八个字符)
jsp 中调用时,则也相应修改 query.do ---> query.doStaff
二、利用URL中的参数传送需要调用的方法名
1.配置web.xml, 映射Servlet路径(以下以 StudentServlet 调用 Servlet 中的 query 和 delete 方法为例子)
StudentServlet
servlet.StudentServlet
StudentServlet
/studentServlet
2.编写Servlet(StudentServlet)
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class StudentServlet
*/
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求参数 method
String method = request.getParameter("method");
System.out.println("获取到的method参数为: " + method);
//调用 method 方法
if(method.equals("query")) {
this.query(request, response);
}else {
if(method.equals("delete")) {
this.delete(request, response);
}
}
}
private void query(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("query 方法被调用");
}
private void delete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("delete 方法被调用");
}
}
3. jsp中发送请求调用Servlet
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
这是一个查询请求,调用Servlet中的query方法
这是一个删除请求,调用Servlet中的delete方法
点查询和删除超链接后台输出结果如下: