自定义MVC
下面我来说一下自定义大概步骤以登录注册作为例子
- 只有一个总Servlet来控制(例如:登录 注册),web.xml
- 在Servlet获取请求的URI(只有项目名+逻辑业务:System.out.println(req.getRequestURI()); 全称:System.out.println(req.getRequestURL());)截取
- 分别创建class文件(对应xx.do), 接口。class文件分别实现接口
- 创建一个配置file文件后缀名.properties(里面要写的是相当于map集合 例如:请求URI的xx.do=相对应的类)
- 优化代码
一:只有一个总Servlet来控制(例如:登录 注册),web.xml
登录(login.jsp文件)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>登录页面</h1>
<form action="login.do" method="post">
Uname:<input type="text" name="uname" /><br/>
Upass:<input type="text" name="upass" /><br/>
<input type="submit" value="登录">
</form>
</body>
</html>
注册(reginter.jsp文件)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>注册页面</h1>
<form action="reginter.do" method="post">
Uname:<input type="text" name="uname" /><br/>
Upass:<input type="text" name="upass" /><br/>
Uset:<input type="text" name="uset" /><br/>
Uage:<input type="text" name="uage" /><br/>
<input type="submit" value="注册">
</form>
</body>
</html>
web.xml
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>com.zking.servlet.TotalServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
那第一步完成了。
二:在Servlet获取请求的URI(只有项目名+逻辑业务:System.out.println(req.getRequestURI()); 全称:System.out.println(req.getRequestURL());)截取
String uri=req.getRequestURI();
uri=uri.substring(uri.lastIndexOf("/")+1, uri.lastIndexOf("."));
三:分别创建class文件(对应xx.do), 接口。class文件分别实现接口
login.java
package com.zking.servlet;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Login implements Total{
public void exrcute(HttpServletRequest req, HttpServletResponse resp){
try {
req.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String uname = req.getParameter("uname");
String upass = req.getParameter("upass");
if ("yu".equals(uname)&&"123".equals(upass)) {
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
System.out.println("执行登录的业务逻辑");
}
}
Reginter.java
package com.zking.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Reginter implements Total {
public void exrcute(HttpServletRequest req, HttpServletResponse resp){
System.out.println("执行注册的业务逻辑");
}
}
Total.java(接口)
package com.zking.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
自定义MVC具体代码
public interface Total {
public void exrcute(HttpServletRequest req, HttpServletResponse resp);
}
四:创建一个配置file文件后缀名.properties(里面要写的是相当于map集合 例如:请求URI的xx.do=相对应的类的全限定名)放入到WEB-INF文件夹下
config.properties
login=com.zking.servlet.Login
reginter=com.zking.servlet.Reginter
login=com.zking.servlet.Login
reginter=com.zking.servlet.Reginter
五:优化代码
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
//获取服务器的相对路径
String path = config.getServletContext().getRealPath("/");
//读取config.properties
Properties properties=new Properties();
Properties objeclPool=new Properties();//对象池
try {
properties.load(new FileInputStream(path+"/WEB-INF/config.properties"));
//将properties保存到Application中
config.getServletContext().setAttribute("config",properties);
config.getServletContext().setAttribute("total",objeclPool);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
加载配置文件 放入Application中 下次再从Application中读取
从Application中读取
//从Application中取config.properties
Properties properties = (Properties) req.getSession().getServletContext().getAttribute("config");
//从Application中获取对象池
Properties objeclPool = (Properties) req.getSession().getServletContext().getAttribute("total");