<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">新建项目,结构如下:</span>
具体类
index.jsp 登陆页面,触发servlet
<%@ 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>
<form action="http://localhost:8080/mvc_1/servlet/actionServlet" method="post"/>
用户名:<input type="text" name="username" /><br/>
密码:<input type="password" name="password"/><br/>
<input type="hidden" name="sign" value="mvc.form.LoginForm"/>
<input type="submit" value="提交" />
</form>
</body>
</html>
rigister.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>注册</title>
</head>
<body>
<form action="http://localhost:8080/mvc_1/servlet/actionServlet" method="post"/>
用户名:<input type="text" name="username" /><br/>
密码:<input type="password" name="password"/><br/>
姓名:<input type="text" name="lastname" /><br>
<input type="hidden" name="sign" value="mvc.form.registerForm"/>
<input type="submit" value="提交" />
</form>
</body>
</html>
<?xml version="1.0" encoding="gbk"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>mvc_1</display-name>
<servlet>
<servlet-name>actionServlet</servlet-name>
<servlet-class>mvc.ActionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>actionServlet</servlet-name>
<url-pattern>/servlet/actionServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
ActionServlet
package mvc;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mvc.control.LoginAction;
import mvc.util.FullBean;
public class ActionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public ActionServlet() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Object o=FullBean.full(request);
BaseForm b=(BaseForm)o;
LoginAction l=new LoginAction();
String s=l.execute(b);
Action action=null;
String sign=request.getParameter("sign");
Map<String,String> map=ActionMapping.getMap();
try{
Class clazz=Class.forName(map.get(sign));
action=(Action)clazz.newInstance();
}catch(Exception e){
e.printStackTrace();
}
String result=action.execute(b);
PrintWriter out=response.getWriter();
out.print(result);
out.flush();
out.close();
}
}
Action 定义一个接口,让所有action实现此接口,这样可以通过接口调用。
package mvc;
public interface Action {
public String execute(BaseForm form);
}
BaseForm 基类,实体Bean集成此类,这里用类的多态
package mvc;
public abstract class BaseForm {
public BaseForm(){
}
}
ActionMapping
package mvc;
import java.util.HashMap;
import java.util.Map;
public class ActionMapping {
public static Map<String,String> getMap(){
Map <String,String>map=new HashMap<String,String>();
map.put("mvc.form.LoginForm", "mvc.control.LoginAction");
map.put("mvc.form.registerForm", "mvc.control.RegisterAction");
return map;
}
}
FullBean 根据反射把类的所有属性赋值
package mvc.util;
import java.lang.reflect.Field;
import javax.servlet.http.HttpServletRequest;
public class FullBean {
public FullBean(){
}
public static Object full(HttpServletRequest request){
Object o=null;
try {
Class clazz=Class.forName(request.getParameter("sign"));
o=clazz.newInstance();
Field [] fields=clazz.getDeclaredFields();
for(Field f:fields){
f.setAccessible(true);
f.set(o,request.getParameter(f.getName()));
f.setAccessible(false);
}
} catch (Exception e) {
e.printStackTrace();
}
return o;
}
}
LoginForm
package mvc.form;
import mvc.BaseForm;
public class LoginForm extends BaseForm{
public LoginForm(){
}
public LoginForm(String username,String password){
this.username=username;
this.password=password;
}
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String toString(){
return "Login--username:"+username+" password:"+password;
}
}
LoginAction
package mvc.control;
import mvc.Action;
import mvc.BaseForm;
public class LoginAction implements Action{
@Override
public String execute(BaseForm form) {
System.out.println(form);
return form.toString();
}
}