JSP內(nèi)置對象
內(nèi)置對象指web容器創(chuàng)建的對象,不需要new來創(chuàng)建

out對象:
out對象是JspWriter類的實例,可以向客戶端輸出內(nèi)容。
<%
out.println("分別輸出緩存區(qū)大小,剩余大小,是否自動清除。");//依賴servlet庫,調(diào)用out對象的方法前需要引入tomcat的庫文件。
out.println(out.getBufferSize() + ";" + out.getRemaining() + ";" + out.isAutoFlush());
// print過程中可以用flush方法提前將緩沖內(nèi)容輸出。
%>
request對象
客戶端的請求會被封裝到request對象之中,在請求完成之前,request對象一直有效。
<form action="request.jsp" name="request" method="post">
\\傳遞至reques.jsp
form方式或url方式:
<a href="request.jsp?username=testusernme ">通過url地址參數(shù)傳遞信息</a>
<%--url傳中文可能會出現(xiàn)亂碼問題--%>
在request.jsp頁面中:
<%
request.setCharacterEncoding("utf-8");
// 可以通過該方法設(shè)定request傳遞過程中使用的字符集,無法解決url傳參時的亂碼問題
%>
username <%=request.getParameter("username")%><br>
url亂碼問題需要修改tomcat中server.xml中:

response對象
該對象具有頁面作用域。
<%
out.println("normal out ");
PrintWriter printWriter = response.getWriter();
printWriter.println("from writer"); //appears more faster than out
response.sendRedirect("index.jsp"); //send request to new page
%>
請求重定向:客戶端行為。response.sendRedirect("url")請求重定向相當(dāng)于兩次請求,只是上一次的請求對象不會保存,地址欄url會改變。
請求轉(zhuǎn)發(fā):服務(wù)器行為。上一次的請求對象會保存,地址欄url不會改變
request.getRequestDispatcher("request.jsp").forward(request,response);
session對象
session表示服務(wù)器與客戶端的一次會話,簡單來說就是進(jìn)入網(wǎng)站到離開網(wǎng)站的時間。保存在服務(wù)器內(nèi)存之中。session對象用于在網(wǎng)頁切換過程中保存用戶的狀態(tài)。
<%
session.setAttribute("username","edwin");
session.setMaxInactiveInterval(100); // set 100 second as the session's live time
%>
session create time :<%=session.getCreationTime()%><br>
<%=simpleDateFormat.format(date)%><br>
session id: <%=session.getId()%><br>
session attribute: <%=session.getAttribute("username")%><br>
session live time: <%=session.getMaxInactiveInterval()%>
session存活時間:
session對象默認(rèn)存活時間約30分鐘(Tomcat)。
session的存活時間可以設(shè)定。
session對象可以通過調(diào)用invalidate()銷毀。
session對象在服務(wù)器重啟后全部銷毀。
完全退出頁面后session依然存在,只是非活躍狀態(tài)。
//web.xml中配置session存活時間。(eg:10分鐘)
web.xml:
<session-config>
<session-timeout>10</session-timeout>
</session-config>
application對象
application對象實現(xiàn)了用戶之間數(shù)據(jù)的共享,可以存放全局變量。
application對象開始于服務(wù)器啟動,終止于服務(wù)器關(guān)閉。
通過set 與 get方法對屬性進(jìn)行操作,getAttributeNames可以獲得所有屬性名。
<%
application.setAttribute("city","shanghai");
application.setAttribute("postcode","10000");
application.setAttribute("email","111@163.com");
%>
city = <%=application.getAttribute("city")%>
<br>
application has attributes :
<%
Enumeration enumeration = application.getAttributeNames();
while (enumeration.hasMoreElements()){
out.println(enumeration.nextElement() + "-------" );
}
%>
page 對象
page 對象指向當(dāng)前page頁本身,類似于this指針。
pageContext 對象
pageContext對象提供了對jsp頁面所有對象及命名空間的訪問,如session,甚至application的某個屬性。
<%session.setAttribute("username","edwin"); %>
<label>get session's attribute by pageContext </label>
<%=pageContext.getSession().getAttribute("username")%>
//edwin
<%
pageContext.forward("index.jsp"); //跳轉(zhuǎn)到index頁面
pageContext.include("reg.jsp"); //頁面中嵌入reg頁面
%>
config 對象
config 對象在servlet初始化時,JSP引擎為servlet傳遞信息時會使用到,如servlet參數(shù)等。
exception 對象
異常對象,當(dāng)頁面產(chǎn)生異常時,就產(chǎn)生了該對象。JSP頁面要使用該頁面,需要將isErrorPage設(shè)為:true。
<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="exception.jsp" %>
//exception 交給exception.jsp處理。
//exception.jsp中,將isErrorPage置為true;
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
附:
get 與 post區(qū)別
表單提交一般格式如下:
<form action="actionName" method="get/post" accept-charset="utf-8">
</form>
get:
以明文方式通過url提交數(shù)據(jù),可以通過url看到數(shù)據(jù),且數(shù)據(jù)大小不超過2KB,但是效率高于post。用于安全性不高且數(shù)據(jù)量不大的操作,如搜索、查詢。
post:
將信息封裝于HTML HEADER中,用于高安全性且數(shù)據(jù)量大的情況,如注冊,上傳。
