本文共 2725 字,大约阅读时间需要 9 分钟。
1.1 上次课内容回顾:
注解:* JDK中的三个注解:[/align] * 自定义注解: * @interface * 注解类型:基本类型,String,Class,注解,枚举,以上类型的一维数组. * 注解的反射:Servlet3.0 * 注解开发: * 文件上传: * 异步请求:动态代理: * Proxy.newProxyInstance(ClassLoader cl,Class[] interfaces,InvocationHandler handler); * 如何增强类中的某个方法:类加载器: * 引导类加载器: * 扩展类加载器: * 应用类加载器:
1.2 网上商城的实战的案例:1.2.1 需求:
1.2.2 数据库设计:1.2.3 网上商城用户模块的代码实现:(前台)1.2.3.1 编写一个通用的Servlet:传统方式的开发一个请求对应一个Servlet:这样的话会导致一个模块的Servlet过多,导致整个项目的Servlet都会很多.能不能做一个处理?让一个模块致用一个Servlet处理请求.注册:登录:激活:商品查询所有:商品查询某个:传统:public class UserServlet extends HttpServlet{ public void service(HttpServletRequest req,HttpServletResponse resp){ // 接收参数: String methodName = request.getParameter(“method”); if(“regist”.equals(methodName)){ regist(req,resp); }else if(“login”.equals(methodName)){ login(req,resp); } } public void regist(HttpServletRequest req,HttpServletResponse resp){ } public void login(HttpServletRequest req,HttpServletResponse resp){ }}public class ProductServlet extends HttpServlet{ public void service(HttpServletRequest req,HttpServletResponse resp){ // 接收参数: String methodName = request.getParameter(“method”); if(“findAll”.equals(methodName)){ findAll(req,resp); }else if(“findById”.equals(methodName)){ findById(req,resp); } } public void findAll(HttpServletRequest req,HttpServletResponse resp){ } public void findById(HttpServletRequest req,HttpServletResponse resp){ }}
反射:
public class BaseServlet extends HttpServlet{ public void service(HttpServletRequest req,HttpServletResponse resp){ // http://localhost:8080/store_2.0/UserServlet?method=regist // http://localhost:8080/store_2.0/ProductServlet?method=findAll String methodName = request.getParameter(“method”); // 获得Class: Class clazz = this.getClass(); Method method = clazz.getMethod(methodName,HttpServletRequest.class,HttpSerlvetResponse.class); String path = (String)method.invoke(this,req,resp); if(path != null){ req.getRequestDispatcher(path).forward(req,resp); } }}public class UserServlet extends BaseServlet { public String regist(HttpServletRequest req,HttpServletResponse resp){ return “/login.jsp”; } public String login(HttpServletRequest req,HttpServletResponse resp){ return “/index.jsp”; }}public class ProductServlet extends BaseServlet{ public String findAll(HttpServletRequest req,HttpServletResponse resp){ } public String findById(HttpServletRequest req,HttpServletResponse resp){ }}
转载于:https://blog.51cto.com/13517854/2125608