• 极客专栏正式上线!欢迎访问 https://www.jikewenku.com/topic.html
  • 极客专栏正式上线!欢迎访问 https://www.jikewenku.com/topic.html

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

项目实战 Geekerstar 11个月前 (05-16) 346次浏览 已收录 0个评论 扫描二维码
文章目录[隐藏]

改进之前的版本

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

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

新建一个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

<!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>
</head>
<body>
<form action="/BaseServlet/ServletDemo03?method=addStu" method="post">
	用户<input type="text" name="username"/><br/>
	<button>提交</button>
</form>
<br/>
<a href="/BaseServlet/ServletDemo03?method=delStu">删除学生</a><br/>
<button onclick="fn()">按钮</button>
<script>
function fn(){
	$.post("/BaseServlet/ServletDemo03",{"method":"checkStu","user":"tom"},function(data){
		alert(data);
	});
}
</script>
</body>
</html>

新建一个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;
	}
}

源码下载

下载地址
丨极客文库, 版权所有丨如未注明 , 均为原创丨
本网站采用知识共享署名-非商业性使用-相同方式共享 3.0 中国大陆许可协议进行授权
转载请注明原文链接:Javaweb网上商城项目实战(3)抽取BaseServlet-版本2
喜欢 (0)
[247507792@qq.com]
分享 (0)
Geekerstar
关于作者:
本站技术支持

您必须 登录 才能发表评论!

  • 精品技术教程
  • 编程资源分享
  • 问答交流社区
  • 极客文库知识库

客服QQ


QQ:2248886839


工作时间:09:00-23:00