servlet从客户端向服务端发送请求调用功能的几种方式

本文介绍通过Servlet处理客户端请求的不同方式及优化过程,包括使用表单、链接和AJAX发起请求,并探讨了如何利用反射减少代码冗余,最终通过抽象基类实现模块间的灵活扩展。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:通过表单向服务端发起请求 (action)

2:通过链接向服务端发起请求

3:通过ajax向服务端发起请求

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>--这里添加ajax的东西
</head>
<body>
<form action="/BaseServlet/ServletDemo01?method=addStu" method="post">--1,通过页面action  点提交按钮触发servlet
      用户<input type="text" name="username"/><br/>
      <button>提交</button>
</form>
<br/>
<a href="/BaseServlet/ServletDemo01?method=delStu">删除学生</a><br/>--2,通过点‘删除学生’,链接触发servlet
<button onclick="fn()">按钮</button>         --3,通过按钮ajax触发servlet 
<script>
function fn(){
	$.post("/BaseServlet/ServletDemo01",{"method":"checkStu","user":"tom"},function(data){
	alert(data);
	});
}
</script>
</body>
</html>

通过表单的请求触发到传到servlet中:

public class ServletDemo01 extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取客户端提交到服务端的method对应的值
		String md=request.getParameter("method");
		//定义变量,存放功能执行完毕之后要转发的路径
		String path=null;
		
		//通过判断md中不同的内容来决定本次功能,然后将参数再次传到对应的方法中进行执行
		if("addStu".equals(md)){
			path=addStu(request, response);
		}else if("delStu".equals(md)){
			path=delStu(request, response);
		}else if("checkStu".equals(md)){
			path=checkStu(request, response);
		}else if("".equals(md)){
			
		}
		if(null!=path){
			//服务端的转发
			request.getRequestDispatcher(path).forward(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 {
		System.out.println("检查学生");
		response.getWriter().println("DDDDDD");
		return null;
	}		
}

优化:接下来通过反射 执行对应方法, 减少判断method的if else语句

public class ServletDemo02 extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取客户端提交到服务端的method对应的值
		String md=request.getParameter("method");
		//定义变量,存放功能执行完毕之后要转发的路径
		String path=null;
		
        //---------------------------------------------------------------------------------------------
        //通过反射 执行对应方法, 减少判断method的if else语句
          //获取到当前字节码对象(ServletDemo02.class在内存中对象)
		Class clazz = this.getClass();
		try {
			//获取clazz上名称为md方法
			Method method=clazz.getMethod(md, HttpServletRequest.class,HttpServletResponse.class);
			if(null!=method){
				//调用找到的方法
				path=(String)method.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.getWriter().println("DDDDDD");
		return null;
	}
}

继续优化:抽取出一个servlet的公共类

public class BaseServlet extends HttpServlet {
    //重写service方法,将之前的公共代码复制进service方法中
	@Override
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("service.....");
		//获取客户端提交到服务端的method对应的值
				String md=request.getParameter("method");
				//定义变量,存放功能执行完毕之后要转发的路径
				String path=null;
				//获取到当前字节码对象(ServletDemo02.class在内存中对象)
				Class clazz = this.getClass();
				try {
					//获取clazz上名称为md方法
					Method method=clazz.getMethod(md, HttpServletRequest.class,HttpServletResponse.class);
					if(null!=method){
						//调用找到的方法
						path=(String)method.invoke(this, request,response);
					}
					if(null!=path){
						//服务端的转发
    						request.getRequestDispatcher(path).forward(request, response);
					}
				} catch (Exception e) {
					e.printStackTrace();
				} 
	}
}

如果再添加模块,此时父类的service方法通过反射直接找到此模块下的方法,只需要写对路径 :

//继承写有公共servlet的类 BaseServlet 
public class ServletDemo03 extends BaseServlet {
	public ServletDemo03() {
             //调用servlet首先调用‘无参构造’,接着调用‘init()方法,找不到就去父类里找’
             //无参构造和init()方法首次访问会调用一次,init()调用可配置
		System.out.println("没有参数构造函数");
	}
	
	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("DDDDDD");
		return null;
	}
}
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值