一个servlet类反射相应实现servlet功能的普通类
来源:互联网 发布:小三转正后的婚姻知乎 编辑:程序博客网 时间:2024/06/08 12:00
package com.framework;
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 ActionServlet extends HttpServlet {
String actionPackage="";
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
System.out.println("===进入 "+this.getClass().getName());
String url = request.getRequestURI();
String actionName = "";
String methodName = "";
try{
System.out.println("===请求是: "+ url);
actionName = url.substring(url.lastIndexOf('/')+1, url.lastIndexOf('.'));
System.out.println("===类名是: "+ actionName);
methodName = url.substring(url.lastIndexOf('.')+1);
System.out.println("===方法是: "+methodName);
}catch(Exception e){
throw new ActionException("===不合法的url:["+url+"]/t"+e.getMessage(),e.getCause());
}
Object action = findAction(actionName);
System.out.println("/t===获得Action:"+ action);
Method method = findMethod(action,methodName);
System.out.println("/t===获得方法:"+method);
//调用 javaBean
ActionForward returnValue = (ActionForward)method.invoke(action, request,response);
if(null == returnValue) throw new ActionException("===["+actionName+"]的["+methodName+"]没有返回值!");
System.out.println("/t===跳转的url:"+returnValue.getContent());
//负责 跳转
if(returnValue.getType()==ActionForward.redirect_true){
response.sendRedirect(request.getContextPath()+returnValue.getContent());
}else if(returnValue.getType() == ActionForward.redirect_false){
request.getRequestDispatcher(returnValue.getContent()).forward(request, response);
}else{
response.getWriter().print(returnValue.getContent());
}
}catch (Exception e) {
e.printStackTrace();
}
}//doGet()
private Object findAction(String acName)throws Exception{
Object action = null;
String actionPath = actionPackage+"."+acName;
try{
Class clz = Class.forName(actionPath);//加载类 //UsersAction action;
action = clz.newInstance(); //action = new UsersAction();
}catch(Exception e){
throw new ActionException("===给定的路径["+actionPath+"]不能得到类实例/t"+e.getMessage(),e.getCause());
}
return action;
}//findAction()
private Method findMethod(Object action,String mdName) throws Exception{
Method md = null;
try{
md = action.getClass().getMethod(mdName, HttpServletRequest.class,HttpServletResponse.class);
}catch(Exception e){
throw new ActionException("===类型["+action.getClass()+"]不那得到["+mdName+"(HttpServletRequest,HttpServletResponse)]方法/t"+e.getMessage(),e.getCause());
}
return md;
}//findMethod()
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
@Override
public void init() throws ServletException {
this.actionPackage = this.getInitParameter("actionpackage");
}
}
- 一个servlet类反射相应实现servlet功能的普通类
- 使用普通的Servlet实现文件下载功能
- 【Servlet笔记】Servlet 和 普通类 读取配置文件的方法
- 利用反射机制实现servlet的分派功能
- Servlet的实现类
- servlet类的实现
- Servlet实现的代理服务器功能
- Servlet登陆功能的实现
- servlet分页功能的实现
- Servlet注册功能的实现
- jsp+javaBean+servlet+mysql完整的实现一个登录功能
- 五、用反射实现一个Servlet类里处理多个请求,不推荐重写service
- servlet和普通类获取资源文件的方法
- Servlet的实现类-GenericServlet
- 如何使用Servlet和filter实现:一个Servlet文件完成多个Servlet功能
- Servlet 实现定时器功能
- Servlet 实现下载功能
- Servlet实现下载功能
- 下雨天
- java模板引擎Freemarker
- 我的第一篇日志
- VC制作一个简单的播放器(只要几分钟)
- 部分常用JavaScript代码
- 一个servlet类反射相应实现servlet功能的普通类
- CColorDialog
- 协议适配器错误的原因
- 解决oem中任务调度部分输入正确windows用户名和密码不能执行任务的问题
- RD自身修养 生命不息,学习不止
- 学习XML八个重点
- 动画程序设计源代码
- 40条优化php代码的小实例
- c++结束