1、Out對象
主要用來向客戶端輸出各種格式的數據,並且管理應用服務器上的輸出緩沖區,Out對象的基類是 javax.servlet.jsp.JspWriter類。
Out的主要方法:
out.println(DataType);或out.print(DataType);
實例:
<%@page language="java"
contentType="text/html;charset=gb2312"
import="java.util.*"
%>
<HTML>
<HEAD>
<TITLE> out對象使用實例</TITLE>
</HEAD>
<BODY>
<%
out.println(new Date().toLocaleString());
out.print("<BR>");
out.print("測試成功");
%>
</BODY>
</HTML>
2、Request對象
Request.setAttribute()與Request.getAttribute()方法實例(也可以使用forward建立請求關系):
主頁面:
<%@page language="java"
contentType="text/html;charset=gb2312"
%>
<%@ include file="1.jsp" %>
你剛才輸入的內容是:
<%=request.getAttribute("gr")%>
引入頁面:
<%@page language="java"
contentType="text/html;charset=gb2312"
import="java.util.*"
%>
<HTML>
<HEAD>
<TITLE> request對象使用實例</TITLE>
</HEAD>
<BODY>
<%
request.setAttribute("gr","123333");
%>
</FORM>
</BODY>
</HTML>
Request.getParameter()方法實例
主頁面
<%@page language="java"
contentType="text/html;charset=gb2312"
import="java.util.*"
%>
<HTML>
<HEAD>
<TITLE> request.getParameter()方法使用實例</TITLE>
</HEAD>
<BODY>
<FORM METHOD=POST ACTION="2.jsp">
<INPUT TYPE="text" NAME="gr1"><BR>
<INPUT TYPE="text" NAME="gr2"><BR>
<INPUT TYPE="text" NAME="gr3"><BR>
<INPUT TYPE="submit" NAME="submit" value="提交">
<INPUT TYPE="reset" NAME="reset" value="清除">
</FORM>
</FORM>
</BODY>
</HTML>
引入頁面
<%@page language="java"
contentType="text/html;charset=gb2312"
%>
你剛才輸入的內容是:<BR>
<%=request.getParameter("gr1")%><BR>
<%=request.getParameter("gr2")%><BR>
<%=request.getParameter("gr3")%><BR>
Request.getParameterName()方法實例
主頁面
<%@page language="java"
contentType="text/html;charset=gb2312"
%>
<HTML>
<HEAD>
<TITLE> request.getParameterName()方法使用實例</TITLE>
</HEAD>
<BODY>
<FORM METHOD=post ACTION="2.jsp">
<INPUT TYPE="text" NAME="gr1"><BR>
<INPUT TYPE="text" NAME="gr2"><BR>
<INPUT TYPE="text" NAME="gr3"><BR>
<INPUT TYPE="submit" value="提交">
<INPUT TYPE="reset" value="清除">
</FORM>
</FORM>
</BODY>
</HTML>
指向頁面
<%@page language="java"
contentType="text/html;charset=gb2312"
import="java.util.*"
%>
你剛才輸入的內容是:<BR>
<%
Enumeration e=request.getParameterNames();
while(e.hasMoreElements()){
String parameterName=(String)e.nextElement();
String parameterValue=(String)request.getParameter(parameterName);
out.print("參數名稱:" parameterName "<BR>");
out.print("參數內容:" parameterValue "<BR>");
}
%>
Request.getAttributeName()方法實例
主頁面:
<%@page language="java"
contentType="text/html;charset=gb2312"
import="java.util.*"
%>
<HTML>
<HEAD>
<TITLE> request.getAttributeName()方法使用實例</TITLE>
</HEAD>
<BODY>
<jsp:include page="2.jsp" flush="true"/>
<%
Enumeration e=request.getAttributeNames();
while(e.hasMoreElements()){
String attributeName=(String)e.nextElement();
String attributeValue=(String)request.getAttribute(attributeName);
out.print("變量名稱:" attributeName);
out.print("變量內容:" attributeValue "<BR>");
}
%>
</FORM>
</FORM>
</BODY>
</HTML>
轉向頁面
<%@page language="java"
contentType="text/html;charset=gb2312"
%>
<%
request.setAttribute("gr1","111");
request.setAttribute("gr2","222");
request.setAttribute("gr3","333");
%>
request.getRemoteAddr()方法實例: