Java Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package cn.netjava.servlet;
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.netjava.action.Action; import cn.netjava.action.ActionFactory; public class ControlServlet extends HttpServlet{ private static final long serialVersionUID = 1L; protected void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{ //得到当前servlet的请求路径 String pathName = request.getServletPath(); //request.getContextPath(); //得到项目名字 System.out.println("pathName:"+pathName); //得到请求的Action名字 int index=pathName.indexOf('.'); String ActionName=pathName.substring(1,index); System.out.println("ActionName:"+ActionName); //获取运行时参数 String actionClassName=this.getInitParameter(ActionName); //得到Action对象 Action action=ActionFactory.getActionFactory().getAction(actionClassName); //执行Action的execute得到要返回的URL路径 String url = action.execute(request,response); if(url==null){ request.getRequestDispatcher("error.jsp").forward(request, response); }else { request.getRequestDispatcher(url).forward(request, response); } } } |
Java Code
1
2 3 4 5 6 7 8 9 10 11 12 |
package cn.netjava.action;
public interface Action { /** * 所有Action类继承这个接口 * @param request 请求对象 * @param response 响应对象 * @return 返回页面地址 */ public String execute(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response); } |
Java Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package cn.netjava.action;
public class ActionFactory { //单例模式不需要创建对象 private static ActionFactory af; private ActionFactory(){} public static ActionFactory getActionFactory() { if(af == null) { af=new ActionFactory(); } return af; } public Action getAction(String ActionClassName) { Action action =null; try { //利用反射根据类名创建对象 action =(Action)Class.forName(ActionClassName).newInstance(); }catch(Exception e) { e.printStackTrace(); } return action; } } |
Java Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package cn.netjava.action;
public class ActionFactory { //单例模式不需要创建对象 private static ActionFactory af; private ActionFactory(){} public static ActionFactory getActionFactory() { if(af == null) { af=new ActionFactory(); } return af; } public Action getAction(String ActionClassName) { Action action =null; try { //利用反射根据类名创建对象 action =(Action)Class.forName(ActionClassName).newInstance(); }catch(Exception e) { e.printStackTrace(); } return action; } } |
XML Code
1
2 3 4 5 6 7 8 9 10 11 12 13 |
<servlet>
<servlet-name>controlServlet</servlet-name> <servlet-class>cn.netjava.servlet.ControlServlet</servlet-class> <init-param> <param-name>loginAction</param-name> <param-value>cn.netjava.action.LoginAction</param-value> </init-param> </servlet> <servlet-mapping> <!-- 指定该servlet对应的地址 --> <servlet-name>controlServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> |
HTML Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <form action="loginAction.do" method="post"> <table border="1" cellspacing="0" cellpadding="10" width="70%" style="border:red soild;border-collapse:collapse"> <tr> <!-- 这里必须要设置空的value --> <td colspan="2" align="center">用户名: <input type="text" name="username" id="username" value="" /><br /></td> </tr> <tr> <td></td> </tr> <tr> <td colspan="2" align="center">密码: <input type="password" name="password" id="password" value="" /><br /></td> </tr> <tr> <th><input type="submit" value="登陆" /> <input type="checkbox" name="savePW" value="true" />记住密码 <input type="button" value="注册" onclick="location.href='register.jsp'" /></th> </tr> </table> </form> </body> </html> |