java三级考试_java web 学习 --第五天(Java三级考试)
Response對象
response對象主要是向客戶端瀏覽器發送二進制數據,如輸出Cookie、設置HTTP文件頭信息等方面內容
response主要功能和方法
getWrite() ? 獲得PrintWrite類的對象實例,實現向瀏覽器輸出信息
addCookie() 在客戶端計算機磁盤上創建出Cookie對象實例,在Cookie對象實例可以保存客戶端信息特征,然后采用request對象的getCookies()方法獲取客戶機所有Cookie對象
addHeader() 添加HTTP頭文件信息,將信息傳送到客戶瀏覽器中
containsHeader() 判斷指定名字的文件頭是否存在,返回布爾型true / false
setHeader() 設置指定名字HTTP頭文件的值,若該值存在則覆蓋
sendRedirect() 重定向到由參數targetURL所指示的目標JSP頁面或Servlet程序,不能向客戶端輸出信息
setContentType() 在相應中可以設置內容的文檔數據類型和格式
setBufferSize() 設置Web容器的緩沖區大小,配合getBufferSize()方法返回該緩沖器信息
利用response對象實現向客戶機種寫入Cookie信息
Cookie或稱Cookies,是指Web應用系統為了辨別訪問者身份而存儲在客戶機中的一個文本文件,其中包含特定數據,比如登陸郵箱:
可以把用戶名和密碼放在客戶機Cookie中,下次訪問不需要再輸入用戶名密碼
讀取Cookie文件信息,使用Cookie類中的getName()和getValue()返回客戶端的某一個特定Cookie對象名所對應的值。而利用response對象addCookie(cookie data )方法可以寫入Cookie對象中所包裝的數據。
實例:在系統首頁中添加讀寫Cookie信息
在index jsp頁面中添加如下代碼
String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
My JSP add Cookie informationString nowAccessTime=null;
Cookie mycookie=null;
Cookie[] cookies=null;
Date now=null;%>
now=newDate();if(cookies==null)
{
lastAccessTime=(now.getYear()+1900+"Year"+now.getMonth()+"Month"+now.getDay()+"Day"+now.getHours()+"Hour"+now.getMinutes()+"Minute"+now.getSeconds()+"Second");
mycookie=new Cookie("lastAccessTime",lastAccessTime);
mycookie.setMaxAge(30*24*60*60); //30 days 24hours 60mins 60 seconds
response.addCookie(mycookie);
}else
for(int index=0;index
{if(cookies[index].getName().equals("lastAccessTime"))
{
lastAccessTime=cookies[index].getValue();
nowAccessTime=(now.getYear()+1900+"Year "+now.getMonth()+" Month "+now.getDay()+" Day "+now.getHours()+" Hour "+now.getMinutes()+" Minute "+now.getSeconds()+" Second");
mycookie=new Cookie("lastAccessTime",nowAccessTime);
mycookie.setMaxAge(30*24*60*60); //30 days 24hours 60mins 60 seconds
response.addCookie(mycookie);break;
}
}
out.print("the last time your visit this system is "+lastAccessTime);%>
首先用request的getCookie獲取一下cookie,如果沒有cookie信息,使用response的addCookie增加cookie信息
區分重定向與跳轉方式的區別
請求轉發過程中客戶端瀏覽器只向server端產生一次請求,而重定向是兩次;
請求轉發時在瀏覽器的URL地址欄中的信息不會發生改變,仍然是原來的URL而重定向將會轉向目標URL
使用HTTP請求轉發:
使用轉發由于只有一次請求,所以在一個頁面的request.setAttribute 能夠在跳轉后的頁面使用request.getAttribute獲取其屬性值
使用4個jsp頁面和一個類文件
新建 login.jsp 設置登陸框,用戶名密碼。。。。。
String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
This is my first JSP pageusername:
password:
userType:
user
master
新建response.jsp頁面當用戶名是:young并且密碼是1234跳轉到index.jsp否則跳轉到error.jsp,引入userInfo類,使用其verifyID方法校驗用戶名密碼
userinfo類代碼:
packagemypackage;public classuserInfo {/***@paramargs
* two parameters add get/set methods*/String userName=null;
String passWord=null;publicString getUserName() {returnuserName;
}public voidsetUserName(String userName) {this.userName =userName;
}publicString getPassWord() {returnpassWord;
}public voidsetPassWord(String passWord) {this.passWord =passWord;
}public booleanverifyID( )
{if(userName.equals("young") && passWord.equals("1234"))
{
System.out.print("login successful\n");return true;
}else{
System.out.print("login failure\n");return false;
}
}
}
代碼如下:
String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
My JSP 'response.jsp' starting pageRequestDispatcher rd=null;%>
user.setPassWord(request.getParameter("password"));if(user.verifyID())
{
request.setAttribute("userNameString",user.getUserName());
targetPage="index.jsp";
System.out.print(user.getUserName());
}else{
request.setAttribute("errorMSG","sorry,Login FAILED.\n");
targetPage="error.jsp";
}
rd=request.getRequestDispatcher(targetPage);
rd.forward(request,response);%>
新建error.jsp 這個頁面會獲取response.jsp傳遞過來的errorMSG
String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
My JSP 'error.jsp' starting page新建index.jsp
String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
My JSP add Cookie informationString nowAccessTime=null;
Cookie mycookie=null;
Cookie[] cookies=null;
Date now=null;%>
now=newDate();if(cookies==null)
{
lastAccessTime=(now.getYear()+1900+"Year"+now.getMonth()+"Month"+now.getDay()+"Day"+now.getHours()+"Hour"+now.getMinutes()+"Minute"+now.getSeconds()+"Second");
mycookie=new Cookie("lastAccessTime",lastAccessTime);
mycookie.setMaxAge(30*24*60*60); //30 days 24hours 60mins 60 seconds
response.addCookie(mycookie);
}else
for(int index=0;index
{if(cookies[index].getName().equals("lastAccessTime"))
{
lastAccessTime=cookies[index].getValue();
nowAccessTime=(now.getYear()+1900+"Year "+now.getMonth()+" Month "+now.getDay()+" Day "+now.getHours()+" Hour "+now.getMinutes()+" Minute "+now.getSeconds()+" Second");
mycookie=new Cookie("lastAccessTime",nowAccessTime);
mycookie.setMaxAge(30*24*60*60); //30 days 24hours 60mins 60 seconds
response.addCookie(mycookie);break;
}
}
out.print("the last time your visit this system is "+lastAccessTime);%>
登陸成功會顯示: 可以看出,跳轉后的頁面URL仍然是跳轉前的URL并未發生任何變化,由于只進行一次請求,所以request.setAttribute 和request.getAttribute能夠正常工作
登陸失敗:
而是用response.sendRedirect()會出現2次請求不能再是用request的對象包裝和傳遞參數,可以使用session.setAttribute("NAME","VALUE")使用EL表達式語句${sesstionScope.userNameString}
從session會話對象中獲取傳遞的參數,同時重定向后的URL為重定向頁面的URL。
總結
以上是生活随笔為你收集整理的java三级考试_java web 学习 --第五天(Java三级考试)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 进程睡眠_Linux进程的睡眠
- 下一篇: Java改知能机_Java 面试突击之