提交 | 用户 | age
|
a6a76f
|
1 |
package com.yc.utils; |
F |
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.io.PrintWriter; |
|
5 |
|
|
6 |
import javax.servlet.http.HttpServletResponse; |
|
7 |
|
|
8 |
/** |
|
9 |
* |
|
10 |
* 向客户端输出数据的工具类(操作完后已经关闭流) |
|
11 |
* |
|
12 |
* @creator heqing |
|
13 |
* @create-time 2011-5-4 上午09:19:37 |
|
14 |
*/ |
|
15 |
public class ResponseUtils { |
|
16 |
|
|
17 |
/** |
|
18 |
* 输出字符串 |
|
19 |
* @param response |
|
20 |
* @param str |
|
21 |
*/ |
|
22 |
public static void print(HttpServletResponse response, String str) { |
|
23 |
try { |
|
24 |
response.setContentType("text/html;charset=utf-8"); |
|
25 |
PrintWriter out = response.getWriter(); |
|
26 |
out.print(str); |
|
27 |
out.flush(); |
|
28 |
out.close(); |
|
29 |
} catch (IOException e) { |
|
30 |
e.printStackTrace(); |
|
31 |
} |
|
32 |
} |
|
33 |
|
|
34 |
/** |
|
35 |
* 输出js脚本 |
|
36 |
* @param response |
|
37 |
* @param str如 :alert('世界你好!'); |
|
38 |
*/ |
|
39 |
public static void printScript(HttpServletResponse response, String str) { |
|
40 |
try { |
|
41 |
response.setContentType("text/html;charset=utf-8"); |
|
42 |
PrintWriter out = response.getWriter(); |
|
43 |
out.print("<script type='text/javascript'>"+str+"</script>"); |
|
44 |
out.flush(); |
|
45 |
out.close(); |
|
46 |
} catch (IOException e) { |
|
47 |
e.printStackTrace(); |
|
48 |
} |
|
49 |
} |
|
50 |
|
|
51 |
/** |
|
52 |
* 向页面输出json |
|
53 |
* |
|
54 |
* **/ |
|
55 |
public static void printJson(HttpServletResponse resp,String str){ |
|
56 |
try { |
|
57 |
resp.setContentType("application/json;charset=utf-8"); |
|
58 |
PrintWriter out = resp.getWriter(); |
|
59 |
out.print(str); |
|
60 |
out.flush(); |
|
61 |
out.close(); |
|
62 |
} catch (IOException e) { |
|
63 |
e.printStackTrace(); |
|
64 |
} |
|
65 |
} |
|
66 |
|
|
67 |
} |