Controller类
package mvc;
import javax.servlet.http.HttpServletRequest;
import entity.User;
public class Controller {
@RequestMapping("/login.do")
public String showLogin() {
return "login";
}
@RequestMapping("/handle_login.do")
public String handleLogin(User user) {
System.out.println(user);
return "reg";
}
@RequestMapping("/reg.do")
public String showRed(HttpServletRequest req) {
return "reg";
}
}
DispatchServlet类
package mvc;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class DispatchServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private HandlerMapping hm;
@Override
public void init() throws ServletException {
hm = new HandlerMapping();
SAXReader read = new SAXReader();
String path = getInitParameter("config");
InputStream in = getClass().getClassLoader().getResourceAsStream(path);
try {
Document doc = read.read(in);
Element root = doc.getRootElement();
List<Element> bs = root.elements("bean");
for(Element b : bs) {
String className = b.attributeValue("class");
hm.parseController(className);
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String contextPath = request.getContextPath();
String path = request.getRequestURI();
System.out.println(path);
Handler handler = hm.getHandler(path.substring(contextPath.length()));
try {
if(handler!=null) {
path = handler.execute(request);
path = "/WEB-INF/jsp/"+path+".jsp";
request.getRequestDispatcher(path).forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("系统错误!");
}
}
}
EncodingFilter类
package mvc;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class EncodingFilter implements Filter {
public EncodingFilter() {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
public void init(FilterConfig fConfig) throws ServletException {
}
}
Handler类
package mvc;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
public class Handler {
private Object controller;
private Method me;
public Handler() {
}
public Handler(Object controller, Method me) {
super();
this.controller = controller;
this.me = me;
}
public String execute(HttpServletRequest req) throws Exception{
int count = me.getParameterCount();
if(count==1) {
Class<?>[] classes = me.getParameterTypes();
for(Class cls : classes) {
System.out.println("参数完全限定名:"+cls.getName());
if(cls.getName().equals("javax.servlet.http.HttpServletRequest")) {
return (String) me.invoke(controller,req);
}
Method[] mes = cls.getDeclaredMethods();
Object o = cls.newInstance();
Map<String,Method> map = new HashMap<String,Method>();
for(Method me : mes) {
map.put(me.getName(), me);
}
Map<String, String[]> map2 = req.getParameterMap();
Set<String> keySet = map2.keySet();
for(String key : keySet) {
System.out.println("属性名:"+key);
String par = req.getParameter(key);
String last = key.substring(1);
String fir = String.valueOf(key.charAt(0)).toUpperCase();
key = fir+last;
Method me = map.get("set"+key);
me.invoke(o, par);
}
return (String) me.invoke(controller, o);
}
}
return (String) me.invoke(controller);
}
}
HandlerMapping类
package mvc;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import javax.security.auth.message.callback.PrivateKeyCallback.Request;
public class HandlerMapping {
private Map<String,Handler> map = new HashMap<String,Handler>();
public void parseController(String className) {
try {
Class<?> cls = Class.forName(className);
Method[] mes = cls.getDeclaredMethods();
for(Method me : mes) {
RequestMapping ann = me.getAnnotation(RequestMapping.class);
if(ann!=null) {
String value = ann.value();
map.put(value, new Handler(cls.newInstance(),me));
}
}
System.out.println(map);
System.out.println("初始化完毕!");
} catch (Exception e) {
e.printStackTrace();
}
}
public Handler getHandler(String url) {
return map.get(url);
}
}
RequestMapping注解
package mvc;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target(METHOD)
public @interface RequestMapping {
String value();
}
mvc.xml文件内容
<beans>
<bean class="mvc.Controller"></bean>
</beans>