一个Servlet中可以有多个处理请求的方法

 我来答
百度网友5e15b90
2017-10-18 · TA获得超过1031个赞
知道小有建树答主
回答量:806
采纳率:91%
帮助的人:832万
展开全部
方法一
可以通过在请求的时候加上参数,然后在servlet中获取请求的参数,再去调用对应的方法。达到一个servlet处理多个请求的目的
test.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'test.jsp' starting page</title>
</head>
<body>
<!--
请求的时候加上对应的请求参数
-->
<a href="customerServlet?method=add"> Add</a>
<br>
<br>
<a href="customerServlet?method=query"> Query</a>
<br>
<br>
<a href="customerServlet?method=delete"> Delete</a>
</body>
</html>

CustomerServlet.java:
package com.aaa.mvcapp.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CustomerServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取对应的请求参数
String method = request.getParameter("method");
//根据请求参数去调用对应的方法。
if ("add".equals(method)) {
add(request, response);

} else if ("query".equals(method)) {
query(request, response);
}

else if ("delete".equals(method)) {
delete(request, response);
}
}

private void delete(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
System.out.println("delete");

}

private void query(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
System.out.println("query");
}

private void add(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
System.out.println("add");

}
}

方法二
1.在servlet的mapping中配置为 *.do来响应多个请求
2. 在servlet中获取servletPath。(例如/=add.do)
3. 去除/ 和.do,利用反射再去调用对应的方法。
web.xml(servlet配置):
<servlet>
<servlet-name>CustomerServlet</servlet-name>
<servlet-class>com.aaa.mvcapp.servlet.CustomerServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>CustomerServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>123456789

test.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'test.jsp' starting page</title>
</head>
<body>
<a href="add.do"> Add</a>
<br>
<br>
<a href="query.do"> Query</a>
<br>
<br>
<a href="delete.do"> Delete</a>
</body>
</html>

CustomerServlet.java:
package com.aaa.mvcapp.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;

public class CustomerServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// 获取方法名字
String servletPath = request.getServletPath();// /xxxxxx.do
// 去掉斜杠和.do
String methodName = servletPath.substring(1, servletPath.length() - 3);// xxxxxx

try {
// 利用反射获取方法
Method method = getClass().getDeclaredMethod(methodName,
HttpServletRequest.class, HttpServletResponse.class);
// 执行相应的方法
method.invoke(this, request, response);
} catch (Exception e) {
// TODO: handle exception
}
}

private void delete(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
System.out.println("delete");

}

private void query(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
System.out.println("query");
}

private void add(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
System.out.println("add");

}
}

这样一来不同的请求可以会用同一个servlet来处理,在servlet内部为不同的请求分别有不同的方法去处理。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式