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的一些简单效果的更多相关文章

  1. 自己实现的简单MVC框架(类似Struts2+Spring)

    一.框架简介 本框架是一个类似于Struts2+Spring的框架,目的在于个人钻研和技术分享,将流行技术框架Struts2.Spring中使用到的主要技术以较为简化的方式实现出来,给大家一个更直观的 ...

  2. 十三、事务、连接池 、ThreadLocal 、BaseServlet自定义Servlet父类 、 DBUtils à commons-dbutils

    l 事务 l 连接池 l ThreadLocal l BaseServlet自定义Servlet父类(只要求会用,不要求会写) l DBUtils à commons-dbutils 事务 l 事务的 ...

  3. struts2和servlet同时用(访问servlet时被struts2过滤器拦截问题的解决)

    在同一个项目中间,如果既用到servlet有用了struts2的框架,运行项目时可能无法正常使用servlet,原因是在配置struts2的核心控制器时<url-pattern>/*< ...

  4. 【WEB小工具】BaseServlet—一个Servlet处理多个请求

    package cn.itcast.test.web.servlet; import java.io.IOException; import java.io.PrintWriter; import j ...

  5. Duilib实现类似电脑管家扫描目录效果

    实现原理: 1.后台开线程遍历目录,遍历出一个文件路径在界面上更新显示(通过发消息通知主界面) 2.需要扩展一下Duilib控件,在此我扩展了CLabelUI,重写了PaintText函数 扩展控件的 ...

  6. WPF如何实现类似iPhone界面切换的效果(转载)

    WPF如何实现类似iPhone界面切换的效果 (version .1) 转自:http://blog.csdn.net/fallincloud/article/details/6968764 在论坛上 ...

  7. Adobe Edge Animate –地球自转动画的实现,类似flash遮罩层的效果

    Adobe Edge Animate –地球自转动画的实现,类似flash遮罩层的效果 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权和原文地址. 目前Edge的功能尚 ...

  8. PHP实现类似题库抽题效果

    PHP实现类似题库抽题效果 大家好,我顾某人又回来了,最近学了一点PHP,然后就想写个简单小例子试试,于是就写了一个类似于从题库抽题的东西,大概就是先输入需要抽题的数量,然后从数据库中随机抽取题目. ...

  9. 利用反射优化Servlet抽象出父类BaseServlet

    在编写servlet的时候发现每个servlet里面的doPost方法都如: protected void doPost(HttpServletRequest request, HttpServlet ...

随机推荐

  1. Java Jsoup Spider抓取数据入库

    这里从车商网上进行数据抓取,请保持良好的职业道德不要将数据用于商业途径.工信部官网有汽车方面的公告目录,那里有最全的pdf或word数据,鉴于word和pdf解析的繁琐和耗时,我暂时用这个网站的数据进 ...

  2. 可执行Jar包调用动态链接库(DLL/SO)

    踩过了很多的坑,查了很多资料,在此记录一下,以SpringBoot项目为基础. Maven加入JNA依赖 <!-- JNA start --> <dependency> < ...

  3. Jquery EasyUI弹出窗体

    $("#btnCreate").click(function () { $("#modalwindow").html("<iframe widt ...

  4. 目标检测之行人检测(Pedestrian Detection)---行人检测之简介0

    一.论文 综述类的文章 [1]P.Dollar, C. Wojek,B. Schiele, et al. Pedestrian detection: an evaluation of the stat ...

  5. Unity3D研究院编辑器之自定义默认资源的Inspector面板

    比如编辑模式下对场景或者特定文件夹有一些操作可以在这个面板里来完成. 代码如下: using System.Collections; using System.Collections.Generic; ...

  6. 3.11 T-SQL语句

    T-SQL语句 1.创建表create table Car     --创建一个名字是Car的表-- ( Code varchar(50) primary key, --第一列名字是Code 数据类型 ...

  7. 各种RTMP直播流播放权限_音视频_数据花屏_问题检测与分析工具EasyRTMPClient

    之前的一篇博客<网络摄像机IPCamera RTSP直播播放网络/权限/音视频数据/花屏问题检测与分析助手EasyRTSPClient>,我们介绍了RTSP流的检测和分析工具EasyRTS ...

  8. php总结4——数组的定义及函数、冒泡排序

    4.1 数组的定义 数组:变量存储的有序序列. 索引数组:下标为数字的数组.  $数组名称(下标)    下标从0开始的数字. 直接定义: $arr[0]=123; $arr[1]="chi ...

  9. win7计划任务定时执行PHP脚本设置图解

    做php开发的朋友有时候会希望自己的电脑能每天定时的运行一下某个脚本,但定时执行php脚本这种概念似乎多半是在linux中才提到,下面这篇文章主要和大家分享一下在win7下如何设置计划任务,以实现定时 ...

  10. 【zabbix】微信告警消息模版

    下面给出了一个zabbix微信告警消息的模版, 消息最后加上#号和短横线的设计有两个原因: 1,zabbix的微信告警消息总是被截断,比如最后一个告警时间,如果没有最后一行#号,在微信上看的时候时间不 ...