jsp做成mvc模式的代码


package
cn.bd.house.web; public interface Action { /** * 所有的具体Action实现这个接口 * @param request 请求对象 * @param response 应答对象 * @return :结果页面 */ public String execute(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response); }
package cn.bd.house.web;


/**
 * 根据action名字,创建action对象
 * @author Administrator
 *
 */
public class ActionFactory {

    //单例模式:不需要创建对象  
    private ActionFactory(){  
    }  
    //单实例访问方法,得到ActionFactory对象  
    public static ActionFactory getActionFactory(){  
        if(af == null){  
            af = new ActionFactory();  
        }  
        return af;  
    }  
    /** 
     * 根据具体的Action类名字创建Action对象 
     * @param ActionClassName :具体的Action类全名 
     * @return:Action类型对象 
     */  
    public Action getAction(String ActionClassName){  
        Action action = null;  
        try{  
            action = (Action) Class.forName(ActionClassName).newInstance();  
        }catch(Exception e){  
            e.printStackTrace();  
        }  
        return action;  
    }  
 
    private static ActionFactory af; 
    
}

只有一个sevlet

package cn.bd.house.web;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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();
        
        //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);        
        }
    }
}

web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>



  <servlet>

    <description>This is the description of my J2EE component</description>

    <display-name>This is the display name of my J2EE component</display-name>

    <servlet-name>ControlServlet</servlet-name>

    <servlet-class>cn.bd.house.web.ControlServlet</servlet-class>
    
    <init-param>
              <!-- 页面请求的地址-->
        <param-name>LoginAction</param-name>  
               <!-- 处理action的后台Action地址-->
        <param-value>cn.bd.house.web.LoginAction</param-value>   
    </init-param>
    
    <init-param>   
        <param-name>RegisterAction</param-name>   
        <param-value>cn.bd.house.web.RegisterAction</param-value>
    </init-param>

  </servlet>


  

  <servlet-mapping>

    <servlet-name>ControlServlet</servlet-name>

    <!-- <url-pattern>/ControlServlet</url-pattern> -->
    <url-pattern>*.do</url-pattern>

  </servlet-mapping>


  
  <!-- <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list> -->
</web-app>
原文地址:https://www.cnblogs.com/taobd/p/6181384.html