封装的类Users.java
package com.cj.pojo;
public class Users {
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;
}
}
servlet实现 有两个(BaseServlet.java和LoginServlet.java)
BaseServlet
package com.cj.servlet;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class BaseServlet
*/
@WebServlet("/BaseServlet")
public class BaseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public BaseServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
//当请求发送到action 之前
//调用MloginAction类中getModel()获取要将表单数据封装到哪个实例化的对象中
//获得到该对象之后,我们可以获得 类类型
//获得类 类型之后,获得类中的属性
//request,getParameters获得 表单提交的所有数据名
//从而获取值
//如果表单提交的name值与实体类中属性名一致
//那么我们将获得表单中的数据 封装到us对象当中去
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
//在程序运行时 获取当前类的getModel方法对象
Method method = this.getClass().getDeclaredMethod("getModel", null);
//通过invoke方法 调用本类中的该方法 获得实体类对象
Object object = method.invoke(this, null);
//通过实体类对象获得 类类型
Class cl = object.getClass();
//通过类类型获得 类中的属性对象数组
Field[] fi = cl.getDeclaredFields();
//通过request获得所有的 表单中提交的name值
Enumeration en = request.getParameterNames();
//循环枚举中的值
while (en.hasMoreElements()) {
//获得枚举中的值
String fieldName = en.nextElement().toString();
//循环类中所有的属性对象
for(int i=0;i<fi.length;i++){
if (fieldName.equals(fi[i].getName())) {
fi[i].setAccessible(true);//尤为重要,可以被外部赋值
fi[i].set(object, request.getParameter(fieldName));
}
}
}
//调用默认的方法 执行处理
Method method2 = this.getClass().getDeclaredMethod("execute", null);
//获得返回的url字符串
Object url = method2.invoke(this, null);
//根据返回的字符串 跳转页面
request.getRequestDispatcher(String.valueOf(url)).forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
LoginServlet 继承于 BaseServlet
package com.cj.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cj.pojo.Users;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends BaseServlet {
private Users us = new Users();
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("自动封装数据后的结果");
System.out.println(us.getUsername());
System.out.println(us.getPassword());
return "/index.jsp";
}
public Users getModel() {
// TODO Auto-generated method stub
return us;
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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="<%=path %>/servlet/LoginServlet" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="tijiao">
</form>
</body>
</html>