一、导入jar包
见 http://pan.baidu.com/s/1dE9kMJZ
二、导入c3p0并修改值
c3p0.driverClass=com.mysql.jdbc.Driver c3p0.jdbcUrl=jdbc:mysql://localhost:3306/store c3p0.user=root c3p0.password=123456
三、导入工具类
见 http://pan.baidu.com/s/1miutJXe
有关这些工具类的分析请转向http://www.cnblogs.com/Michael2397/p/7633395.html
四、通用servet
1、我们可以直接重写service方法即可,在service方法中写上面的判断代码即可
以前
只定义一个Userservlet重写doget和dopost例如添加用户的时候以前: /store/addUser现在: /store/user?method=add修改用户的时候以前: /store/updateUser现在: /store/user?method=update 初步改进 在userservlet中doget方法中Stirng m=request.getParameter("method");if("add".equals(m)){add(HttpServletRequest,HttpServletResponse)}else if("update".equals(m)){update(HttpServletRequest,HttpServletResponse)}else if(){}
最终改进:
1、BaseServlet extends HttpServlet{}
2、在baseservlet中重写service方法1.获取请求的方法2.找到相应的子类 父类中的this代表的是子类Class clazz=this.getClass()3.子类调用方法通过方法名称 获取一个指定的方法 Method m=clazz.getMethod(方法名,HttpServletRequest.class,HttpServletResponse.class)方法执行m.invoke(this,HttpServletRequest,HttpServletResponse);
3、其他的servlet继承baseServlet
2、请求转发 重定向 打印数据改进
以前
在servlet中 我们执行方法之后无非 请求转发 重定向 打印数据 请求转发 reuqest.getRequestDispatcher().forward()最终改进
继续完善我们baseservlet中service: 在第3步,方法执行返回值,现在我让所有的方法都返回一个字符串 add update字符串代表的是请求转发的路径若该方法不转发只需要返回一个nullString s=(String)m.invoke(this,HttpServletRequest,HttpServletResponse);if(s!=null){reuqest.getRequestDispatcher(s).forward(...);}
五、创建通用servlet
BaseServlet只需要重写service即可
1、新建Baseservlet
package com.louis.web.servlet;import java.io.IOException; import java.lang.reflect.Method;import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/*** 通用servlet*/ @WebServlet("/base") public class BaseServlet extends HttpServlet {@Overridepublic void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {try {//1获取子类,创建子类或调用子类的时候,this代表的是子类对象Class clazz = this.getClass();//2获取请求方法String m = request.getParameter("method");//3获取方法对象Method method = clazz.getDeclaredMethod(m, HttpServletRequest.class,HttpServletResponse.class);//4让方法执行,返回值为请求转发的路径String s = (String)method.invoke(this, request,response); //相当于 userservlet.add(request,response)//5判断s是否为空if(s!=null) {request.getRequestDispatcher(s).forward(request, response);}} catch (Exception e) {// TODO Auto-generated catch block e.printStackTrace();throw new RuntimeException();} }}
2、配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><servlet><description></description><display-name>BaseServlet</display-name><servlet-name>BaseServlet</servlet-name><servlet-class>com.louis.web.servlet.BaseServlet</servlet-class></servlet><servlet-mapping><servlet-name>BaseServlet</servlet-name><url-pattern>/base</url-pattern></servlet-mapping>
<error-page> //配置错误页面<error-code>500</error-code><location>/500.jsp</location></error-page><error-page><error-code>404</error-code><location>/404.jsp</location></error-page></web-app>
3、添加500.jsp,404.jsp
六、创建UserServlet
1、编写UserServlet
package com.louis.web.servlet;import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/*** 和用户相关的servlet*/public class UserServlet extends BaseServlet {public String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("userServlet的add方法执行了");return null;}}
2、配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><servlet><description></description><display-name>BaseServlet</display-name><servlet-name>BaseServlet</servlet-name><servlet-class>com.louis.web.servlet.BaseServlet</servlet-class></servlet><servlet-mapping><servlet-name>BaseServlet</servlet-name><url-pattern>/base</url-pattern></servlet-mapping> <servlet><description></description><display-name>UserServlet</display-name><servlet-name>UserServlet</servlet-name><servlet-class>com.louis.web.servlet.UserServlet</servlet-class></servlet><servlet-mapping><servlet-name>UserServlet</servlet-name><url-pattern>/user</url-pattern></servlet-mapping><error-page><error-code>500</error-code><location>/500.jsp</location></error-page><error-page><error-code>404</error-code><location>/404.jsp</location></error-page> </web-app>
3、在浏览器上验证http://localhost:8080/store/user?method=add
user:通过web.xml得到执行的是userServlet
method=add:因为userServlet是继承baseServlet,所以要先经过baseServlet,通过反射执行this.add方法(this指的是子类)
问题
问题1
解决:参考http://www.cnblogs.com/vivizhang/p/vivizhang.html
因为eclipse默认添加的@WebServlet("/ServletDemo")与web.xml中的冲突,删掉其中一个
2、this关键字在继承中的使用
参考:http://www.cnblogs.com/Michael2397/p/7633932.html
3、web.xml配置
参考:http://www.cnblogs.com/hxsyl/p/3435412.html
4、request.getRequestDispatcher().forward(request,response)和response.sendRedirect()的区别
参考:http://blog.csdn.net/uk8692/article/details/12865571