动态反射

1.页面

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 
 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 4 <html>
 5   <head>
 6     
 7     <title>利用反射的io-1</title>
 8     
 9     <meta http-equiv="pragma" content="no-cache">
10     <meta http-equiv="cache-control" content="no-cache">
11     <meta http-equiv="expires" content="0">    
12     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
13     <meta http-equiv="description" content="This is my page">
14     <!--
15     <link rel="stylesheet" type="text/css" href="styles.css">
16     -->
17     <script type="text/javascript" src ="${pageContext.request.contextPath }/js/jquery-1.11.1.js"></script>
18     <script type="text/javascript">
19         var xmlHttpRequest = null;
20         function getXmlHttpRequest() {
21             // 如果非IE
22             if(window.XMLHttpRequest) {
23                 xmlHttpRequest = new XMLHttpRequest();
24             } else if(window.ActiveXObject){
25                 xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
26             } 
27         }
28         
29         function ajaxRequest(url, cb, clz, fileName) {
30             getXmlHttpRequest();
31             if(xmlHttpRequest) {
32                 // 初始化设置的动作
33                 xmlHttpRequest.open("post", url, true);
34                 xmlHttpRequest.onreadystatechange = cb;
35                 xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
36                 // 正式发送请求
37                 xmlHttpRequest.send("clz=" + clz + "&fileName=" + fileName);
38             }
39         }
40         
41         function cb1 () {
42             if(xmlHttpRequest.readyState == 4 && 
43                 xmlHttpRequest.status == 200) {
44                 var result = xmlHttpRequest.responseText;
45                 alert(result);
46             }
47         }
48         
49         function cb2 () {
50             if(xmlHttpRequest.readyState == 4 && 
51                 xmlHttpRequest.status == 200) {
52                 var result = xmlHttpRequest.responseText;
53                 alert(result);
54                 result = eval("("+result+")");
55                 for(var v in result) {
56                     alert(result[v]);
57                 }
58             }
59         }
60         $(function(){
61             $("#InputEmpbtn").click(function(){
62                 ajaxRequest("../Jxl02Servlet",cb1,"entity.Emp","/user.properties");
63             });
64             $("#ExputEmpbtn").click(function(){
65                 ajaxRequest("../OJxl01Servlet",cb2,"entity.Emp");
66             });
67         })
68     </script>
69   </head>
70   
71   <body>
72       <input id="InputEmpbtn" type="button" value="员工导入" />
73     <input id="ExputEmpbtn" type="button" value="员工导出" />
74   </body>
75 </html>


反射

2.

 1 public void doPost(HttpServletRequest request, HttpServletResponse response)
 2             throws ServletException, IOException {
 3 
 4         try{
 5             response.setContentType("text/html");
 6             PrintWriter out = response.getWriter();
 7             
 8             String clzStr = request.getParameter("clz");
 9             String fileName = request.getParameter("fileName");
10             Class<?> c = Class.forName(clzStr);
11             Object obj = c.newInstance();
12             Field[] fields = obj.getClass().getDeclaredFields();
13             ReaderProperties reader = new ReaderProperties();
14             for (Field field : fields) {
15                 field.setAccessible(true);
16                 String name = field.getName();
17                 Properties properties =reader.loadProperties(fileName);
18                 Iterator<Entry<Object, Object>> it = properties.entrySet().iterator();  
19                 while (it.hasNext()) {  
20                     Entry<Object, Object> entry = it.next();  
21                     Object key = entry.getKey();  
22                     Object value = entry.getValue();  
23                     if(name.equals(key)) {
24                         if(field.getType().toString().equals("double")) {
25                             field.set(obj, Double.valueOf(value.toString()));
26                         } else {
27                             field.set(obj, value.toString());
28                         }
29                     }
30                 }  
31             }
32             reflectObj(obj);
33         } catch(Exception e){
34             e.printStackTrace();
35         }
36         
37     }
38 
39     private void reflectObj(Object obj) throws Exception {
40         File file = new File("d:/obj.xls");
41         WritableWorkbook book = Workbook.createWorkbook(file);
42         WritableSheet sheet = book.createSheet("第一个", 0);
43         Field[] fields = obj.getClass().getDeclaredFields();
44         for (int i = 0; i < fields.length; i++) {
45             Field field = fields[i];
46             field.setAccessible(true);
47             String name = field.getName();
48             String v = field.get(obj) == null ? "" : field.get(obj).toString();
49             Label label1 = new Label(i,0,name);
50             Label label2 = new Label(i,1,v);
51             sheet.addCell(label1);
52             sheet.addCell(label2);
53         }
54         book.write();
55         book.close();
56     }


加载配置文件

3.

 1 package test;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.util.Enumeration;
 6 import java.util.Iterator;
 7 import java.util.Properties;
 8 import java.util.Map.Entry;
 9 
10 public class ReaderProperties {
11 
12     public static void main(String[] args) {
13         Properties properties = new ReaderProperties().loadProperties("/jdbc.properties");
14         Enumeration<?> enu = properties.propertyNames();  
15         while (enu.hasMoreElements()) {  
16             Object key = enu.nextElement();  
17             System.out.println(key);  
18         }  
19         Enumeration<Object> enu2 = properties.elements();  
20         while (enu2.hasMoreElements()) {  
21             Object value = enu2.nextElement();  
22             System.out.println(value);  
23         }
24         
25         Iterator<Entry<Object, Object>> it = properties.entrySet().iterator();  
26         while (it.hasNext()) {  
27             Entry<Object, Object> entry = it.next();  
28             Object key = entry.getKey();  
29             Object value = entry.getValue();  
30             System.out.println("key   :" + key);  
31             System.out.println("value :" + value);  
32             System.out.println("---------------");  
33         }  
34     }
35 
36     
37     public Properties loadProperties(String resources) {
38         // 使用InputStream得到一个资源文件
39         InputStream inputstream = this.getClass()
40         .getResourceAsStream(resources);
41         // new 一个Properties
42         Properties properties = new Properties();
43         try {
44             // 加载配置文件
45             properties.load(inputstream);
46             return properties;
47         } catch (IOException e) {
48             throw new RuntimeException(e);
49         } finally {
50             try {
51                 inputstream.close();
52             } catch (IOException e) {
53                 throw new RuntimeException(e);
54             }
55         }
56 
57     }
58 }


配置文件

4.

1 deptNo=10
2 deptName=\u8D22\u52A1\u90E8

 

5.

1 empNo=9527
2 empName=\u5F20\u4E09
3 salary=10000

 

6.

 

posted @ 2015-01-07 17:49  江湖一笑  阅读(162)  评论(0编辑  收藏  举报