反射
1.读取正在运行类的方法和属性
1.Class对象
Class c=Class. forName("co.goho.yuanyu.pojo.Student");
Class co=Student.class;
Class c1=student.getClass();
c.getConstructors();
c.getMethods();
c.getFields();
c.getDeclaredMethods();
//获得Student类的class对象
Class c = Class.forName("cn.goho.yuanyu.pojo.Student");
//获得Sudent类的所有的公开方法,返回一个方法数组
Method[] methods = c.getMethods();
for (Method m : methods) {
System.out.println(m.getName());
}
//获得Student类的构造方法
Constructor[] constructors = c.getConstructors();
for (Constructor ctt : constructors) {
System.out.println(ctt.getName());
}
//获得Student类的公有成员变量
Field[] fields = c.getFields();
for (Field field : fields) {
System.out.println(field.getName());
}
//获得Student类的所有成员变量
Field[] fields1 = c.getDeclaredFields();
for (Field field : fields1) {
System.out.println(field.getName());
}
获得名称为a,参数为int的方法
Method m=c.getMethod("a",int.class)
获得指定构造方法
Constructor ctt=c.getConstructor(int.class,String.class,float.class);
调用构造函数 创建对象
c.newInstance(4,"张三",38.9f);
//使用class对象获得构造方法,并且使用构造方法创建对象
Constructor ctt0 = c.getConstructor();
Object o0 = ctt0.newInstance();
System.out.println(o0);
//调用有参构造,获得指定构造方法
Constructor ctt2 = c.getConstructor(int.class, String.class, float.class);
Object o1 = ctt2.newInstance(1, "张三", 2.3f);
System.out.println(o1);
//获得名称为a,参数为int的方法
//o0对象中只有初始化属性,显示3,“李四”,200
//找到对象的三个set
Method a = c.getMethod("a", int.class);
Method b = c.getMethod("b", String.class);
Method d = c.getMethod("c", float.class);
a.invoke(o0, 3);
b.invoke(o0, "李四");
d.invoke(o0, 200f);
System.out.println(o0);
1.步骤,创建动态web项目
2.创建RegisterServlet(urlPattern="/register")继承HttpServlet,重写doGet和doPost,
所有请求交给doGet方法处理
3.修改index.jsp文件,添加一个表单。注册User (Stirng username,String
password,String realname,String tel)
4.创建Userservlet处理所有的和账号密码相关的请求
/user?method=register
5.在UserServlet先读取到mehtod的值,知道用户的意图
6.使用反射在当前类中找到对应的方法处理该请求
7.提取公共类
RegisterServlet类
@WebServlet(urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String username=req.getParameter("username");
String password=req.getParameter("password");
String realname=req.getParameter("realname");
String tel=req.getParameter("tel");
System.out.println(username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
UserServlet类
@WebServlet(urlPatterns = "/user")
//注册 /user?method=register
//登录 /user?method=login
//退出 /user?method=logout
//读取到method参数的值,就可以指定用户的意图
public class UserServlet extends BaseServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//先读取method
String m=req.getParameter("method");
//根据m来找到对应的方法
if(m!=null){
try {
//通过用户传送过来的method参数找到对应的方法
Method method=this.getClass().getDeclaredMethod(m,HttpServletRequest.class);
method.invoke(this,req,resp);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
public void register(HttpServletRequest req,HttpServletResponse resp){
Map<String,String[]>parameterMap=req.getParameterMap();
Set<String>set=parameterMap.keySet();
Iterator<String>it=set.iterator();
while ((it.hasNext())){
System.out.println(it.next());
}
}
public void showUser(HttpServletRequest req,HttpServletResponse resp){
List<String>strings=new ArrayList<>();
strings.add("张三李四");
strings.add("123");
strings.add("9527");
req.setAttribute("strings",strings);
try {
req.getRequestDispatcher("show.jsp").forward(req,resp);
} catch (ServletException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册</title>
<style>
div input{
text-align: center;
}
</style>
</head>
<body>
<form method="post" action="${pageContext.request.contextPath}/user?method=register">
<div><input type="text" name="username" placeholder="账号"></div>
<div><input type="text" name="password" placeholder="密码"></div>
<div><input type="text" name="realname" placeholder="姓名"></div>
<div><input type="text" name="tel" placeholder="电话"></div>
<div><input type="submit" value="注册"></div>
</form>
</body>
</html>
前端servlet问题的解决方案
1.继承BaseServlet
2.servlet.service
3.不在设置单独请求映射,在请求中添加方法参数,根据方法参数,找到对应的函数处理请求
4.反射,通过函数的名称和函数的参数类型找到方法,并且invoke该方法
5.继承,当一个类继承BaseServlet之后,BaseServlet中的共有方法就直接可以被子类调用
BaseServlet类
public class BaseServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//先读取method
String m=req.getParameter("method");
//根据m来找到对应的方法
if(m!=null){
try {
//通过用户传送过来的method参数找到对应的方法
Method method=this.getClass().getDeclaredMethod(m,HttpServletRequest.class);
method.invoke(this,req,resp);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
public void register(HttpServletRequest req,HttpServletResponse resp){
Map<String,String[]> parameterMap=req.getParameterMap();
Set<String> set=parameterMap.keySet();
Iterator<String> it=set.iterator();
while ((it.hasNext())){
System.out.println(it.next());
}
}
public void showUser(HttpServletRequest req,HttpServletResponse resp){
List<String> strings=new ArrayList<>();
strings.add("张三李四");
strings.add("123");
strings.add("9527");
req.setAttribute("strings",strings);
try {
req.getRequestDispatcher("show.jsp").forward(req,resp);
} catch (ServletException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}