request內置對象
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Request內置對象</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setAttribute("password", "123456");
request.setCharacterEncoding("utf-8");//設置接受表單時的請求接受默認編碼字符集為utf-8,與發出請求的字碼集相同
%>
<h2>request內置對象</h2>
用戶名:<%=request.getParameter("username") %><br>
密碼:<%=request.getAttribute("password") %><br> <!-- getAttriybute只能得到setAttribute對應的參數 -->
愛好:<%
if(request.getParameterValues("favorite")!=null){
String[] favorite = request.getParameterValues("favorite");
for(int i=0;i<favorite.length;i++) //一開始想該數組數據的值若為空,則.length為0,然而並不是
out.println(favorite[i]+" ");
}
%>
請求體的MIME類型:<%=request.getContentType() %><br>
協議類型及版本號:<%=request.getProtocol() %><br>
服務器主機名:<%=request.getServerName() %><br>
服務器端口號:<%=request.getServerPort() %><br>
文件的單位長度:<%=request.getContentLength() %>byte<br>
請求客戶端的IP地址:<%=request.getRemoteAddr() %><br>
請求的真是物理路徑:<%=request.getRealPath("Request.jsp") %><br>
請求的上下文路徑:<%=request.getContextPath() %><br>
</body>
</html>
request內置對象