JSP 中request與response的用法詳解

jsp 中request與response的用法詳解

概要:

在學(xué)習這兩個對象之前,我們應(yīng)該已經(jīng)有了http協(xié)議的基本了解了,如果不清楚http協(xié)議的可以看我的關(guān)于http協(xié)議的介紹。因為其實request和response的使用大部分都是對http協(xié)議的操作。

request對象的介紹

我們先從request對象進行介紹:

我們知道http協(xié)議定義了請求服務(wù)器的格式:

請求行
請求頭
空格
請求體(get請求沒有請求體)

好了,這里我們就不詳細介紹了,我們只看幾個應(yīng)用就可以了,沒什么難度:

應(yīng)用1 獲取請求頭

/** 
 * 獲取http請求頭 
 *   string getheader(string name),適用于單值頭 
 *   int getintheader(string name),適用于單值int類型的請求頭 
 *   long getdateheader(string name),適用于單值毫秒類型的請求頭 
 *    enumeration<string> getheaders(string name),適用于多值請求頭 
 * 
 * 
 */ 
public class cservlet extends httpservlet { 
 
  public void doget(httpservletrequest request, httpservletresponse response) 
      throws servletexception, ioexception { 
 
    string ip = request.getremoteaddr();//獲取ip地址 
    string method = request.getmethod();//獲取請求方式 
    system.out.println(ip); 
    system.out.println(method); 
    getliulanqi(request); 
    protectmethod(request, response); 
  } 
//防盜鏈 
  public void protectmethod(httpservletrequest request, 
      httpservletresponse response) { 
    string url = request.getheader("referer"); 
    system.out.println(url); 
    if(url != null && url.equalsignorecase("http://localhost:8080/day10/myhtml.html") ) { 
      system.out.println("hi"); 
    } 
    else { 
      response.setstatus(302); 
      response.setheader("location","/day10/myhtml.html"); 
      system.out.println("重定向"); 
    } 
  } 
//判斷瀏覽器的類型 
  public void getliulanqi(httpservletrequest request) { 
    string user = request.getheader("user-agent"); 
    if(user != null) { 
      string usernoc = user.tolowercase(); 
      if(usernoc.contains("chrome")) { 
        system.out.println("瀏覽器類型:谷歌" ); 
      } 
      else if(usernoc.contains("firefox/")) { 
        system.out.println("瀏覽器類型:火狐"); 
      } 
      else { 
        system.out.println("瀏覽器類型:ie"); 
      } 
    } 
    else { 
      system.out.println("null"); 
    } 
  } 
} 

應(yīng)用2獲取url路徑

/** 
 * 獲取請求url 
  http://localhost:8080/day10_2/aservlet?username=xxx&password=yyy 
    > string getscheme():獲取協(xié)議,http 
    > string getservername():獲取服務(wù)器名,localhost 
    > string getserverport():獲取服務(wù)器端口,8080 
    > *****string getcontextpath():獲取項目名,/day10_2 
    > string getservletpath():獲取servlet路徑,/aservlet 
    > string getquerystring():獲取參數(shù)部分,即問號后面的部分。username=xxx&password=yyy 
    > string getrequesturi():獲取請求uri,等于項目名+servlet路徑。/day10_2/aservlet 
    > string getrequesturl():獲取請求url,等于不包含參數(shù)的整個請求路徑。http://localhost:8080/day10_2/aservlet 
 * 
 * 
 */ 
public class dservlet extends httpservlet { 
 
  public void doget(httpservletrequest request, httpservletresponse response) 
      throws servletexception, ioexception { 
    system.out.println(request.getscheme()); 
    system.out.println(request.getservername()); 
    system.out.println(request.getserverport()); 
    system.out.println(request.getcontextpath()); 
    system.out.println(request.getservletpath()); 
    system.out.println(request.getquerystring()); 
    system.out.println(request.getrequesturi()); 
    system.out.println(request.getrequesturl()); 
  } 
 
} 

應(yīng)用3獲取請求參數(shù)

/** 
 * 
 * 請求參數(shù):有一個參數(shù)一個值的,還有一個參數(shù)多個值! 
    > *****string getparameter(string name):獲取指定名稱的請求參數(shù)值,適用于單值請求參數(shù) 
    > string[] getparametervalues(string name):獲取指定名稱的請求參數(shù)值,適用于多值請求參數(shù) 
    > enumeration<string> getparameternames():獲取所有請求參數(shù)名稱 
    > *****map<string,string[]> getparametermap():獲取所有請求參數(shù),其中key為參數(shù)名,value為參數(shù)值。 
 * 
 */ 
public class eservlet extends httpservlet { 
 
  public void doget(httpservletrequest request, httpservletresponse response) 
      throws servletexception, ioexception { 
    dopost(request,response); 
  } 
 
  public void dopost(httpservletrequest request, httpservletresponse response) 
      throws servletexception, ioexception { 
    request.setcharacterencoding("utf-8");//調(diào)用他來解決亂碼問題的,不會的可以看我寫的解決亂碼的那一篇 
    string name = request.getparameter("name"); 
    string password = request.getparameter("password"); 
    string[] aihao = request.getparametervalues("aihao"); 
    response.setheader("content-type", "text/html;charset=utf-8"); 
    printwriter pw = response.getwriter(); 
    pw.println("name:" + name + "<br/>"); 
    pw.println("password:" + password + "<br/>"); 
    pw.print("aihao"); 
    for (int i = 0; i < aihao.length; i++) { 
      switch (i) { 
      case 1: 
        pw.print("吃飯"); 
        break; 
      case 2: 
        pw.print("睡覺"); 
        break; 
      case 3: 
        pw.print("打豆豆"); 
        break; 
 
      default: 
         
        break; 
      } 
    } 
    pw.close(); 
  } 
 
} 
<!-- 用于發(fā)送請求的html --> 
<html> 
 <head> 
  <title>denglu.html</title> 
  <meta http-equiv="content-type" content="text/html; charset=utf-8"> 
 </head> 
  
 <body> 
  <form action="/day10/eservlet" method="post"><!-- 通過post方法提交 --> 
    姓名:<input type="text" name="name"/><br/> 
    密碼:<input type="password" name="password"><br/> 
    愛好:<input type="checkbox" name="aihao" value="1"/>吃飯 
    <input type="checkbox" name="aihao" value="2"/>睡覺 
    <input type="checkbox" name="aihao" value="3"/>打豆豆 
    <input type="submit" value="提交"> 
  </form> 
 </body> 
</html> 

應(yīng)用4設(shè)置request對象屬性

用來實現(xiàn)同一個應(yīng)用程序servlet之間值得傳遞,我們還用到了轉(zhuǎn)發(fā)

/** 
 * 
 ** request域 
    servlet中三大域?qū)ο螅簉equest、session、application,都有如下三個方法: 
    > void setattribute(string name, object value) 
    > object getattribute(string name) 
   > void removeattribute(string name); 
    > 同一請求范圍內(nèi)使用request.setattribute()、request.getattribute()來傳值!前一個servlet調(diào)用setattribute()保存值,后一個servlet調(diào)用getattribute()獲取值。 
 
 * 
 */ 
public class fservlet extends httpservlet { 
 
  public void doget(httpservletrequest request, httpservletresponse response) 
      throws servletexception, ioexception { 
    request.setattribute("name","weijinhao"); 
    request.setattribute("password", "123"); 
    requestdispatcher rd = request.getrequestdispatcher("/gservlet"); 
    rd.forward(request, response); 
  } 
 
} 

public class gservlet extends httpservlet { 
 
  public void doget(httpservletrequest request, httpservletresponse response) 
      throws servletexception, ioexception { 
    string name = (string)request.getattribute("name"); 
    string password = (string)request.getattribute("password"); 
    response.setheader("content-type", "text/html;charset=utf-8"); 
    printwriter pw = response.getwriter(); 
    pw.println("name:" + name + "<br/>"); 
    pw.println("password:" + password + "<br/>"); 
    pw.close(); 
  } 
 
} 

好了,request的用法大致就是這摸多了。

讓我們來看看response對象吧。

response對象的使用

http協(xié)議定義了響應(yīng)瀏覽器的格式:

響應(yīng)行
響應(yīng)頭
空格
響應(yīng)體

response對象的大致應(yīng)用是用在設(shè)置響應(yīng)碼,設(shè)置響應(yīng)頭,及設(shè)置響應(yīng)正文。

應(yīng)用1設(shè)置響應(yīng)正文

/** 
   * response的正文 
   * 1,字符流 
   * 2,字節(jié)流 
   */ 
  public void doget(httpservletrequest request, httpservletresponse response) 
      throws servletexception, ioexception { 
    //string n = "hello javaweb"; 
    //byte[] b = n.getbytes(); 
    //字節(jié)流的使用 
    /*servletoutputstream outputstream = response.getoutputstream(); 
    outputstream.write(b); 
    outputstream.close();*/ 
     
     
    //字符流的使用 
    /*printwriter p = response.getwriter(); 
    p.write(n); 
    p.close();*/ 
     
    //commoms工具包的使用 
    fileinputstream fileinputstream = new fileinputstream("c:/users/74087/desktop/6226354_994604.jpg"); 
    byte[] b = ioutils.tobytearray(fileinputstream); 
    servletoutputstream outputstream = response.getoutputstream(); 
    outputstream.write(b); 
    outputstream.close(); 
  } 
 
} 

應(yīng)用2 設(shè)置響應(yīng)碼和響應(yīng)頭

/** 
   *response 的響應(yīng)碼 
   *1, senderror(int sc)//發(fā)送錯誤代碼 
   *2, senderror(int sc, java.lang.string msg) //發(fā)送錯誤代碼及描述 
   *3, setstatus(int sc) //發(fā)送成功響應(yīng)代碼 
   *response 的響應(yīng)頭 
   *1, setdateheader(java.lang.string name, long date) 
   *2, setheader(java.lang.string name, java.lang.string value) 
   *3, setintheader(java.lang.string name, int value) 
   *  下面的和上面的作用一樣但是是為了進行多值的添加 
   *  adddateheader(java.lang.string name, long date) 
   *  addheader(java.lang.string name, java.lang.string value) 
   *  addintheader(java.lang.string name, int value) 
   */ 
  public void doget(httpservletrequest request, httpservletresponse response) 
      throws servletexception, ioexception { 
    //sendnum(response); 
    //sendre(response); 
    //refresh(response); 
    //nocache(response); 
  } 
  //禁用瀏覽器緩存 
  public void nocache(httpservletresponse response) { 
    response.setheader("cache-control","no-cache"); 
    response.setheader("pragma","0"); 
    response.setheader("expires","no-cache"); 
  } 
  //自動跳轉(zhuǎn) 
  public void refresh(httpservletresponse response) { 
    response.setheader("refresh", "5;url=http://www.baidu.com"); 
  } 
  //重定向的使用 
  public void sendre(httpservletresponse response) { 
    response.setstatus(302); 
    response.setheader("location", "http://www.baidu.com"); 
  } 
 
  public void sendnum(httpservletresponse response) throws ioexception { 
    response.setstatus(200); 
    response.senderror(404); 
    response.senderror( 404, "ser accept your request but i don't want to you see my web page"); 
  } 
 
} 

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章
亚洲国产精品第一区二区,久久免费视频77,99V久久综合狠狠综合久久,国产免费久久九九免费视频