jsp九種內置對象:request, reponse, out, session, application, config, pagecontext, page, exception.
一.request對象:該對象封裝了用戶提交的信息,通過調用該對象相應的方法可以獲取封裝的信息,即使用該對象可以獲取用戶提交信息。
1.request對象可以使用getparameter(string s)方法獲取該表單通過text提交的信息。如:
request.getparameter(“boy”)
例:request1.jsp:
<%@ page contenttype="text/html;charset=gb2312" %>
<html>
<body bgcolor=green><font size=1>
<form action="tree.jsp" method=post name=form>
<input type="text" name="boy">
<input type="submit" value="enter" name="submit">
</form>
</font>
</body>
</html>
tree.jsp:
<%@ page contenttype="text/html;charset=gb2312" %>
<html>
<body bgcolor=green><font size=1>
<p>獲取文本框提交的信息:
<%string textcontent=request.getparameter("boy");
%>
<br>
<%=textcontent%>
<p> 獲取按鈕的名字:
<%string buttonname=request.getparameter("submit");
%>
<br>
<%=buttonname%>
</font>
</body>
</html>
使用request對象獲取信息要格外小心,要避免使用空對象,否則會出現nullpointerexception異常,所以我們可以作以下處理.
request3.jsp
<%@ page contenttype="text/html;charset=gb2312" %>
<html>
<body bgcolor=cyan><font size=5>
<form action="" method=post name=form>
<input type="text" name="girl">
<input type="submit" value="enter" name="submit">
</form>
<%string textcontent=request.getparameter("girl");
double number=0,r=0;
if(textcontent==null)
{textcontent="";
}
try{ number=double.parsedouble(textcontent);
if(number>=0)
{r=math.sqrt(number) ;
out.print("<br>"+string.valueof(number)+"的平方根:");
out.print("<br>"+string.valueof(r));
}
else
{out.print("<br>"+"請輸入一個正數");
}
}
catch(numberformatexception e)
{out.print("<br>"+"請輸入數字字符");
}
%>
</font>
</body>
</html>
2.處理漢字信息:當request對象獲取客戶提交的漢字字符時,會出現亂馬問題,必須進行特殊處理。首先,將獲取的字符串用iso-8859-1進行編碼,并將編碼存放到一個字節數組中,然后再將這個屬組轉化為字符竄對象即刻。
如: string textcontent=request.getparameter("boy");
byte b[]=textcontent.getbytes("iso-8859-1");
textcontent=new string(b);
例:tree2.jsp
<%@ page contenttype="text/html;charset=gb2312" %>
<mhml>
<body>
<p>獲取文本框提交的信息:
<%string textcontent=request.getparameter("boy");
byte b[]=textcontent.getbytes("iso-8859-1");
textcontent=new string(b);
%>
<br>
<%=textcontent%>
<p> 獲取按鈕的名字:
<%string buttonname=request.getparameter("submit");
byte c[]=buttonname.getbytes("iso-8859-1");
buttonname=new string(c);
%>
<br>
<%=buttonname%>
</body>
</html>
3.常用方法舉例:
getprotocol(), getservletpath(), getcontentlength(), getmethod(), getremoteaddr(), getremotehost(), getservername(), getparametername()
例:
<%@ page contenttype="text/html;charset=gb2312" %>
<%@ page import="java.util.*" %>
<mhml>
<body bgcolor=cyan>
<font size=5>
<br>客戶使用的協議是:
<% string protocol=request.getprotocol();
out.println(protocol);
%>
<br>獲取接受客戶提交信息的頁面:
<% string path=request.getservletpath();
out.println(path);
%>
<br>接受客戶提交信息的長度:
<% int length=request.getcontentlength();
out.println(length);
%>
<br>客戶提交信息的方式:
<% string method=request.getmethod();
out.println(method);
%>
<br>獲取http頭文件中user-agent的值::
<% string header1=request.getheader("user-agent");
out.println(header1);
%>
<br>獲取http頭文件中accept的值:
<% string header2=request.getheader("accept");
out.println(header2);
%>
<br>獲取http頭文件中host的值:
<% string header3=request.getheader("host");
out.println(header3);
%>
<br>獲取http頭文件中accept-encoding的值:
<% string header4=request.getheader("accept-encoding");
out.println(header4);
%>
<br>獲取客戶的ip地址:
<% string ip=request.getremoteaddr();
out.println(ip);
%>
<br>獲取客戶機的名稱:
<% string clientname=request.getremotehost();
out.println(clientname);
%>
<br>獲取服務器的名稱:
<% string servername=request.getservername();
out.println(servername);
%>
<br>獲取服務器的端口號:
<% int serverport=request.getserverport();
out.println(serverport);
%>
<br>獲取客戶端提交的所有參數的名字:
<% enumeration enum=request.getparameternames();
while(enum.hasmoreelements())
{string s=(string)enum.nextelement();
out.println(s);
}
%>
<br>獲取頭名字的一個枚舉:
<% enumeration enum_headed=request.getheadernames();
while(enum_headed.hasmoreelements())
{string s=(string)enum_headed.nextelement();
out.println(s);
}
%>
<br>獲取頭文件中指定頭名字的全部值的一個枚舉:
<% enumeration enum_headedvalues=request.getheaders("cookie");
while(enum_headedvalues.hasmoreelements())
{string s=(string)enum_headedvalues.nextelement();
out.println(s);
}
%>
<br>
</font>
</body>
</html>
4.獲取html表單提交的數據
單選框radio
例:
radio.jsp:
<html>
<%@ page contenttype="text/html;charset=gb2312" %>
<body bgcolor=cyan><font size=1 >
<p>詩人李白是中國歷史上哪個朝代的人:
<form action="answer.jsp" method=post name=form>
<input type="radio" name="r" value="a">宋朝
<input type="radio" name="r" value="b">唐朝
<input type="radio" name="r" value="c">明朝
<input type="radio" name="r" value="d" checked="ok">元朝
<br>
<p>小說紅樓夢的作者是:
<br>
<input type="radio" name="p" value="a">曹雪芹
<input type="radio" name="p" value="b">羅貫中
<input type="radio" name="p" value="c">李白
<input type="radio" name="p" value="d">司馬遷
<br>
<input type="submit" value="提交答案" name="submit">
</form>
</font>
</body>
</html>
answer.jsp:
<html>
<%@ page contenttype="text/html;charset=gb2312" %>
<body bgcolor=cyan><font size=1 >
<% int n=0;
string s1=request.getparameter("r");
string s2=request.getparameter("p");
if(s1==null)
{s1="";}
if(s2==null)
{s2="";}
if(s1.equals("b"))
{ n++;}
if(s2.equals("a"))
{ n++;}
%>
<p>您得了<%=n%>分
</font>
</body>
</html>
列表框<select><option>
例:
select.jsp:
<html>
<%@ page contenttype="text/html;charset=gb2312" %>
<body bgcolor=cyan><font size=1 >
<p>選擇計算和的方式
<form action="sum.jsp" method=post name=form>
<select name="sum" size=2>
<option selected value="1">計算1到n的連續和
<option value="2">計算1到n的平方和
<option value="3">計算1到n的立方和
</select>
<p>選擇n的值:<br>
<select name="n" >
<option value="10">n=10
<option value="20">n=20
<option value="30">n=30
<option value="40">n=40
<option value="50">n=50
<option value="100">n=100
</select>
<br><br>
<input type="submit" value="提交你的選擇" name="submit">
</form>
</font>
</body>
</html>
sum.jsp:
<html>
<%@ page contenttype="text/html;charset=gb2312" %>
<body bgcolor=cyan><font size=1 >
<% long sum=0;
string s1=request.getparameter("sum");
string s2=request.getparameter("n");
if(s1==null)
{s1="";}
if(s2==null)
{s2="0";}
if(s1.equals("1"))
{int n=integer.parseint(s2);
for(int i=1;i<=n;i++)
{sum=sum+i;
}
}
else if(s1.equals("2"))
{int n=integer.parseint(s2);
for(int i=1;i<=n;i++)
{sum=sum+i*i;
}
}
else if(s1.equals("3"))
{int n=integer.parseint(s2);
for(int i=1;i<=n;i++)
{sum=sum+i*i*i;
}
}
%>
<p>您的求和結果是<%=sum%>
</font>
</body>
</html>
二.response對象:對客戶的請求做出動態的響應,向客戶端發送數據。
1.動態響應contentype屬性
當一個用戶訪問一個jsp頁面時,如果該頁面用page指令設置頁面的contenttype屬性是text/html,那么jsp引擎將按照這種屬性值作出反映。如果要動態改變這個屬性值來響應客戶,就需要使用response對象的setcontenttype(string s)方法來改變contenttype的屬性值。
格式:response.setcontenttype(string s)
參數s可取text/html,, application/x-msexcel, application/msword等。
例如:response1.jsp
<%@ page contenttype="text/html;charset=gb2312" %>
<html>
<body bgcolor=cyan><font size=1 >
<p>我正在學習response對象的
<br>setcontenttype方法
<p>將當前頁面保存為word文檔嗎?
<form action="" method="get" name=form>
<input type="submit" value="yes" name="submit">
</form>
<% string str=request.getparameter("submit");
if(str==null)
{str="";
}
if(str.equals("yes"))
{response.setcontenttype("application/msword;charset=gb2312");
}
%>
</font>
</body>
</html>
2.response重定向:在某些情況下,當響應客戶時,需要將客戶重新引導至另一個頁面,可以使用response的sendredirect(url)方法實現客戶的重定向。
例如:response2.jsp:
<html>
<head><title>where to go</title></head>
<body>
<%
string address = request.getparameter("where");
if(address!=null){
if(address.equals("chinawebber"))
response.sendredirect("http://www.chinawebber.com");
else if(address.equals("yahoo"))
response.sendredirect("http://www.yahoo.com");
else if(address.equals("sun"))
response.sendredirect("http://www.sun.com");
}
%>
<b>please select:</b><br>
<form action="goto.jsp" method="get">
<select name="where">
<option value="chinawebber" selected>go to chinawebber
<option value="sun" > go to sun
<option value="microsoft" > go to microsoft
</select>
<input type="submit" value="go">
</form>
三.session對象
1.什么是session:session對象是一個jsp內置對象,它在第一個jsp頁面被裝載時自動創建,完成會話期管理。
從一個客戶打開瀏覽器并連接到服務器開始,到客戶關閉瀏覽器離開這個服務器結束,被稱為一個會話。當一個客戶訪問一個服務器時,可能會在這個服務器的幾個頁面之間反復連接,反復刷新一個頁面,服務器應當通過某種辦法知道這是同一個客戶,這就需要session對象。
2.session對象的id:當一個客戶首次訪問服務器上的一個jsp頁面時,jsp引擎產生一個session對象,同時分配一個string類型的id號,jsp引擎同時將這個id號發送到客戶端,存放在cookie中,這樣session對象和客戶之間就建立了一一對應的關系。當客戶再訪問連接該服務器的其他頁面時,不再分配給客戶新的session對象,直到客戶關閉瀏覽器后,服務器端該客戶的session對象才取消,并且和客戶的會話對應關系消失。當客戶重新打開瀏覽器再連接到該服務器時,服務器為該客戶再創建一個新的session對象。
3.session對象常用方法:
i.public string getid():獲取session對象編號。
ii.public void setattribute(string key,object obj):將參數object指定的對象obj添加到session對象中,并為添加的對象指定一個索引關鍵字。
iii.public object getattribute(string key):獲取session對象中含有關鍵字的對象。
iv.public boolean isnew():判斷是否是一個新的客戶。
例如:session1.jsp
<%@ page contenttype="text/html;charset=gb2312" %>
<html>
<body bgcolor=cyan><font size=5>
<body>
<%string s=session.getid();
%>
<p>您的session對象的id是:
<br>
<%=s%>
<body>
<html>
例如:session2.jsp
<%@ page contenttype="text/html;charset=gb2312" %>
<html>
<body>
<%! int number=0;
synchronized void countpeople()
{
number++;
}
%>
<%
if(session.isnew())
{
countpeople();
string str=string.valueof(number);
session.setattribute("count",str);
}
%>
<p>您是第<%=(string)session.getattribute("count")%>個訪問本站的人。
<body>
<html>
四.aplication對象
1.什么是application:
服務器啟動后就產生了這個application對象,當客戶再所訪問的網站的各個頁面之間瀏覽時,這個application對象都是同一個,直到服務器關閉。但是與session不同的是,所有客戶的application對象都是同一個,即所有客戶共享這個內置的application對象。
2.application對象常用方法:
(1)public void setattribute(string key,object obj): 將參數object指定的對象obj添加到application對象中,并為添加的對象指定一個索引關鍵字。
(2)public object getattribute(string key): 獲取application對象中含有關鍵字的對象。
例:application.jsp
<html>
<head>
<title>application變量的使用</title>
</head>
<body>
<center>
<font size = 5 color = blue>application變量的使用</font>
</center>
<hr>
<p></p>
<%
object obj = null;
string strnum = (string)application.getattribute("num");
int num = 0;
//檢查是否num變量是否可取得
if(strnum != null)
num = integer.parseint(strnum) + 1; //將取得的值增加1
application.setattribute("num", string.valueof(num)); //起始num變量值
%>
application對象中的<font color = blue>num</font>變量值為
<font color = red><%= num %></font><br>
</body>
</html>
五.out對象
out對象是一個輸出流,用來向客戶端輸出數據。out對象用于各種數據的輸出。
常用方法:
?。?)out.print():輸出各種類型數據。
?。?)out.newline():輸出一個換行符。
?。?)out.close():關閉流。
例:out.jsp
<%@ page contenttype="text/html;charset=gb2312" %>
<%@ page import="java.util.date"%>
<html>
<head>
<%
date now = new date();
string hours=string.valueof(now.gethours());
string mins=string.valueof(now.getminutes());
string secs=string.valueof(now.getseconds());
%>
現在是
<%out.print(string.valueof(now.gethours()));%>
小時
<%out.print(string.valueof(now.getminutes()));%>
分
<%out.print(string.valueof(now.getseconds()));%>
秒
</font>
</body>
</html>
六.cookie
1.什么是cookie:
cookie是web服務器保存在用戶硬盤上的一段文本。cookie允許一個web站點在用戶的電腦上保存信息并且隨后再取回它。
舉例來說,一個web站點可能會為每一個訪問者產生一個唯一的id,然后以cookie文件的形式保存在每個用戶的機器上。
如果您使用ie瀏覽器訪問web,您會看到所有保存在您的硬盤上的cookie。它們最常存放的地方是:c:/windows/cookies(在win 2000中則是c:/documents and settings/您的用戶名/cookies )
cookie是以“關鍵字key=值value“的格式來保存紀錄的.
2.創建一個cookie對象,調用cookie對象的構造函數可以創建cookie。cookie對象的構造函數有兩個字符串參數:cookie名字和cookie值。
cookie c=new cookie(“username”,”john”);
3. jsp中如果要將封裝好的cookie對象傳送到客戶端,使用response的addcookie()方法。
格式:response.addcookie(c)
4.讀取保存到客戶端的cookie,使用request對象的getcookies()方法,執行時將所有客戶端傳來的cookie對象以數組的形式排列,如果要取出符合需要的cookie對象,就需要循環比較數組內每個對象的關鍵字。
例:
cookie[] c=request.getcookies();
if(c!=null)
for(int i=0;i<c.length;i++)
if(“username”.equals(c[i].getname()))
out.println(c[i].getvalue());
5.設置cookie對象有效時間:setmaxage()。
c.setmaxage(3600);
6.cookie應用
1.網站能夠精確地知道有多少人瀏覽過。
由于代理服務器、緩存等的使用,唯一能幫助網站精確統計來訪人數的方法就是為每個訪問者建立一個唯一的id。使用cookie,網站可以完成以下工作:
●測定多少人訪問過;
●測定訪問者有多少是新用戶(即第一次來訪),多少是老用戶;
●測定一個用戶多久訪問一次網站。
網站使用數據庫達到上述目標。當一個用戶第一次訪問時,網站在數據庫中建立一個新的id,并把id通過cookie傳送給用戶。用戶再次來訪時,網站把該用戶id對應的計數器加1,得到用戶的來訪次數。
例:cookie.jsp
<%@ page contenttype="text/html; charset=gb2312"
import="java.util.date" %>
<html>
<head>
<title>cookie的存取</title>
</head>
<body>
<center>
<font size = 5 color = blue>cookie的存取</font>
</center>
<hr>
<p></p>
<%
//建立cookie變數
cookie intval = new cookie("intval", "100");
cookie temp = null;
response.addcookie(intval); //將cookie變數加入cookie中
cookie[] cookies = request.getcookies();
//取得cookie資料
int cookielen = cookies.length;
//取得cookie變數陣列的長度
if(cookielen != 0) //判斷是否成功取得cookie資料
{
for (int i = 0; i < cookielen; i++)
{
temp = cookies[i]; //取得cookies陣列中的cookie變數
if (temp.getname().equals("intval"))
{ //判斷是否取得名為dateval的cookie資料
%>
cookie中<font color = blue>intval</font>變量的值為
<font color = red><%= intval.getvalue()%></font><br>
<%
}
}
}
else
{
%>
不存在cookie<br>
<%
}
%>
</body>
</html>
例:cookiedate.jsp
<%@ page contenttype="text/html; charset=gb2312"
import="java.util.date"%>
<html>
<head>
<title>自cookie存取日期/時間數據</title>
</head>
<body>
<center>
<font size = 5 color = blue>自cookie存取日期/時間數據</font>
</center>
<hr>
<p></p>
<%
date now = new date(); //取得目前的系統時間
cookie dateval = new cookie("dateval", string.valueof(now.gettime()));
//欲將儲存至cookie時間/日期值轉換為毫秒數
response.addcookie(dateval); //將cookie變數加入cookie中
cookie temp = null;
dateval = null; //重設cookie變數
cookie[] cookies = request.getcookies();
//取得cookie資料
int cookielen = cookies.length;
//取得cookie變數陣列的長度
if(cookielen != 0) //判斷是否成功取得cookie資料
{
for (int i = 0; i < cookielen; i++)
{
temp = cookies[i]; //取得cookies陣列中的cookie變數
if (temp.getname().equals("dateval"))
{ //判斷是否取得名為dateval的cookie資料
%>
cookie中<font color = blue>dateval</font>
變量的值為<font color = red>
<%= new date(long.parselong(temp.getvalue())) %>
</font><br>
<%
}
}
}
else //若無法取得cookie資料則執行下面的敘述
{
%>
無法取得cookie<br>
<%
}
%>
</body>
</html>
新聞熱點
疑難解答