1.MVC的设计模式:使用JSP+Servlet+JavaBean来进行设计
优点:JSP用于显示数据,JavaBean用于封装和处理数据,Servlet控制。
案例如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 接收数据
/*String username = request.getParameter("username");
String password = request.getParameter("password");
// 封装数据
User user = new User();
user.setUsername(username);
user.setPassword(password);*/
Map<String, String[]> map = request.getParameterMap();
User user = new User();
try {
BeanUtils.populate(user, map);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println(user.getUsername());
System.out.println(user.getPassword());
// 调用另一个类处理数据
// 跳转到JSP
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
<title>JSP 设计模式MVC</title>
</head>
<body>
<form action="${ pageContext.request.contextPath }/ServletDemo" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="submit">
</form>
</body>
打印结果:
tommy
123445
2.内省->反射复习
反射:A.java —> A.class —> JVM。Classloader
创建Person类
public class Person {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, String age) {
super();
this.name = name;
this.age = age;
}
public void run(){
System.out.println("run....");
}
public void sayHello() {
System.out.println("sayhello");
}
}
person的Class,类加载器将Class文件加载到虚拟机,内存中,那么有一个Class对象,代表是Class文件加载到内存后所形成的一个对象。
反射,第一步获得Class文件加载到内存的形成字节码文件的Class的对象。
//反射的目的是为了操作某个类的属性或者方法
主要操作三个字段,Constructor、Field、Method
获取Class的方法:
@Test
public void demo1() throws ClassNotFoundException{
// 通过类名获得
Class clazz = Person.class;
// 通过类的实例对象获得。
Person person = new Person();
Class clazz1 = person.getClass();
// 使用Class的forName的静态方法获得
Class clazz2 = Class.forName("reflect.Person");
System.out.println(clazz + "\t" + clazz1 + "\t"+clazz2);
}
【通过反射操作构造方法】
@Test
/*
* 通过反射操作其构造方法:
* DBUtils: Person person = queryRunner.query(sql,new BeanHandler(Person.class));
*
* */
public void ContructorDemo() throws ClassNotFoundException, Exception, IllegalAccessException{
// 反射的第一步先获得class
Class clazz = Class.forName("reflect.Person");
Person person = (Person) clazz.newInstance();//直接生成一个类的实例。
person.run();
// 采用有参构造创建
Constructor constructor = clazz.getConstructor(String.class,String.class);
Person person2 = (Person) constructor.newInstance("tommy","29");
System.out.println(person2.name+"\t"+person2.age);
}
【通过反射获得类中的属性】
@Test
public void Fielddemo() throws Exception{
Class clazz = Class.forName("reflect.Person");
// 获得公有的属性
//clazz.getField("name");
//java.lang.reflect.Field[] field = clazz.getFields();//获得所有属性
// 获得私有的属性
Field field2 = clazz.getDeclaredField("name");
Field field3 = clazz.getDeclaredField("age");
field2.setAccessible(true);
field3.setAccessible(true);
Constructor constructor = clazz.getConstructor(String.class,String.class);
Person person = (Person) constructor.newInstance("hans","45");
String name = (String) field2.get(person);
String age = (String) field3.get(person);
System.out.println(name + "name" + age +"age");
}
【通过反射获得类中的方法,并且让方法执行】
@Test
/*
* 通过反射获得类中的方法,并且让方法执行。
*
*
*
* */
public void MethodDemo() throws Exception{
Class clazz = Class.forName("reflect.Person");
Method method = clazz.getDeclaredMethod("run");
method.setAccessible(true);
method.invoke(clazz.newInstance());
// 获得带有参数的方法
Method method2 = clazz.getDeclaredMethod("sayHello", String.class);
String s = (String) method2.invoke(clazz.newInstance(), "allen");
System.out.println(s);
}
/*
*
* Servlet的执行过程:
*
* <servlet>
<servlet-name>servletdemo2</servlet-name>
<servlet-class>com.itheima.a_servlet.servletdemo2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletdemo2</servlet-name>
<url-pattern>/servletdemo2</url-pattern>
</servlet-mapping>
Class clazz = Class.forName("com.itheima.demo2.servlet.ServletDemo1");
Servlet servlet = (Servlet)clazz.newInstance();
Method method = Clazz.getMethod("service",HttpServletRequest.class,HttpServletResponse.class)
method.invoke(clazz.newInstance(),request,response);
*
*
* */