jsp學習筆記之四:內建對象,jsp學習筆記內建

來源:互聯網
上載者:User

jsp學習筆記之四:內建對象,jsp學習筆記內建

application對象:

 設定一個名為name,值為val的應用內共用的資料

<%  application.setAttribute("name",val);%>

 擷取名為name的應用內共用資料的值

<%  application.setAttribute("name");%>

 

exception對象:

 一般用於異常處理頁面。在當前頁面出現錯誤時,會將錯誤類型和錯誤資訊傳到異常處理頁面,此時異常處理頁面可以用以下指令擷取錯誤資訊和類型:

<%=exception.getClass()%><%=exception.getMessage()%>

 

out對象:

<%  out.println("<tr>");  out.println("text");  out.println("</tr>");%>

效果同

<tr>text</tr>

 

pageContext對象:

 設定不同範圍的變數:

//設定名為page的變數,值為hello,預設範圍為page,無需另外加入參數pageContent.setAttributesScope("page","hello");//設定名為request2的變數,值為hello,範圍為requestpageContent.setAttributesScope("request2","hello",PageContext.REQUEST_SCOPE);//設定名為session2的變數,值為hello,範圍為sessionpageContent.setAttributesScope();("session2","hello",PageContext.SESSION_SCOPE);//設定名為app2的變數,值為hello,範圍為applicationpageContent.setAttributesScope("app2","hello",PageContext.APPLICATION_SCOPE);

 擷取不同範圍的變數:

//擷取名為page的變數,預設範圍設為pagepageContent.setAttributesScope("page");//擷取名為request2的變數,範圍定位為requestpageContent.setAttributesScope("request2",PageContext.REQUEST_SCOPE);

  註:範圍為session、application的不再一一列出

 擷取變數的範圍:

//擷取名為page的變數的範圍pageContext.getAttributesScope("page");

  傳回值對應:

    1:page    2:request    3:session    4:application

 

request對象:<url>

  post請求和get請求的區別參考文章: http://kimmking.github.io/2017/12/01/comparing-get-and-post/

    post請求案例:

form.jsp(發送方)

<form id="f1" method="post" action="request.jsp">    請輸入一個數字:<input type="text" value="" name="number" id="number"/></form>

request.jsp(接收方)

<%        //擷取所有要求標頭的名稱        Enumeration<String> headerNames=request.getHeaderNames();        while(headerNames.hasMoreElements()){            //逐個取出請求名稱            String headerName=headerNames.nextElement();            //擷取並列印對應請求名稱的值            out.println(headerName+request.getHeader(headerName)+"<br/>");        }        //設定解碼方式,對於中文,使用GBK解碼方式        request.setCharacterEncoding("GBK");        //擷取並列印從form.jsp傳來的名為number的參數值        String num=request.getParameter("number");
     //擷取多個值的請求參數:
     //String[] mVal=request.getParameter("mValues"); out.println("number: "+num+"<br>");%>

    註:get請求與post請求的發送和接收類似

reponse對象:

 響應產生非字元響應執行個體:

產生一張圖片並產生非字元響應(案例摘自《輕量級JAVA EE公司專屬應用程式實戰》)

<%--  Created by IntelliJ IDEA.  User: macrazds  Date: 18-3-11  Time: 下午3:43  To change this template use File | Settings | File Templates.--%><%@ page contentType="image/png" language="java" %><%@ page import="java.awt.image.*,javax.imageio.*,java.io.*,java.awt.*" %><%    //建立BufferedImage對象    BufferedImage image=new BufferedImage(340,160,BufferedImage.TYPE_INT_RGB);    //以image對象來擷取Graphics對象    Graphics g=image.getGraphics();    //畫圖(所畫圖形會儲存在image對象中):    g.fillRect(0,0,400,400);    //設定顏色  下同    g.setColor(new Color(255,0,0));    //畫弧  下同    g.fillArc(20,20,100,100,30,120);    g.setColor(new Color(0,255,0));    g.fillArc(20,20,100,100,150,120);    g.setColor(new Color(0,0,255));    g.fillArc(20,20,100,100,270,120);    g.setColor(new Color(0,0,0));    //設定文本字型    g.setFont(new Font("Arial Black",Font.PLAIN,16));    //畫出字串    g.drawString("red:climb",200,60);    g.drawString("green:swim",200,100);    g.drawString("blue:jump",200,140);    g.dispose();  //畫完後將g對象dispose    //將映像輸出到頁面的響應    ImageIO.write(image,"png",response.getOutputStream());%>

  接著在其他頁面直接調用img標籤即可顯示此圖片:

<img src="img.jsp">

  註:本例在chrome中無法將img.jsp顯示,具體原因還不清楚,但可在firefox或IE等正常顯示

  重新導向:

   進入當前頁面後,重新導向(自動跳轉)到另一頁面,且不保留傳給原頁面request範圍的屬性及所有請求參數

//重新導向到index.jsp<% response.sendRedirect("index.jsp"); %>

 增加Cookie:

  Cookie:常用於網站記錄客戶資訊,如使用者名稱等。

  寫一個username的Cookie:

<%    //首先擷取名為name的請求參數    String name=request.getParameter("name");    //以擷取到的name為值,建立一個名為username的Cookie對象    Cookie c=new Cookie("username",name);    //設定Cookie生存期,為3600秒    c.setMaxAge(3600);    //在用戶端處添加新Cookie對象c    response.addCookie(c);%>

  從用戶端中擷取指定Cookie:

<%    //擷取本機所有保留的Cookie    Cookie[] cookies=new request.getCookies();    //遍曆擷取到的Cookies    for(Cookie c:cookies){        //符合條件(這裡的條件是Cookie對象的名稱相符)的Cookie作相應處理(這裡僅僅是列印出來)        if(c.getName().equals("username"))            out.println(c.getValue());    }%>

  值得一提的情況是,若Cookie的內容是中文的話,可以用到以下指令:

java.net.URLEncoder.encode("中文內容","gbk");  //這裡的傳回值是String類型,可以直接作為Cookie構造方法的(值)參數java.net.URLDecoder.decode(cookie.getValues());  //如果內容是中文,並已通過上面一條指令正確建立了中文內容的Cookie對象,這個可以直接放到out.println(String str);裡面直接顯示

 

session對象:

 這個對象表示瀏覽器和伺服器之間的一次會話,即瀏覽器串連到伺服器開始,直到與伺服器斷開,為一次會話。常用於使用者登陸系統等。此外,其範圍內的屬性可以在(來自同一個伺服器的)多個頁面間共用。

 常用方法:

//設定一個session屬性session.setAttribute(String attName,Object attValue);//擷取一個session屬性值session.getAttribute(String attName);

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.