Javaweb网上商城项目实战(3)抽取BaseServlet-版本2

改进之前的版本

[v_notice]本节内容与项目无关,只是为了说明编写通用Servlet的目的以及实现,如果比较熟悉Javaweb了可以跳过[/v_notice]

上一节最后我们也意识到之前的方法存在着不可忽视的弊端,这一节我们改进之前的方法,用反射来做。代码如下:

新建一个ServletDemo02.java

package com.geekerstar.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 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;
		//获取到当前字节码对象(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;
	}			
}

运行程序,结果无误。

抽取公共类

接下来正式开始版本的改造,抽取一个公共类BaseServlet

package com.geekerstar.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 BaseServlet extends HttpServlet {
	@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();
				} 
	}
}

使用新的公共类

新建一个index3.html





Insert title here%MINIFYHTML6edc669f6d59f52400f39220aaf253dd7%

用户

删除学生
%MINIFYHTML6edc669f6d59f52400f39220aaf253dd8%%MINIFYHTML6edc669f6d59f52400f39220aaf253dd9%

新建一个ServletDemo03

package com.geekerstar.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 ServletDemo03 extends BaseServlet {
	public ServletDemo03() {
		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;
	}
}

源码下载

[dm href=’https://www.jikewenku.com/product/1700.html’]下载地址[/dm]

本站所有文章均由网友分享,仅用于参考学习用,请勿直接转载,如有侵权,请联系网站客服删除相关文章。若由于商用引起版权纠纷,一切责任均由使用者承担
极客文库 » Javaweb网上商城项目实战(3)抽取BaseServlet-版本2

欢迎加入「极客文库」,成为原创作者从这里开始!

立即加入 了解更多