out对象
out 对象是 javax.servlet.jsp.JspWriter类的一个实例。out 对象的作用域是本页面,因此对于每一个 JSP 页面,都有一个 out 对象。out 对象,用于各种数据的输出。它表示为客户打开的输出流, PrintWriter 使用它向客户发送输出。注意:通过使用页面的 buffer 属性,能调整缓冲区的大小,甚至可以关闭缓冲区。out.println(“…”); out.print(“…”); //输出一个换行符 out.newLine(); //输出缓冲区的内容 out.flush(); //关闭输出流 out.close();
response对象
-
重定向网页
-
response.sendRedirect(“index.jsp”);
和< jsp:forward > 指令的最大区别就是:< jsp:forward > 只能在本网站内跳转, response.sendRedirect 可以跳转到任何一个地址的页面。
-
动态contentType响应
-
当请求一个 JSP 页面的时候,如果该页面用 page指令设置页面的 contentType属性的值是 text/html,那么服务器将把静态页面部分发送给客户端。也可以设置为其他一些相应类型,如: text/plain(文本文件)、 application/x- msexcel( Excel文件)和 application/ msword( Word文件)等。
<%@ page contentType="text/html;charset=GBK" %> <HTML> <BODY> <P>response对象 <BR>setContentType方法 <P>将当前页面保存为word文档吗 <% response.setContentType("application/msword;charset=GB2312"); %> </BODY> </HTML>
-
设 置缓冲区
-
<%@ page contentType="text/html;charset=GBK" %> <% response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", -1); %> <% out.println("设置完毕!");%>
-
HTTP文件响应
<%@ page contentType="text/html;charset=GBK" %> <%@ page import="java.util.*" %> <P>现在的时间是:<BR> <% out.println(""+new Date()); response.setHeader("Refresh","5"); %>
-
request对象
1. 用户在客户端的表单中填写信息2. 通过 GET 或者 POST方法把填写的信息提交到服务器上3. 调用相应的 Request 对象处理信息。注意:借助Request对象从客户端的到的数据有String型。数值计算时,要类型转换
4. 将 JSP 文件处理结果返回给用户。request获得表单信息:String name=request.getParameter(name);
//常用方法
getProtocol:获取客户取得的协议 getServletPath:获取客户提交信息的页面 getContentLength:获取客户提交信息的长度 getMethod:获取客户提交信息的方式 getHeader:获取HTTP文件中User-Agent、accept、accept-encoding、Host的值 getRemoteAddr:获取客户的IP地址 getRemoteHost:获取客户机的名称 getServerName:获取服务器的名称 getServerPort:获取服务器的端口号
application对象
public void setAttribute(String key,Object obj)//将对象obj添加到application对象中,并为其指定索引关键字key。
public Object getAttribute(String key)//获取application对象中含有关键字key的对象。由于任何对象都可以添加到application中,
//因此用此方法取回对象的时候,需要强制转化为原来的类型。
public void removeAttribute(String key)移去application对象中含关键字key的对象。
count.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY>
<%
Integer number=(Integer)application.getAttribute("Count");
if(number==null) {
number=new Integer(1);
application.setAttribute("Count",number);
}
else {
number=new Integer(number.intValue() + 1);
application.setAttribute("Count",number);
}
%>
您是第<%=(Integer)application.getAttribute("Count")%>
个访问本站的客户。
</BODY>
</HTML>
count_image.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY>
<%!
String G(Integer counter)
{
String S, myimage;
myimage = "";
S = counter.toString() ;
for(int i = 0; i<S.length(); i++)
{
myimage = myimage + "<IMG SRC=image/" + S.charAt(i) + ".GIF>";
}
return myimage;
}
%>
<%
Integer number=(Integer)application.getAttribute("Count");
if(number==null) {
number=new Integer(1);
application.setAttribute("Count",number);
}
else {
number=new Integer(number.intValue() + 1);
application.setAttribute("Count",number);
}
%>
您是第<%=G((Integer)application.getAttribute("Count"))%>
个访问本站的客户。
</BODY>
</HTML>
session对象
public void setAttribute(String key,Object obj)//将对象obj添加到session中,并为其指定索引关键字key。
public Object getAttribute(String key)//获取session中含有关键字key的对象。由于任何对象都可以添加到session中,因此用此方法取回对象的时候,需要//强制转化为原来的类型。
public void removeAttribute(String key)//移去session中含关键字key的对象。
public Boolean isNew()//判断是否是一个新的客户
buy1.jsp
<%@ page contentType="text/html;charset=GBK" %>
<HTML>
<BODY>
<%
if (request.getParameter("c1") != null ){
session.setAttribute("s1", request.getParameter("c1"));
}
if (request.getParameter("c2") != null ){
session.setAttribute("s2", request.getParameter("c2"));
}
if (request.getParameter("c3") != null ){
session.setAttribute("s3", request.getParameter("c3"));
}
%>
各种肉大甩卖,一律十块:<br>
<FORM METHOD="POST" action="buy1.jsp">
<p><input type="checkbox" name="c1" value="猪肉">猪肉</p>
<p><input type="checkbox" name="c2" value="牛肉">牛肉</p>
<p><input type="checkbox" name="c3" value="羊肉">羊肉</p>
<p><input type="submit" value="提交" name="B1">
<input type="reset" value="全部重写" name="B2">
<a href="buy2.jsp">买点别的</a>
<a href="display.jsp">查看购物车</a> </P>
</FORM>
</BODY>
</HTML>
buy2.jsp
<%@ page contentType="text/html;charset=GBK" %>
<HTML>
<BODY>
<%
if (request.getParameter("b1") != null ){
session.setAttribute("s4", request.getParameter("b1"));
}
if (request.getParameter("b2") != null ){
session.setAttribute("s5", request.getParameter("b2"));
}
if (request.getParameter("b3") != null ){
session.setAttribute("s6", request.getParameter("b3"));
}
%>
各种球大甩卖,一律八块:
<form method="POST" action="buy2.jsp">
<p><input type="checkbox" name="b1" value="篮球">篮球</p>
<p><input type="checkbox" name="b2" value="足球">足球</p>
<p><input type="checkbox" name="b3" value="排球">排球</p>
<p><input type="submit" value="提交" name="x1">
<input type="reset" value="全部重写" name="B2">
<a href="buy1.jsp">买点别的</a>
<a href="display.jsp">查看购物车</a>
</P>
</FORM>
</BODY>
</HTML>
display.jsp
<%@ page contentType="text/html;charset=GBK" %> <HTML> <BODY> 你选择的结果是: <center> <% String str = ""; if(session.getAttribute("s1") != null){ str = (String)session.getAttribute("s1"); byte b[]=str.getBytes("ISO-8859-1"); str=new String(b); out.print(str + "<br>"); } if(session.getAttribute("s2")!= null){ str = (String)session.getAttribute("s2"); byte b[]=str.getBytes("ISO-8859-1"); str=new String(b); out.print(str + "<br>"); } if(session.getAttribute("s3")!=null){ str = (String)session.getAttribute("s3"); byte b[]=str.getBytes("ISO-8859-1"); str=new String(b); out.print(str + "<br>"); } if(session.getAttribute("s4")!=null){ str = (String)session.getAttribute("s4"); byte b[]=str.getBytes("ISO-8859-1"); str=new String(b); out.print(str + "<br>"); } if(session.getAttribute("s5")!=null){ str = (String)session.getAttribute("s5"); byte b[]=str.getBytes("ISO-8859-1"); str=new String(b); out.print(str + "<br>"); } if(session.getAttribute("s6")!=null){ str = (String)session.getAttribute("s6"); byte b[]=str.getBytes("ISO-8859-1"); str=new String(b); out.print(str + "<br>"); } %> </center> </BODY> </HTML>