《JSP開發技術》讀書筆記之內建對象

來源:互聯網
上載者:User

jsp中隱含定義了9個對象,也叫內建對象。

page:代表當前對象;pageContext:頁面內容物件,記錄當前頁面的相關資訊;request:該對象封裝了使用者提交的資訊;response:對客戶的請求做出動態響應,向用戶端發送資料;session:會話對象,記錄會話範圍內的相關資訊;application:應用對象,記錄應用範圍內的相關資訊;out:輸出資料流對象,用於向瀏覽器輸出資訊;config:設定物件,記錄當前頁面的配置資訊;exception:異常對象,記錄出現異常的頁面的錯誤資訊。

pageContext對象

屬性設定和擷取:void setAttribute()Object getAttribute()void removeAttribute()Object findAttribute()int getAttributeScope()Enumeration getAttributeNamesInScope()頁面轉寄和包含:void forward()<%pageContext.forward("top.jsp");%>void include()<%pageContext.include("top.jsp", true);%>

request對象

1、屬性設定和擷取:<%request.setAttribute("username", "david");out.println(request.getAttribute("username"));request.removeAttribute("username");Enumeration temp = request.getAttributeNames();while (temp.hasMoreElements())    out.println((String)temp.nextElement() + "<br>");%>2、設定字元集編碼:void setCharacterEncoding(String arg0)3、擷取請求參數:<%String name = request.getParameter("username");String hobby[] = request.getParameterValues("hobby");Enumeration temp = request.getParameterNames();while (temp.hasMoreElements())    out.println((String)temp.nextElement() + "<br>");Map map = request.getParameterMap();Iterator it = map.keySet().iterator();while (it.hasNext())out.println((String)it.next());<%--該方法用於擷取輸入資料流對象,可通過輸入資料流對象實現檔案上傳--%>ServletInputStream getInputStream();%>4、擷取請求行資訊:假設請求是:http://localhost:8888/test/hello.jsp?name=david對應的http請求行:GET /test/hello.jsp?name=david HTTP /1.1<%out.println(request.getMethod());GETout.println(request.getRequestURI());/test/hello.jspout.println(request.getQueryString());name=davidout.println(request.getProtocol());HTTP /1.1out.println(request.getContextPath());/testout.println(request.getPathInfo());nullout.println(request.getPathTranslated());nullout.println(request.getServletPath());hello.jsp%>5、擷取要求標頭資訊:<%out.println(request.getHeader("Accept-Language"));out.println(request.getIntHeader("Content-Length"));Enumeration ee = request.getHeaders("Accept-Encoding");while (ee.hasMoreElements())    out.println((String)ee.nextElement() + "<br>");Enumeration ee = request.getHeaderNames();while (ee.hasMoreElements())    out.println((String)ee.nextElement() + "<br>");%>int getDateHeader(String name);String getContentType();int getContentLength();Cookie[] getCookies();6、擷取網路資訊:<%out.println(request.getRemoteAddr());%>String getRemoteHost();int getRemotePort();String getLocalAddr();String getLocalName();int getLocalPort();String getServerName();int getServerPort();String getScheme();String getRequestURL();7、擷取會話對象:HttpSession getSession(boolean arg0);HttpSession getSession();8、擷取請求指派對象:RequestDispatcher getRequestDispatcher(String arg0);

 

response對象

1、設定狀態行<%response.setStatus(response.SC_NOT_FOUND);response.sendError(response.SC_NOT_FOUND, "this is a test");%>2、設定回應標頭資訊<%response.setHeader("Cache-Control", "no-cache");response.addHeader("Cache-Control", "no-cache");response.addHeader("Cache-Control", "no-cache");response.setContentType("text/html;charset=gbk");response.setCharacterEncoding("gbk");response.setLocal(new java.util.Local("zh", "cn"));%>void setIntHeader(String name, int value);void addIntHeader(String name, int value);void setDateHeadder(String name, long value);void addDateHeadder(String name, long value);void setContentType(String s);void setContentLength(int length);setContentType()、setCharacterEncoding()和setLocal()都可以設定輸出資料流的字元編碼,區別是:setCharacterEncoding()只能用來設定out輸出資料流所採用的編碼,但是其優先權最高,可以覆蓋後兩種方法中的設定;setContentType()既可以設定out輸出資料流字元的編碼方式,也可以設定瀏覽器接收到這些字元後以什麼編碼方式來解碼,它的優先權置中;setLocal()也只能用來設定out輸出資料流所採用的編碼,優先權最低,會被前兩種設定覆蓋。3、輸出相應本文ServletOutputStream getOutputStream();PrintWriter getWriter();void setBufferSize(int size);int getBufferSize();void flushBuffer();void reset()4、重新導向<%response.sendRedirect("/jsp/other.jsp");%>

session對象

1、屬性設定和擷取void setAttribute(String name, Object value);Object getAttribute(String name);void removeAttribute(String name);Enumeration getAttributeNames();2、session狀態相關操作String getId();long getCreationTime();void setMaxInactiveInterval(int interval);int getMaxInactiveInterval();boolean isNew();void invalidate();long getLastAccessedTime();

application對象

1、屬性設定和擷取void setAttribute(String name, Object value);Object getAttribute(String name);void removeAttribute(String name);Enumeration getAttributeNames();2、Web程式初始化參數設定<%-- web.xml的參數配置 --%><%out.println(application.getInitParameter("charset"));Enumeration temp = application.getIniParameterNames();  while (temp.hasMoreElements())      out.println((String)temp.nextElement() + "<br>"); %>3、訪問資源檔(1) 返回某個資來源目錄中所有子目錄和檔案路徑名稱,如<%Set set = application.getResourcePaths("/WEB-INF");Iterator it = set.iterator();while (it.hasNext())out.println((String)it.next());%>(2) 返回指定的資源路徑對應的一個URL,參數path以"/"開頭,如<%java.net.URL url = application.getResource("/other.jsp");%>(3) 返回串連到某個資源上的輸入資料流,如<%InpurStream is = application.getResourceAsStream("upload.jsp");ServletOutputStream os = response.getOutputStream();int len = is.read();while (len != -1){os.write(len);len = is.read();}is.close();os.close();%>上述代碼實現了檔案內容的傳輸,通過application.getResourceAsStream("upload.jsp")取得web路徑下的資源檔upload.jsp的輸入資料流,然後寫到response的輸出資料流中。(4) 返回某個虛擬路徑所映射的本地檔案系統路徑,如<%ServletInputStream sis = request.getInputStream();String filepath = application.getRealPath("/upload.txt");FileOutputStream fos = new FileOutputStream(filepath);int len = sis.read();while (len != -1){fos.write(len);len = sis.read();}fos.close();%>上述代碼將請求資訊中的實體內容寫到web路徑下的檔案upload.txt中。

out對象

out是抽象類別javax.servlet.jsp.JspWriter類型的一個執行個體對象,用於向用戶端進行寫操作。void clear();void clearBuffer();void close();int getBufferSize();int getRemainning();boolean isAutoFlush();void newLine();void print();void println();

config對象

web.xml配置資訊,貌似跟application對象的“web程式初始化參數設定”類似,將代碼中的application改成config即可。<%  out.println(config.getInitParameter("charset"));    Enumeration temp = config.getIniParameterNames();    while (temp.hasMoreElements())        out.println((String)temp.nextElement() + "<br>");   %>

exception對象

擷取異常資訊:String getMessage();將異常資訊及其追蹤輸出至標準錯誤流:void printStackTrace();

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.