模仿struts1的servlet页面分发
2011-10-13 19:19
190 查看
DispatchServlet类:package com.zzz.jbpm.web;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 分发请求,处理中文,控制登录
*
*/
public abstract class DispatchServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
System.out.println("DispatchServlet uri:" + request.getRequestURI());
// jbpm.do:index
String method = request.getParameter("method");
System.out.println("DispatchServlet method:" + method);
if (method == null || "".equals(method.trim())) {
index(request, response);
return;
}
dispatchMethod(request, response, method);
}
protected HashMap methods = new HashMap();
protected Class clazz = this.getClass();
protected Class[] types = { HttpServletRequest.class, HttpServletResponse.class };
// 实现回调子类的公共方法
private void dispatchMethod(HttpServletRequest request, HttpServletResponse response, String name) throws ServletException, IOException {
Method method = null;
try {
synchronized (methods) {
method = (Method) methods.get(name);
if (method == null) {
method = clazz.getMethod(name, types);
methods.put(name, method);
}
}
} catch (Exception e) {
throw new RuntimeException(clazz.getSimpleName() + "子类没有找到对应的方法:" + e.getMessage());
}
try {
Object[] args = { request, response };
method.invoke(this, args);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("不能成功调用子类的方法:" + e.getMessage());
}
}
// 没有输入method,默认访问此方法
protected abstract void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
使用方法:
public class DeployServlet extends BaseServlet { @Override protected void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//TODO }
相关文章推荐
- struts中调用servlet动态生成柱状图到jsp页面
- Cookie - Servlet,Struts中设置cookies Jsp页面接收取cookies
- 通过struts和servlet实现404默认页面跳转
- 模仿struts:servlet
- struts action 页面跳转并传参数值
- 从servlet跳转至jsp页面
- servlet页面跳转
- Servlet中请求数据的传递、请求转发与页面跳转
- servlet使用声明式异常处理指定错误跳转页面,ie下无法正常显示
- struct2学习(一)—struts全局结果页面,局部结果页面,转发和重定向
- 【重温】_JSP与Servlet重温笔记,要点概述,jsp_servlet简单登录页面
- Servlet页面登录的数据库验证程序(一)
- JSP/Servlet基础——登录成功后返回之前的页面
- Servlet页面注册用户的小程序(一)
- Servlet学习笔记(六)-----数据共享和页面跳转
- servlet通过URL传参到jsp页面出现乱码问题的解决
- jsp 、 java 、 servlet 页面获取路径的方法
- Struts中ActionContext和ServletActionContext的比较
- struts问题-Error instantiating servlet class org.apache.struts.action.ActionServlet
- 严重: Servlet.service() for servlet jsp threw exception The Struts dispatcher cannot be found.