文章目錄
-
-
- request對象
-
- 使用request對象接收請求參數
- 擷取請求的頭部資訊
- 擷取主機和客戶機的資訊
- response對象
-
- 設定頭資訊
- 設定頁面重定向
- session對象
-
- 擷取session ID
- 使用者登入資訊的儲存
- application對象
- out對象
- page對象
-
request對象
首先建立Java項目,然後添加Web子產品,以及配置Tomcat. 如果将request對象看作是客戶請求的一個執行個體,那麼這個執行個體就包含了客戶請求的所有資料。是以。可以通過request來擷取用戶端與伺服器端的資訊。如IP位址、傳遞的參數名和參數值、應用系統的名稱、服務主機名稱等。
使用request對象接收請求參數
request對象接收請求參數:一個是getParameter();另一個是getParameterValues()
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>'getParameter.jsp'</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body style="text-align: center;">
<%
//String name= new String(request.getParameter("name").getBytes("ISO-8859-1"),"utf-8");
String name= request.getParameter("name");
//String city= new String(request.getParameter("city").getBytes("ISO-8859-1"),"utf-8");
String city= request.getParameter("city");
if(name!=null&&city!=null)
{
%>
<p>Welcome <%=name %>,您所在的城市是<%=city %></p>
<%
}else{
%>
<p>歡迎通路本頁面!</p>
<%
}
%>
</body>
</html>
在URL參數傳遞時,頁面位址後用?連接配接請求參數;由參數=相連,表示參數名和參數位址;一個請求攜帶多個參數時用&連接配接。
在表單form中,有兩個非常重要的屬性:action和method。屬性action表示表單送出後跳轉到指定頁面處理參數。method有兩個值,分别是post和get。get,表示在表單中設定的參數和參數值将附加在頁面位址的末尾的形式送出,這樣不安全,而post則不會。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>'ex3_1.jsp'</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
<form action="getParameter.jsp" method="post">
<table align="center">
<tr>
<td>姓名</td>
<td><input type="text" name="name" value=""/></td>
</tr>
<tr>
<td>城市</td>
<td><input type="text" name="city" value=""/></td>
</tr>
<tr>
<td><input type="submit" value="送出"/></td>
<td><input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</body>
</html>
擷取請求的頭部資訊
請求頭部資訊在實際中也是十分重要的,這些資訊有時候對伺服器的響應特别有用,而且也能檢視到伺服器中的相關資訊。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>'getHeaders.jsp'</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body >
<%
Enumeration<String> enumeration = request.getHeaderNames();
while(enumeration.hasMoreElements()){
String name = enumeration.nextElement();
String value = request.getHeader(name);
if(value==null||"".equals(value)){
value="空字元串";
}
%>
<p>表頭名稱:<%=name %> 對應的值:<%=value %></p>
<%} %>
</body>
</html>
擷取主機和客戶機的資訊
getRemoteAddr():擷取客戶主機IP
getRemoteHost():擷取客戶主機名稱
getLocalAddr():擷取本地主機IP
getLocalHost():擷取本地主機名稱
getServerName ():擷取伺服器主機名稱
getServerPort():擷取伺服器端口
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>'getHostInfo.jsp'</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body >
<p>
本地機器IP:<%=request.getLocalAddr() %><br>
本地機器名稱:<%=request.getLocalName() %><br>
本地機器端口:<%=request.getLocalPort() %><br>
</p>
<p>
客戶主機IP:<%=request.getRemoteAddr() %><br>
客戶主機名稱:<%=request.getRemoteHost() %><br>
客戶主機端口:<%=request.getRemotePort()%><br>
</p>
<p>
伺服器IP:<%=request.getServerName() %><br>
伺服器端口:<%=request.getServerPort() %><br>
</p>
</body>
</html>
response對象
當使用者通路一個界面時,就會産生一個HTTP請求,伺服器做出響應時調用的是response響應包。
設定頭資訊
設定頭資訊包括設定頁面傳回的MIME類型、傳回的字元集、頁面中的meta等資訊。例如:
response.setContentType(String type):其中type的值為“text/html;charset=utf-8”
page指令:基本固定的格式為<%@ page language=“java” import=“java.util.*” pageEncoding=“UTF-8”%>
設定meta資訊是指在HTML頁面中存在于< h e a d head head>與< / h e a d /head /head>之間的資訊。
設定頁面重定向
重定向是指一個頁面在收到一個通路請求後,根據請求的URL重新跳轉到其他的頁面。
設定重定向的方法是:response.sendRedirect(String url)
session對象
擷取session ID
擷取session對象ID可以判斷會話是否是同一會話,使用者可以通過會話中的資訊來進行相關的操作。一個Web應用的session對象ID值是唯一的,并且兩個應用之間的參數利用session對象是擷取不到值的。
使用者登入資訊的儲存
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">
<title>使用者登入</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<div style="text-align: center;">
<span style="font-size: 26px;">使用者登入</span>
<hr/>
<form action="validate.jsp" method="post">
使用者名稱:<input type="text" name="username"/>
<br/>
使用者密碼:<input type="password" name="password"/>
<br/>
<input type="submit" value="登入"/>
</form>
</div>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>validate.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<%!
//聲明一個使用者集合,模拟從資料庫中取出使用者集
Map<String, String> map =new HashMap<String, String>();
//聲明驗證的辨別
boolean flag = false;
%>
<%
//向集合添加資料
map.put("John","123456");
map.put("Smith","222222");
map.put("Bob","333333");
map.put("Bruth","666666");
%>
<%!
//聲明驗證方法
boolean validate(String username,String password){
String passwd = map.get(username);
if(passwd!=null&&passwd.equals(password)){
return true;
}else{
return false;
}
}
%>
<%
//獲得頁面送出的使用者名跟密碼
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username==null||username==""||password==null||password==""){
response.sendRedirect("login.jsp");
}
flag = validate(username,password);
if(flag){
//儲存在session對象中
session.setAttribute("username",username);
session.setAttribute("password",password);
response.sendRedirect("class.jsp");
}
%>
<body>
<div style="text-align: center;">
<span style="font-size: 26px;">使用者登入</span>
</div>
<br/>
<div style="text-align: center;">
<%if(!flag){ %>
<a href="login.jsp">重新登入系統</a>
<%} %>
</div>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String name = (String)session.getAttribute("username");
if(name==null){
response.sendRedirect("login.jsp");
}
%>
<!DOCTYPE HTML>
<html>
<head>
<title>My JSP 'score.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<div style="text-align: center;">
<span style="font-size: 24px;">班級管理</span>
<hr/>
<h3>學生:<%=name %></h3>
<table align="center">
<tr>
<td>
<a href="addClass.jsp">班級錄入</a>
</td>
<td>
<a href="modifyClass.jsp">班級修改</a>
</td>
<td>
<a href="queryClass.jsp">班級查詢</a>
</td>
<td>
<a href="delClass.jsp">班級删除</a>
</td>
</tr>
</table>
<a href="logout.jsp">登出</a>
</div>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String username = (String)session.getAttribute("username");
session.removeAttribute("John");
session.invalidate();
response.sendRedirect("login.jsp");
%>
<!DOCTYPE HTML>
<html>
<head>
<title>My JSP 'logout.jsp' starting page</title>
</head>
<body>
</body>
</html>
application對象
當伺服器關閉時,application對象才消失,它可以視為Web的全局變量。 application對象還可以儲存通路網站的人數,也就是我們所說的網站計數器。
<%@ page language="java" pageEncoding="UTF-8"%>
<%
Integer count =(Integer) application.getAttribute("count");
if(count==null){
count=1;
}else{
count++;
}
application.setAttribute("count",count);
%>
<!DOCTYPE HTML>
<html>
<head>
<title>網站計數器</title>
</head>
<body>
歡迎通路本網站,您是第<%=count %>位通路客戶!
</body>
</html>
out對象
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>示範out對象</title>
</head>
<body>
<div style="text-align: center;">
<hr>
<h4>以下就是一個表格</h4>
<%
out.println("<table border='2' align='center'>");
out.println("<tr>");
out.println("<td width='60'>"+"姓名"+"</td>");
out.println("<td width='40'>"+"性别"+"</td>");
out.println("<td width='80'>"+"出生年月"+"</td>");
out.println("<td width='60'>"+"城市"+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='60'>"+"Smith"+"</td>");
out.println("<td width='60'>"+"Male"+"</td>");
out.println("<td width='60'>"+"1984.8"+"</td>");
out.println("<td width='60'>"+"NerYork"+"</td>");
out.println("</tr>");
out.println("</table>");
%>
</div>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>示範out對象clear方法</title>
</head>
<body>
<h4>這是HTML中的内容</h4>
<%
out.print("<h4>這是out對象輸出的資訊</h4>");
out.clear();
%>
<h4 style="text-align: center;">這是HTML中的資訊</h4>
</body>
</html>
若在JSP頁面中調用clear方法,那麼之前向用戶端輸出流中寫入的資料都将被清除。