JSP内置对象的使用

1.使用request对象获取表单数据

Ex4_20.jsp
 1 <%@ page language="java" contentType="text/html; charset=GB18030"
 2     pageEncoding="GB18030"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <font size=3>
11 <form action="Ex4_21.jsp" method="post" name="form">
12 姓名:
13     <input type="text" name=name>
14     <br>
15 <p>诗人李白是中国历史上哪个朝代的人:</p>
16     <br>
17         <input type="radio" name="poet" value="a">宋朝
18         <input type="radio" name="poet" value="b">唐朝
19         <input type="radio" name="poet" value="c">明朝
20         <input type="radio" name="poet" value="d">元朝
21 <p>小说红楼梦的作者是:</p>
22     <br>
23         <input type="radio" name="author" value="a">曹雪芹
24         <input type="radio" name="author" value="b">罗贯中
25         <input type="radio" name="author" value="c">李白
26         <input type="radio" name="author" value="d">司马迁
27 <p>末代皇帝是:</p>
28     <br>
29         <input type="radio" name="emperor" value="a">光绪
30         <input type="radio" name="emperor" value="b">同治
31         <input type="radio" name="emperor" value="c">咸丰
32         <input type="radio" name="emperor" value="d">宣统
33     <br>
34         <input type="submit" value="提交答案" name="submit">
35 </form>
36 </font>
37 </body>
38 </html>
Ex4_21.jsp
 1 <%@ page language="java" contentType="text/html; charset=GB18030"
 2     pageEncoding="GB18030"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <center>
11 <font size=4>
12 <%
13     int n=0;
14     //从request对象中取出个参数的值
15     //获得用户名
16     String name =request.getParameter("name");
17     byte[] a=name.getBytes("ISO_8859-1");
18     name=new String(a,"GB2312");
19     //通过参数poet、author、emperor的值获得用户选择第一题、第二题、第三题的结果
20     String s1=request.getParameter("poet");
21     String s2=request.getParameter("author");
22     String s3=request.getParameter("emperor");
23     //判断各选项的答案是否正确,用计数器n计算成绩。
24     if(s1==null){
25             s1="";
26     }
27     if(s2==null){
28         s2="";
29     }
30     if(s3==null){
31         s3="";
32     }
33     if(s1.equals("b")){
34         n++;
35     }
36     if(s2.equals("a")){
37         n++;
38     }
39     if(s3.equals("d")){
40         n++;
41     }
42 %>
43 <br><br><br><%=name %><br><br><br>
44 您答对了<%=n %>道题
45 </font>
46 </center>
47 </body>
48 </html>

运行结果为:

 

 

 

2.使用request对象处理数据编码

处理方法如下:

String str="hello";
byte[] b=str.getBytes("ISO-8859-1");
str=new String (b,"GB18030");
Ex4_22.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<font size=3></font>
<form action="Ex4_23.jsp" method="post" name="form">
    <input type="text" name="name">
    <input type="submit" value="提交" name="submit">
</form>
</body>
</html>
Ex4_23.jsp
 1 <%@ page language="java" contentType="text/html; charset=GB18030"
 2     pageEncoding="GB18030"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <p>获取文本框提交的信息:
11 <%
12     String textContent=request.getParameter("name");
13     byte[] b=textContent.getBytes("ISO-8859-1");
14     textContent=new String(b,"GB18030");
15 %>
16 <br>
17 <%=textContent %>
18 <p>获取按钮的名字:
19 <%
20     
21     String buttonName=request.getParameter("submit");
22     byte[] c=buttonName.getBytes("ISO-8859-1");
23     buttonName=new String(c,"GB18030");
24 %>
25 <br>
26 <%=buttonName %>
27 </body>
28 </html>

 

 

 

 

3.使用request对象获得客户端、服务器端信息

 1 <%@ page language="java" contentType="text/html; charset=GB18030"
 2     pageEncoding="GB18030"%>
 3 <%@ page import="java.util.*" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <font size=5>
12 <br>客户使用的协议是:
13 <%
14     String protocol =request.getProtocol();
15     out.println(protocol);
16 %>
17 <br>获取接受客户提交信息的页面:
18 <%
19     String path =request.getServletPath();
20     out.println(path);
21 %>
22 <br>接受客户提交信息的长度:
23 <%
24     int length =request.getContentLength();
25     out.println(length);
26 %>
27 <br>客户提交信息的方式:
28 <%
29     String method =request.getMethod();
30     out.println(method);
31 %>
32 <br>获取HTTP头文件中User-Agent的值:
33 <%
34     String header1 =request.getHeader("User-Agent");
35     out.println(header1);
36 %>
37 <br>获取HTTP头文件中accept的值:
38 <%
39     String header2 =request.getHeader("accept");
40     out.println(header2);
41 %>
42 <br>获取HTTP头文件中Host的值:
43 <%
44     String header3=request.getHeader("Host");
45     out.println(header3);
46 %>
47 <br>获取HTTP头文件中accept-encoding的值:
48 <%
49     String header4=request.getHeader("accept-encoding");
50     out.println(header4);
51 %>
52 <br>获取客户的IP地址:
53 <%
54     String IP=request.getRemoteAddr();
55     out.println(IP);
56 %>
57 <br>获取客户机的名称:
58 <%
59     String clientName=request.getRemoteHost();
60     out.println(clientName);
61 %>
62 <br>获取服务器的名称:
63 <%
64     String serverName =request.getServerName();
65     out.println(serverName);
66 %>
67 <br>获取服务器的端口号:
68 <%
69     int serverPort =request.getServerPort();
70     out.println(serverPort);
71 %>
72 <br>获取客户端提交的所有参数的名字:
73 <%
74     Enumeration enum1=request.getParameterNames();
75     while(enum1.hasMoreElements()){
76         String s=(String)enum1.nextElement();
77         out.println(s);
78     }
79 %>
80 <br>获取头名字的一个枚举:
81 <%
82     Enumeration enum_headed=request.getHeaderNames();
83     while(enum_headed.hasMoreElements()){
84         String s=(String)enum_headed.nextElement();
85         out.println(s);
86     }
87 %>
88 <br>获取头文件中指定头名字全部值的一个枚举:
89 <%
90     Enumeration enum_headedValues=request.getHeaders("cookie");
91     while(enum_headedValues.hasMoreElements()){
92         String s=(String)enum_headedValues.nextElement();
93         out.println(s);
94     }
95 %>
96 <br>
97 </font>
98 </body>
99 </html>

运行结果为:

 

 

 

 

4.使用response对象动态响应contentType

  当一个用户访问一个JSP页面时,如果该页面用page指令设置页面的contentType属性是text/html,那么jsp引擎将按照这种属性值反应。

  如果要动态改变这个属性值来响应客户,那么就需要用response对象的setContentType(String s)方法来改变contentType的属性值。

使用方法为:

response.setContentType(String s)

  其中s的取值可以为:text/html、application/x-msexcel、application/msword等。

下面的例子单击yes按钮,文件将会被另存为一份Word格式的文档。

Ex4_25.jsp
 1 <%@ page language="java" contentType="text/html; charset=GB18030"
 2     pageEncoding="GB18030"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <p>我正在学习response对象的setContentType方法
11 <p>将当前页面保存为word文档吗?
12 <form action="" method="get" name="form">
13 <input type="submit" value="yes" name="submit">
14 </form>
15 <%
16     String str=request.getParameter("submit");
17     if(str==null){
18         str="";
19     }
20     if(str.equals("yes")){
21         response.setContentType("application/msword");
22     }
23 %>
24 </body>
25 </html>

 

 

 

 

 

5.使用response对象操作HTTP文件头

  response对象可以通过方法setHeader(String name,String value)设置指定名字的HTTP文件头的值,以此达到操作HTTP文件头的目的。

  response对象设置的值将会覆盖HTTP文件头就有的值。

以下的例子完成了两件事:1.防止了jsp产生的输出保存在浏览器缓存中;2.每三秒要求浏览器刷新一次页面。

Ex4_26.jsp
 1 <%@ page language="java" contentType="text/html; charset=GB18030"
 2     pageEncoding="GB18030"%>
 3 <%@ page import="java.util.*" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 注意观察当前页面时间的变化:
12 <%
13     //防止在JSP或Servlet中的输出不被浏览器保存在cache中
14     response.setHeader("Cache-Control", "no-store");
15     //每三秒钟刷新一次页面
16     response.setHeader("refresh", "3");
17     out.println(new Date().toLocaleString());
18 %>
19 </body>
20 </html>

 

 

 

 

6.使用response对象重定向页面

  使用response对象的sendRediret方法实现客户的重定向。

下面的例子使用了sendRedirect方法:

Ex4_27.jsp
 1 <%@ page language="java" contentType="text/html; charset=GB18030"
 2     pageEncoding="GB18030"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <b>请选择网址:</b><br>
11 <form action="Ex4_28.jsp" method="get">
12 <select name="where">
13     <option value="ChinaWebber" selected>ChinaWebber主页
14     <option value="Yahoo" > Yahoo主页
15     <option value="Sun">Sun主页
16 </select>
17 <input type="submit" value="go">
18 </form>
19 </body>
20 </html>
Ex4_28.jsp
 1 <%@ page language="java" contentType="text/html; charset=GB18030"
 2     pageEncoding="GB18030"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <%
11     String address=request.getParameter("where");
12     if(address!=null){
13         if(address.equals("ChinaWebber")){
14             response.sendRedirect("http://www.ChinaWebber.com");
15         }
16         else if(address.equals("Yahoo")){
17             response.sendRedirect("http://www.yahoo.com");
18         }
19         else if(address.equals("Sun")){
20             response.sendRedirect("http://www.sun.com");
21         }
22     }
23 %>
24 </body>
25 </html>

 

 

 

 

 

7.使用session对象维护页面信息

下面例子包括3个文件:Ex4_29.jsp、Ex4_30.jsp、Ex4_31.jsp

在Ex4_29.jsp文件中设置了username和password两个属性的值,页面跳转到Ex4_30.jsp后,可以发现两属性的值正好是Ex4_29.jsp设置的值,这是session对象维护页面信息的结果。

Ex4_30.jsp文件的最后使用removeValue方法在session对象中移除了username属性,因此在跳转到Ex4_31.jsp时,username的值为null。

Ex4_29.jsp
 1 <%@ page language="java" contentType="text/html; charset=GB18030"
 2     pageEncoding="GB18030"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <center><br><br>
11 session对象维护页面信息示例<br><br><br>
12 <%
13     String username="Jim";
14     String password="123";
15     session.putValue("username", username);
16     session.putValue("password", password);
17 %>
18 <a href="Ex4_30.jsp">指向第二页</a>
19 </center>
20 </body>
21 </html>
Ex4_30.jsp
 1 <%@ page language="java" contentType="text/html; charset=GB18030"
 2     pageEncoding="GB18030"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <%
11     String usr=(String)session.getValue("username");
12     String pwd=(String)session.getValue("password");
13 %>
14 <%=usr %><br>
15 <%=pwd %><br>
16 <%out.println("session create:"+session.getCreationTime()); %><br>
17 <%out.println("session id:"+session.getId()); %><br>
18 <%out.println("session last access:"+session.getLastAccessedTime()); %><br>
19 <%out.println("session原来最大休眠时间:"+session.getMaxInactiveInterval()); %><br>
20 <%session.setMaxInactiveInterval(session.getMaxInactiveInterval()+1); %><br>
21 <%out.println("session最新最大休眠时间:"+session.getMaxInactiveInterval()); %><br>
22 <%
23     String []name=session.getValueNames();
24     out.println("------------------"+"<br>");
25     for(int i=0;i<name.length;i++){
26         out.println(session.getValue(name[i])+"<br>");
27     }
28 %>
29 <%
30     session.removeValue("username");
31 %>
32 <a href="Ex4_31.jsp">指向第三页</a>
33 </body>
34 </html>
Ex4_31.jsp
 1 <%@ page language="java" contentType="text/html; charset=GB18030"
 2     pageEncoding="GB18030"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <%
11     String usr=(String)session.getValue("username");
12     String pwd=(String)session.getValue("password");
13 %>
14 <%=usr %>
15 <br>
16 <%=pwd %>
17 <br>
18 </body>
19 </html>

运行结果为:

 


8.使用application维护全局信息

  在jsp服务器运行时刻,仅有一个application对象,它由服务器创建,也由服务器自动清除,不能被用户创建和清除。

下面的例子中:num变量被application保持。

Ex4_32.jsp
 1 <%@ page language="java" contentType="text/html; charset=GB18030"
 2     pageEncoding="GB18030"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <center>
11 <font size=5>application变量的使用</font>
12 </center>
13 <hr>
14 <p>
15 <%
16     Object obj=null;
17     String strNum=(String)application.getAttribute("NUm");
18     int Num=0;
19     //检查Num变量是否可以取得
20     if(strNum!=null){
21         Num=Integer.parseInt(strNum)+1;//将取得的值+1
22     }
23     application.setAttribute("Num", String.valueOf(Num));
24 %>
25 application对象中的Num变量值为<%=Num %><br>
26 </body>
27 </html>

运行结果为(即使将页面刷新多次,Num的值依然没变。)


 

 

 

posted @ 2012-10-12 21:35  Jim-Huang  阅读(242)  评论(0编辑  收藏  举报