什么是自定义MVC框架?

来源:互联网 发布:淘宝举报中心网址 编辑:程序博客网 时间:2024/06/07 20:32

1.定义TotalDO接口

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface TotalDO {
public void execute(HttpServletRequest req, HttpServletResponse resp);
}

2.实现自定义TotalDO的接口(execute的实现)

public class LoginDO implements TotalDO{
public void execute(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("执行登录的业务逻辑");
String unumber=req.getParameter("unumber");
String upass=req.getParameter("upass");
Users u=new Users(unumber, upass);
UsersBizImp usersBizImp=new UsersBizImp();
boolean b=usersBizImp.login(u);
if(b){
try {
req.getRequestDispatcher("index.jsp").forward(req, resp);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else{
System.out.println("登录失败!!!!");
}
}
}

3、实现Controller类,调用execute方法

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//1.获取请求路径
String uri=req.getRequestURI();
System.out.println("获取请求的路径:"+uri);
//2.截取
//2.1获取最后一个斜杠的位置
int lastXiePostion=uri.lastIndexOf("/");
//2.2获取最后一个点的位置
int lastPointPostion=uri.lastIndexOf(".");
uri=uri.substring(lastXiePostion+1, lastPointPostion);
System.out.println("请求路径:"+uri);

//从application中获取properties
Properties properties=(Properties) req.getSession().getServletContext().getAttribute("properties");
//3.2根据键获取值
String className=properties.getProperty(uri);
System.out.println("获取到相对应的全限定名为:"+className);
Properties propertiesObject=(Properties) req.getSession().getServletContext().getAttribute("propertiesObject");
TotalDO total=(TotalDO) propertiesObject.get(className);
//3.3根据类的全限定名 来 实例化 该对象
try {
if(total==null){
System.out.println("初始化对象");
total=(TotalDO)Class.forName(className).newInstance();
//必须存放
propertiesObject.put(className, total);
}
total.execute(req, resp);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

4.先在init方法中建立一个池(Properties),再在dopost方法中判断,以保证每一个类只会 被实例化一次

@Override
public void init(ServletConfig config) throws ServletException {
//3.加载配置文件config.properties
Properties properties=new Properties();
Properties propertiesObject=new Properties();
//3.1获取服务器的路径
String serverPath=config.getServletContext().getRealPath("/");
try {
FileInputStream fis=new FileInputStream(serverPath+"WEB-INF/config.properties");
try {
properties.load(fis);
//将properties存放到application中
config.getServletContext().setAttribute("properties", properties);
config.getServletContext().setAttribute("propertiesObject", propertiesObject);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

5、配置的总servlet

  <servlet>
  <servlet-name>totalServlet</servlet-name>
  <servlet-class>com.zking.servlet.TotalServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>totalServlet</servlet-name>
  <url-pattern>*.do</url-pattern>
  </servlet-mapping>