社区
Java EE
帖子详情
参数找方法是怎么实现的呢?
kill8108
2006-08-21 03:55:24
通过url中的一个参数method=add就可找到相对象的add方法了,这样怎么实现的呢?
...全文
237
10
打赏
收藏
参数找方法是怎么实现的呢?
通过url中的一个参数method=add就可找到相对象的add方法了,这样怎么实现的呢?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
10 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
kill8108
2006-08-24
打赏
举报
回复
谢谢了各位..........
lyxh_2003
2006-08-22
打赏
举报
回复
以前反射超慢,现在不知道快了点没有
terry_yip
2006-08-22
打赏
举报
回复
下面就是我之前做的代码,你参考一下吧。
public static void main(String args[])
{
TMain main=new TMain();
TWhiteMouse mouse=new TWhiteMouse();
Method[] method=mouse.getClass().getMethods();
for(int i=0;i<method.length;i++)
{
System.out.println(method[i].getName());
System.out.println(method[i].getDeclaringClass());
System.out.println(method[i].getClass());
}
try
{
main.doIt();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doIt() throws Exception
{
TWhiteMouse mouse=new TWhiteMouse();
Method method1=mouse.getClass().getMethod("showStr",null);
Method method2=mouse.getClass().getMethod("setName",new Class[]{String.class});
Method method3=mouse.getClass().getMethod("showName",null);
Method method4=mouse.getClass().getMethod("getName",null);
method1.invoke(mouse,null);
method2.invoke(mouse, new String[]{"哈哈"});
method3.invoke(mouse, null);
System.out.println("name:"+method4.invoke(mouse, null)+"");
}
----------------------------------------------------------------------
package refect;
public class TWhiteMouse
{
String id,name;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public void showStr()
{
System.out.println("###########");
}
public void showName()
{
System.out.println(this.name);
}
}
LMin_CZY
2006-08-22
打赏
举报
回复
第一次执行是基于反射
以后就是直接从内存里根据方法名来调该方法执行了
用户的servlet需要先继承该BaseServlet
LMin_CZY
2006-08-22
打赏
举报
回复
//Config对象
package com.lmin.framework.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Config {
public HttpServletRequest request;
public HttpServletResponse response;
public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public HttpServletResponse getResponse() {
return response;
}
public void setResponse(HttpServletResponse response) {
this.response = response;
}
}
LMin_CZY
2006-08-22
打赏
举报
回复
反射慢可以解决地
贴一个我写的Servlet
package com.lmin.framework.servlet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lmin.framework.dao.BaseDAO;
import com.lmin.framework.exception.DefaultException;
import com.lmin.framework.fenye.Pager;
import com.lmin.test.StudentDAO;
public abstract class BaseServlet extends HttpServlet{
public static Map methodMap = new HashMap();
protected boolean redirect = true; //请求转发 为false表示重定向
public void setRirect(boolean type) {
redirect = type;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
String path = null;
try {
Config config = new Config();
config.setRequest(request);
config.setResponse(response);
path = perform(action,config);
//请求转发操作
if(redirect) {
RequestDispatcher rs = request.getRequestDispatcher(path);
rs.forward(request,response);
} else {
//请求重定向操作
response.sendRedirect(path);
}
} catch (DefaultException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
/**
* 获得方法对象
* @param methodName
* @return
* @throws SecurityException
* @throws NoSuchMethodException
*/
protected Method getMethod(String action) throws SecurityException, NoSuchMethodException {
String key = this.getClass().getName() + "." + action;
Method method = (Method) methodMap.get(key);
if(method == null) {
synchronized(methodMap) {
Class[] clazz = new Class[1];
clazz[0] = Config.class;
method = this.getClass().getDeclaredMethod(action,clazz);
methodMap.put(key,method);
}
}
return method;
}
/**
* 执行方法操作
* @param methodName
* @return
* @throws DefaultException
*/
public String perform(String methodName, Config config) throws DefaultException {
Method method = null;
try {
method = getMethod(methodName);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
String forward = null;
try {
Object[] objects = new Object[1];
objects[0] = config;
forward = (String) method.invoke(this,objects);
} catch (IllegalArgumentException e) {
e.printStackTrace();
throw new DefaultException(e);
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new DefaultException(e);
} catch (InvocationTargetException e) {
e.printStackTrace();
throw new DefaultException(e);
}
return forward;
}
/**
* 分页查询
* @param config
* @param sqlWhere
* @param dao
*/
public void doPage(Config config, String sqlWhere, BaseDAO dao) {
String pageMethod;
pageMethod=config.getRequest().getParameter("pageMethod");
List list = new ArrayList();
if(pageMethod==null || pageMethod.equals("null")) { //第一次读取数据
int size = dao.querySize(sqlWhere);
Pager pager = new Pager(size);
list = dao.query(sqlWhere,0l,pager.getPageSize());
pager.first();
//将pager信息和sql信息存放在session中管理
//这里需要改进
//可以设置一个List来存放各个分页操作的pager对象和sql对象(将pager对象和sql对象封装为一个对象)
//List限制一个长度超过就将队首元素删除
//防止session元素控制分页信息出错或信息疯长
config.getRequest().getSession().setAttribute("pager",pager);
config.getRequest().getSession().setAttribute("sql",sqlWhere);
} else {
Pager pager = (Pager) config.getRequest().getSession().getAttribute("pager");
String sql = (String) config.getRequest().getSession().getAttribute("sql");
if("first".equals(pageMethod)) {
list = dao.query(sql,0l,pager.getPageSize());
pager.first();
} else if("previous".equals(pageMethod)) {
pager.previous();
list = dao.query(sql,pager.getStartRow(),pager.getPageSize());
} else if("next".equals(pageMethod)) {
pager.next();
list = dao.query(sql,pager.getStartRow(),pager.getPageSize());
} else if("last".equals(pageMethod)) {
pager.last();
list = dao.query(sql,pager.getStartRow(),pager.getPageSize());
}
}
config.getRequest().setAttribute("result",list);
}
}
zhmt
2006-08-22
打赏
举报
回复
路过,友情up...
kill8108
2006-08-21
打赏
举报
回复
是的了,各位兄弟有没有些好代码呢?
自己写了个,总觉得速度等的问题都不行呢?
kingofworl
2006-08-21
打赏
举报
回复
反射
flyforlove
2006-08-21
打赏
举报
回复
reflect
自己动手,
实现
《天气之子》滤镜REDUX
为了有效解决这类问题,本课程将基于Pytorch
实现
天空对象的语义分割,并进一步扩展到环境构建、数据集准备、
参数
调节和运行部署等方面内容,最终能够
实现
远超传统算法的运行效果! 具体内容包括: &...
什么是序列化?反序列化? 场景使用? 怎么
实现
???
怎么
实现
??? 或许我们平时大概知道, 嗯,序列化,就是
实现
Serializable 接口, 那再问你,怎么
实现
的, 或许你就有点懵逼,这篇, 让我们来 look 一 look 吧~~~~~ 目录 一.什么是序列化?反序列化? 二.为什么要序列化和...
为什么要
实现
序列化?
工作中我们经常在进行持久化操作和返回数据时都会使用到javabean来统一封装
参数
,方便操作,一般我们也都会
实现
Serializable接口,那么问题来了,首先:为什么要进行序列化;其次:每个实体bean都必须
实现
...
Java中为什么静态
方法
不能被重写?为什么静态
方法
不能隐藏实例
方法
?
前期准备首先理解重写的意思,重写就是子类中对父类的实例
方法
进行重新定义功能,且返回类型、
方法
名以及
参数
列表保持一致,且对重写
方法
的调用主要看实际类型。实际类型如果
实现
了该
方法
则直接调用该
方法
,如果没有...
Java程序利用main函数中args
参数
实现
参数
的传递
Java程序利用main函数中args
参数
实现
参数
的传递 1.运行Java程序的同时,可以通过输入
参数
给main函数中的接收
参数
数组args[],供程序内部使用!即当你在Java命令行后面带上
参数
,Java虚拟机就直接把它们存放到了main...
Java EE
67,513
社区成员
225,879
社区内容
发帖
与我相关
我的任务
Java EE
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
复制链接
扫一扫
分享
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章