BaseServlet优化Servlet,实现类似struts2的一些简单效果
package cn.itcast.web.servlet; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class AServlet extends BaseServlet { private static final long serialVersionUID = 1L; /*@Override
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException { * 1、获取参数,用来说明要调用的方法
* 2、然后判断是哪个方法执行 String methodName=req.getParameter("method"); if(methodName == null || methodName.trim().isEmpty()){
throw new RuntimeException("您没有传递method参数,无法确定要调用的方法");
}
// if(methodName.equals("addUser")){
// addUser(req,res);
// }else if(methodName.equals("eidtUser")){
// editUser(req,res);
// }else if(methodName.equals("deleteUser")){
// deleteUser(req,res);
// } * 我们发现这样的代码不好复用
* 1、我们可以通过方法名称,使用反射调用呢?
* 需要得到class,然后调用它的方法进行查询
* 我们查询的是当前类的方法 Class c=this.getClass();
Method method=null;
try{
method=c.getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
}catch(Exception e){
throw new RuntimeException("您要调用的方法"+methodName+"(HttpServletRequest,HttpServletResponse) 不存在");
} * 调用method try {
method.invoke(this, req,res);
} catch (Exception e) {
System.out.println("您调用的方法"+methodName+"(HttpServletRequest,HttpServletResponse) 内部抛出了异常");
throw new RuntimeException(e);
} }*/
public void addUser(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("addUser()...");
throw new IOException("测试一下");
} public void editUser(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("editUser()...");
} public void findAll(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("findAllUser()...");
} public void deleteUser(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("deleteUser()...");
} }
/**
* 我们发现这样复用性还是不够理想,我们可以抽象出一个弗雷给别人继承
*/
package cn.itcast.web.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; @SuppressWarnings({"rawtypes","unchecked"})
public class BaseServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException { String methodName=req.getParameter("method");
if(methodName == null || methodName.trim().isEmpty()){
throw new RuntimeException("您没有传递method参数,无法确定要调用的方法");
}
Class c=this.getClass();
Method method=null;
try{
method=c.getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
}catch(Exception e){
throw new RuntimeException("您要调用的方法"+methodName+"(HttpServletRequest,HttpServletResponse) 不存在");
}
String text=null;//获取返回值
try {
text=(String) method.invoke(this, req,res);
/*
*获取请求处理方法后返回的字符串,它表示转发或者重定向的路径
*如果返回值中不包含“:”,表示使用默认方式:转发
*如果返回值中包含“:”,将字符串通过“:”截取为两部分,前一部分表示标识,f代表转发,r代表重定向
*如果返回的字符串是null或者"",我们什么也不干
*/
} catch (Exception e) {
System.out.println("您调用的方法"+methodName+"(HttpServletRequest,HttpServletResponse) 内部抛出了异常");
throw new RuntimeException(e);
}
if(text==null || text.trim().isEmpty()) return;
if(!text.contains(":")){
req.getRequestDispatcher(text).forward(req, res);
}else{
int index=text.indexOf(":");
String bz=text.substring(0, index);
String path=text.substring(index+1);
if(bz.equalsIgnoreCase("r")){
res.sendRedirect(path);
}else if(bz.equalsIgnoreCase("f")){
req.getRequestDispatcher(path).forward(req, res);
}else{
throw new RuntimeException("您的指定:"+bz+"暂时没有开通此业务");
}
} }
/**
* 我们在Servlet中实现重定向和转发感觉有点麻烦,我们希望抽象出来再BaseServlet中处理
* request.getRequestDispatcher("xxx").forward(request,response)
* response.sendRedirect("xxx")
*/ }
package cn.itcast.web.servlet; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class BServlet extends BaseServlet { private static final long serialVersionUID = 1L; public String fun1(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException{
//return "forward:/index.jsp";
System.out.println("fun1()...");
return "f:/index.jsp";
}
public String fun2(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException{
//return "redirect:/index.jsp";
System.out.println("fun2()...");
return "r:"+req.getContextPath()+"/index.jsp";
}
public String fun3(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException{
System.out.println("fun3()...");
return "/index.jsp";
}
public String fun4(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException{
System.out.println("fun4()...");
return null;
}
public String fun5(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException{
System.out.println("fun3()...");
return "d:/index.jsp";
}
}
BaseServlet
1.我们希望在一个Servlet中可以有多个请求处理方法
2、客户端发送请求时,必须多给出一个参数,用来说明要调用的方法
3、参数名遵守约定
4、希望帮助简化重定向和转发,利用返回值
domain:User
dao:UserDao
service:UserService
servlet:UserServlet
void init(ServletConfig config)
void destory()
void service(ServletRequest,ServletResponse)throws IOException,ServletException{
在这里让它去调用其他方法!
要求:用户发出请求时,必须给出一个参数
BaseServlet优化Servlet,实现类似struts2的一些简单效果的更多相关文章
- 自己实现的简单MVC框架(类似Struts2+Spring)
一.框架简介 本框架是一个类似于Struts2+Spring的框架,目的在于个人钻研和技术分享,将流行技术框架Struts2.Spring中使用到的主要技术以较为简化的方式实现出来,给大家一个更直观的 ...
- 十三、事务、连接池 、ThreadLocal 、BaseServlet自定义Servlet父类 、 DBUtils à commons-dbutils
l 事务 l 连接池 l ThreadLocal l BaseServlet自定义Servlet父类(只要求会用,不要求会写) l DBUtils à commons-dbutils 事务 l 事务的 ...
- struts2和servlet同时用(访问servlet时被struts2过滤器拦截问题的解决)
在同一个项目中间,如果既用到servlet有用了struts2的框架,运行项目时可能无法正常使用servlet,原因是在配置struts2的核心控制器时<url-pattern>/*< ...
- 【WEB小工具】BaseServlet—一个Servlet处理多个请求
package cn.itcast.test.web.servlet; import java.io.IOException; import java.io.PrintWriter; import j ...
- Duilib实现类似电脑管家扫描目录效果
实现原理: 1.后台开线程遍历目录,遍历出一个文件路径在界面上更新显示(通过发消息通知主界面) 2.需要扩展一下Duilib控件,在此我扩展了CLabelUI,重写了PaintText函数 扩展控件的 ...
- WPF如何实现类似iPhone界面切换的效果(转载)
WPF如何实现类似iPhone界面切换的效果 (version .1) 转自:http://blog.csdn.net/fallincloud/article/details/6968764 在论坛上 ...
- Adobe Edge Animate –地球自转动画的实现,类似flash遮罩层的效果
Adobe Edge Animate –地球自转动画的实现,类似flash遮罩层的效果 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权和原文地址. 目前Edge的功能尚 ...
- PHP实现类似题库抽题效果
PHP实现类似题库抽题效果 大家好,我顾某人又回来了,最近学了一点PHP,然后就想写个简单小例子试试,于是就写了一个类似于从题库抽题的东西,大概就是先输入需要抽题的数量,然后从数据库中随机抽取题目. ...
- 利用反射优化Servlet抽象出父类BaseServlet
在编写servlet的时候发现每个servlet里面的doPost方法都如: protected void doPost(HttpServletRequest request, HttpServlet ...
随机推荐
- UML--组件图,部署图
组件图用于实现代码之间的物理结构,详细来说,就是实现代码交互.通过接口,将不同的软件,程序连接在一起. [理解] 1.组件的定义相当广泛,包含:源码,子系统,动态链接库,Activex控件. 2.组件 ...
- 我如何添加一个空目录到Git仓库?
新建了一个仓库,只是创建一些目录结构,还不里面放什么,要放的内容还没有,还不存在,应该怎么办呢? Git 是不跟踪空目录的,所以需要跟踪那么就需要添加文件! 也就是说 Git 中不存在真正意义上的空目 ...
- Oracle创建自增字段和修改方法-ORACLE SEQUENCE的简单介绍
http://blog.csdn.net/zhoufoxcn/article/details/1762351先假设有这么一个表: create table S_Depart ( DepartI ...
- Android VS IOS
时间: IOS:var d = new Date("2018-04-19 14:23:00".replace(/-/g, "/")); (d = new Dat ...
- hadoop生态系统学习之路(六)hive的简单使用
一.hive的基本概念与原理 Hive是基于Hadoop之上的数据仓库,能够存储.查询和分析存储在 Hadoop 中的大规模数据. Hive 定义了简单的类 SQL 查询语言,称为 HQL.它同意熟悉 ...
- FileOutPutStream 的写操作
package xinhuiji_day07; import java.io.File;import java.io.FileNotFoundException;import java.io.File ...
- iOS --生产JSON格式,创建JSON文件,创建文件夹,指定储存
//生成json文件 - (void)onjson { // 如果数组或者字典中存储了 NSString, NSNumber, NSArray, NSDictionary, or NSNull ...
- ios --转载获ipa 的图片资源
突然想起当初刚学习iOS的时候,就经常通过抓包和提取素材的方式来模仿App,今天就教大家如何一步步提取App的素材! 大家是否有过想要获取别人的素材的想法?看到某些App的资源很不错,很想导出来用 ...
- Fragment 懒加载
我们在遇到Activity嵌套Fragment的时候经常会遇到多个Fragment页面同时加载数据,一开始的时候就初始化很多页面,有的甚至进入页面的时候,会造成缓慢的现象,下面我们就针对这个问题做一下 ...
- Python中的注解“@” 、Java 注解
https://blog.csdn.net/u013474436/article/details/75675113 https://blog.csdn.net/briblue/article/deta ...