——具体jsp聊天室项目体会Java反射
2016-11-16 19:10
239 查看
这是之前写的一篇博客,对java反射机制用法的基本介绍
http://blog.csdn.net/sinat_34803353/article/details/53150164
下面是聊天室项目的一个完整Baseservlet
反射写的一个基本servlet,以前就是,xie一个servlet需要继承HttpServlet,里面是doGet()或者doPost()方法,但它只能执行一个,一般情况下是一个模块对应一个servlet,这样比较清晰,或者像在struct2里面,一个模块的请求都会发到一个action里。这里,我们用反射模拟这种情况,一个servlet里面可以执行多个方法——怎么执行多个方法呢?就是在页面上,我们传了一个参数名,我们这里是叫method,值就是value值,假设value=”login”,然后我们就想让servlet执行login方法,那么这个是怎么做到的呢?——对,就是使用反射。
看这个Baseservlet,首先,我们会接收到一个method的value
下面是项目地址,大家如果相残考,可以下载完整代码
https://github.com/Sunshine-lhy/ChatRoom
http://blog.csdn.net/sinat_34803353/article/details/53150164
下面是聊天室项目的一个完整Baseservlet
反射写的一个基本servlet,以前就是,xie一个servlet需要继承HttpServlet,里面是doGet()或者doPost()方法,但它只能执行一个,一般情况下是一个模块对应一个servlet,这样比较清晰,或者像在struct2里面,一个模块的请求都会发到一个action里。这里,我们用反射模拟这种情况,一个servlet里面可以执行多个方法——怎么执行多个方法呢?就是在页面上,我们传了一个参数名,我们这里是叫method,值就是value值,假设value=”login”,然后我们就想让servlet执行login方法,那么这个是怎么做到的呢?——对,就是使用反射。
看这个Baseservlet,首先,我们会接收到一个method的value
package cn.utils; 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 BaseServlet extends HttpServlet { /* * 它会根据请求中的m,来决定调用本类的哪个方法 */ protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); res.setContentType("text/html;charset=utf-8"); // 例如:http://localhost:8080/demo1/xxx?method=login String methodName = req.getParameter("method");// 它是一个方法名称 // 当没用指定要调用的方法时,那么默认请求的是execute()方法。 if(methodName == null || methodName.isEmpty()) { methodName = "execute"; } // ①得到字节码文件 Class c = this.getClass(); try { // ②通过方法名称获取方法的反射对象 Method m = c.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); //③ 反射方法目标方法,也就是说,如果methodName为login,那么就调用login方法。 String result = (String) m.invoke(this, req, res); // ④通过返回值完成请求转发 if(result != null && !result.isEmpty()) { req.getRequestDispatcher(result).forward(req, res); } } catch (Exception e) { throw new ServletException(e); } } }
下面是项目地址,大家如果相残考,可以下载完整代码
https://github.com/Sunshine-lhy/ChatRoom
相关文章推荐
- JSP应用 java反射简单实现
- Fckeditor和JSP(Java)项目的整合实例
- 关于jsp web项目中的javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/el/ELResolver错误
- WEB项目中出现The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in ei问题的解决方法
- java项目中的.project 和 .classpath文件的具体作用
- [转载]让PHP支持大型项目-构建JSP、PHP与JAVA融为一体的开发环境
- java,jsp servlet mysql ssh毕业设计项目开发源码经验之谈
- java反射具体代码
- 如何解决在java开发中java代码和jsp页面中出现路径因为项目名称的变化(也就是应用名)的问题?
- jsp 获取项目路径,java获取项目路经
- 实习了,不得不放弃asp,学java!公司里要用jsp开发项目!
- Fckeditor和JSP(Java)项目的整合实例
- 如何实现java,jsp,ajax编写的在线聊天室啊?
- 关于JAVA EE项目在WEB-INF目录下的jsp页面如何访问WebRoot中的CSS和JS文件
- Myeclipse更新整个项目后,有时java或jsp文件,会提示错误,找不到类路径等问题。
- 个人做Java项目的体会
- JavaWeb物流管理项目开发(JSP、Servlet、JDBC):3、环境搭建
- jsp(JAVA)伪静态的具体设置过程
- 关于JAVA EE项目在WEB-INF目录下的jsp页面如何访问WebRoot中的CSS和JS文件
- 在jsp中 获取项目路径和在java中获取项目路径