標籤:
JSP的第一個對象為out對象 為向用戶端輸出資訊
//out表示向用戶端輸出各種資料 //對輸出緩衝區進行管理 可以通過page屬性來改變緩衝區out.println("操");out.print("cao");out.newLine();out.println("<br/>");out.print("cao");
重新整理緩衝區 可以用
//強制重新整理緩衝區的資料out.flush();
清空緩衝區 也就是前面輸出會清空掉
//清空緩衝區中的資料out.clearBuffer();out.clear();
out.println("擷取當前的緩衝區大小:" + out.getBufferSize());out.println("當前緩衝區剩餘位元組數目:" + out.getRemaining());
區別是clear和flush會衝突 而clearBuffer不會與flush進行衝突
request對象 用於封裝用戶端的請求
請求的方法名 <%= request.getMethod() %> <br/>請求的url <%= request.getRequestURI() %>擷取請求的協議<%=request.getProtocol() %>擷取伺服器的IP<%=request.getServerName() %>請求的伺服器連接埠<%=request.getServerPort() %>用戶端的IP地址<%= request.getRemoteAddr() %>用戶端的主機名稱<%= request.getRemoteHost() %><!--擷取表單資料 參數參數跟表單名稱一致-->
首先建立一個提交的表單
<pre name="code" class="java"><form action = "do_register.jsp" method = "post">使用者名稱 <input type = "text" name = "userName"/></br>技能 <input type = "checkbox" name = "skills" value = "java">java<input type = "checkbox" name = "skills" value = "OC">OC<input type = "checkbox" name = "skills" value = "Swift">Swift</br><input type = "submit" value = "提交"/><input type = "reset" value = "重設"/></form>
隨後在do_register.jsp中回去參數後,看是否成功 隨後配置property來進行設定
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><%//getParameter方法獲得使用者名稱和技能數組String userName = request.getParameter("userName");String skills = "";String skillArr[] = request.getParameterValues("skills");for(int i = 0; i < skillArr.length; i++){skills += skillArr[i];}//setAttribute方法request.setAttribute("userName",userName);request.setAttribute("skills",skills);%><!-- forward跳轉指令 跳轉到新的介面 --><jsp:forward page="welcome.jsp"></jsp:forward>這裡是處理註冊是否成功,成功後跳轉到welcome.jsp檔案
用getParameter來擷取
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>資訊展示</title></head><body>使用者名稱<%= request.getParameter("userName")%>技能<%=request.getParameter("skills")%></body></html>這裡要區分一下 getParameter來處理用戶端的請求 而setAttribute來表示webPages之間的傳值
response方法,用於返回用戶端的請求
<%//設定頭部的資訊 設定網頁資料不緩衝response.setHeader("cache-Control","no-cache");//設定每兩秒重新整理一次response.setIntHeader("Refresh",2);out.println("date is " + new java.util.Date().toString());%>
設定頁面的直接跳轉
//實現頁面的跳轉response.sendRedirect("http://www.baidu.com");session對象 用於設定會話 在瀏覽器向伺服器請求時,有唯一的一個sessionID 伺服器會進行判定 相同的才給予響應
<%@page import="java.util.Date"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Session</title></head><body>session 的唯一標示符 32位長字元<%=session.getId() %>session的建立時間<%= new java.util.Date( session.getCreationTime()).toString() %>session的最後訪問時間<%=session.getLastAccessedTime() %>session的失效時間 <%=session.getMaxInactiveInterval()%></body></html>
實現一個登陸驗證 首先在Login建立一個表單
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form acton = "do_login.jsp" method = "post">userName:<input type = "text" name = "userName"/>password:<input type = "password" name = "password"/><input type = "submit" value = "submit"><input type = "reset" value = "reset"></form></body></html>
隨後在驗證表單do_login.jsp中進行登陸的驗證
session中配置屬性
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><%String userName = request.getParameter("userName");String password = request.getParameter("password");//驗證是否登入成功if(userName != null && password != null){//會話中配置java對象session.setAttribute("userName",userName);//隨後重新整理跳轉到歡迎介面response.setHeader("refresh","2;URL=welcome.jsp");}%>隨後在歡迎介面根據session獲得的不同值來輸出不同的值
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>歡迎介面</title></head><body><% if(session.getAttribute("userName") != null) { %>歡迎 <%=session.getAttribute("userName") %><a href = "logout.jsp">登出</a></br><%else { %>請先登入<a href = "login.jsp">登入</a><%} %><%if(session.isNew()) {%><br/>歡迎新使用者<%}else{ %>歡迎老使用者<%} %></y></html>
隨後在退出登陸的介面 來進行退出
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%//退出的代碼session.invalidate();response.setHeader("refresh","2;URL=welcome.jsp");%>
config屬性主要在severlt中進行配置 可以在WEB-INF目錄下的web.xml檔案中進行配置
application屬性 用於設定全域的變數
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>application</title></head><body>伺服器資訊<%=application.getServerInfo() %></br>應用程式名稱<%=application.getServletContextName()%><br/>主機名稱<%=application.getVirtualServerName() %></br></body></html>
可以做一個簡單的計數器 如果頁面是第一次訪問的話 就輸出值,隨後儲存到application中,如果是第二次開始訪問的,那麼就從application中取出值,隨後進行++和存值得操作
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>頁面訪問計數器</title></head><body><%Object obj = application.getAttribute("counter");//判斷是否為空白 空為第一次if(obj == null){application.setAttribute("counter",new Integer(1));out.println("該頁面被訪問了1次<br/>");}else{int counterValue = Integer.parseInt(obj.toString());counterValue++;out.println("該頁面被訪問了" + counterValue + "次<br/>");application.setAttribute("counter",counterValue);}%></body></html>page屬性相當於java中的this指標,用於指向本頁面,其是繼承自object類的
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>page對象的字串<%=page.toString() %>當前的object類<%=page.getClass() %>當前的hashCode值<%=page.hashCode() %>繼承自object類</body></html>
異常類必須配置error_page屬性
在throw_error.jsp中人為的指定一個錯誤,隨後進行拋出,指定錯誤介面為handle_error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page errorPage="handle_error.jsp" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>拋出異常類</title></head><body><%int a = 10;int b = a / 0;%></body></html>
隨後在handle_error.jsp中 指定isErrorPage = true 隨後輸出一些異常的資訊即可
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page isErrorPage="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>異常處理</title></head><body>異常描述資訊<%out.println(exception.getMessage());%>exception對象的字串描述<%=exception.toString() %>異常堆棧資訊<%=exception.printStackTrace() %></body></html>
page_Context可以說是一個集大成者,他可以轉換為out 但是系統預設已經帶有一個out了,所以自己命名的話只能命名為其他名稱
<%JspWriter myOut = pageContext.getOut();myOut.println("hello world");也可以在session 中設定值,隨後進行取值和輸出
pageContext.setAttribute("dakim","cao",pageContext.SESSION_SCOPE);String value = session.getAttribute("dakim").toString();out.println(value);
好的部落格可以參考
http://blog.csdn.net/yenange/article/details/5949518
JSP9個內建對象