What is the use of the setAttribute () method of the request object?

xiaoxiao2021-03-06  91

I have learned about this problem when I do TAG analysis. In Java, the data is transmitted through the web server. One is session, one is the requestion, the method is as follows: 1. Using Request.setttribute (" BeanObject ", Sample); // where sample is bean

2. Using the session: httpsession s = request.getSession (); s.setttribute ("beanobject", Sample); // sample is bean

When passing, the data is restored to Object and put it in the Request or Session. After arriving at the destination, restore to the original type, Struts is better in this regard, and he provides the corresponding restore interface in Beanut, in writing < HTML: TEXT Property = "xxx" When Struts parses, the setttribute's bean will take out in the object of Object, then take the value of Property, I use my own method when I analyze, as follows:

************** Main.java ******************************* * package reflect; import java.io *;. public class main {public static void main (String [] args) throws IOException {Bean1 bean1 = new Bean1 (); System.out.println (Reflect.get (bean1, "getName ", 0)); System.out.Println (Reflect.get (bean1," getold ", 0)); system.out.println (Reflect.get (bean1," getname ", 1); system.out. Println (Reflect.get (bean1, "getold", 1); system.out.println (Reflect.get (Bean1, "GetName", 2)); System.get.Println (Reflect.get (bean1, "Getold ", 2)); system.out.println (Reflect.get (bean1," getname ", 3); system.out.println (reflect.get (bean1," getold ", 3); system.out. Println (Reflect.get (Bean1, "GetName", 4)); System.Out.println (Reflect.get (Bean1, "Getold", 4); bean2 bean2 = new bean2 (); system.out.println Reflect.get (bean2, "getmember", 0)); system.out.println (Reflect.get (bean2, "getyear", 0)); system.out.println (reflect.get (bean2, "getmember", 1)); system.out.pri NTLN (Reflect.get (Bean2, "GetYear", 1)); System.Out.println (Reflect.get (bean2, "getmember", 2); system.out.println (reflect.get (bean2, "getyear ", 2)); system.out.println (Reflect.get (bean2," getmember ", 3); system.out.println (Reflect.get (Bean2," GetYear ", 3); system.out. Println (Reflect.get (bean2, "getmember", 4)); system.out.println (Reflect.get (bean2, "getyear", 4));}} *********** *********** REFLECT.JAVA ********************************** Package reflect;

Import java.lang.reflect.Array; import java.lang.reflect.method; import java.util.arrayList;

Public class reflect {public static string get (Object Object, string methodname, int i) {class cls = Object.getClass (); object ret; {bject ret;

Method method = cls.getMethod (methodname, null); ret = method.invoke (cls.newinstance (), null; arraylist array = new arraylist ();

IF (ret.getclass (). isarray ()) {RET = array.get (RET, I);}} cat = NULL;} if (Ret == null) {Return NULL;} return Ret.toString ();}}

************** Beijing1.java ******************************** * package reflect;

Public class bean1 {string [] name = {"Zhao", "Money", "Sun", "Li"}; int [] OLD = {28, 27, 32, 26}; public string [] getName () { Return name;} public int [] getold () {returna}}

************** Beijing2.java ******************************* * package reflect;

Public class bean2 {string [] member = {"Zhang", "Liu", "Wang", "Fang"}; int [] year = {31, 28, 30, 36}; public string [] getmember () { Return Member;} public int [] getYear () {returnies;}}

Everyone pays attention to: mainly reflect.java, you only need to give him the name of the bean and the name of the getProperty, he will pass the data back to you.

转载请注明原文地址:https://www.9cbs.com/read-122138.html

New Post(0)