javaweb基础系列之二

一.谈谈属性保存范围

在jsp中可以通过setAttribute()和getAttribute()方法来设置和取得属性,从而实现数据的共享。同样,jsp中提供了4钟属性保存范围,分别为page、request、session、application。

1.page范围

所谓page范围,就是指设置的属性只在当前页面有效。通过使用pageContext的setAttribu()方法来设置属性,通过getAttribute()方法来获得属性。

2.request范围

所谓request范围,就是指属性在一次请求范围内有效,如果页面从一个页面跳转到另一个页面,那么该属性就失效了。这里所指的跳转是指客户端的跳转,比如客户单机超链接跳转到其他页面,或者通过浏览器地址栏浏览其他网页,如果使用服务端跳转<jsp:forward>,则该属性仍然有效。通过使用request的getAttribute()方法仍可获得属性。

一个例子说明问题:客户端跳转

requestScopeDemo.jsp

<%@  page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>request属性范围</head>
<body>
<%request.setAttribute("name","xuzhuyun");%>
<a href="RequestScopeDemo2.jsp">跳转到RequestScopeDemo2.jsp</a>
</body>
</html>
requestScopeDemo2.jsp

<%@  page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>request属性范围</head>
<body>
<%
String name1 =(String)request.getAttribute("name");
out.println("name的值为:"+name1);
%>
</body>
</html>
name值为null,因为客户端跳转是无法获得request范围的属性的,如果使用服务器端跳转,则可以获得request范围属性。此时将<a href>标签用<jsp:forward>替代,则可以访问到该属性值。

<%@  page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>request属性范围</head>
<body>
<%
String name1 =(String)request.setAttribute("name","xuzhuyun");%>
<jsp:forward page="RequestScopeDemo2.jsp"></jsp:forward>
</body>
</html>

3.session范围

session范围是指客户浏览器与服务器的一次回话范围内,如果和服务器断开连接,那么这个属性也就失效了。通过setAttribute()方法来设置属性,通过getAttribute()方法取得属性。

SessionScope.jsp

<%@  page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>session属性范围</head>
<body>
<%
session.setAttribute("name","xuzhuyun");%>
<a href="RequestScopeDemo2.jsp">跳转到RequestScopeDemo2.jsp</a>
</body>
</html>
SessionScope2.jsp

<%@  page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>session属性范围</head>
<body>
<%
 String name1 =(String)session.getAttribute("name");
out.println("name的值为:"+name1);
%>
</body>
</html>
name返回结果为xuzhuyun。无论是客户端跳转还是服务器端跳转,都能获得session范围的属性,但是如果重新打开浏览器,就不能获得session范围属性了,因为回话已经结束了。

4.application范围

application范围,也就是指在整个服务器范围,直到服务器停止以后才会生效。所以不管是客户端跳转还是服务器端跳转,或者是否是重新打开浏览器,只要服务器一直在运行,就会取得application范围属性。


关于以上范围的属性的移除方法:removeAttribute()

<%@  page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>移除属性</head>
<body>
<%
pageContext.setAttribute("name","xuzhuyun");
pageContext.setAttribute("age",22);
pageContext.setAttribute("sex","男");
%>

<%
 pageContext.removeAttribute("sex");
%>

<%
 String strname = pageContext.getAttribute("name");
 String strage = pageContext.getAttribute("age");
 String strsex = pageContext.getAttribute("sex");

 out.println(strname);
 out.println(strage);
 out.println(strsex);
%>
</body>
</html>
结果只有sex为null 因为属性被移除。

二、常见内置对象的一些常见方法

1.request对象

 request对象包括一系列方法用来获得客户端请求参数,包括的方法以及方法说明如下:


javaweb基础系列之二_第1张图片

获得所有的参数名称:

一个表单

<%@  page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>表单</head>
<body>
<form action = "RequestDemo2.jsp" method="post">
	用户名:<input type= "text" name ="username"><br>
	密码:<input type = "password" name="userpwd"><br>
	<input type ="submit" value="提交">
</form>
</body>
</html>

取所有的参数名称

<%@  page language="java" contentType="text/html;charset=gb2312" import="java.util.*"%>
<html>
<head>获得所有的参数名称</head>
<body>
<%
	Enumeration e = request.getParameterNames();
	While(e.hasMoreElements()){
	String str = (String)e.nextElement();
	out.println(str);
	}
%>
</form>
</body>
</html>

获得参数的所有的参数值

前面接收的参数都只有一个参数值,某些情况下一个参数可能有多个参数值,比如多选框,多选列表,要接收多个参数,需要使用request的getParameterValues()方法

一个实例:

<%@  page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>表单</head>
<body>
<form action = "RequestDemo2.jsp" method="post">
	用户名:<input type= "text" name ="username"><br>
	密码:<input type = "password" name="userpwd"><br>
	喜欢的运动:
	<input type="checkbox" name ="sport" value = "pingpong">乒乓
	<input type="checkbox" name ="sport" value = "basketball">篮球
	<input type="checkbox" name ="sport" value = "football">足球
	<input type ="submit" value="提交">
</form>
</body>
</html>

取值

<%@  page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>表单</head>
<body>
<%
	Stirng[] strArr = request.getParameterValues("sport");
	out.println("喜欢的运动");
	For(String str : strArr){
		out.println(str);
	}
%>
</body>
</html>

获得封装所有参数值的Map

通过request对象的getParameterMap()方法来获得封装所有参数值的Map对象,通过该Map对象可以获得指定参数的参数值

<%@  page language="java" contentType="text/html;charset=gb2312" import="java.util.*"%>
<html>
<head>表单</head>
<body>
<%
	Map mapParameter = request.getParameterMap();//获得封装所有参数的Map
	String[] strUsername=(String[])mapParameter.get("username");
	out.println("用户名:"+strUsername);
	String[] strUserpwd=(String[])mapParameter.get("userpwd");
	out.println("密码:"+strUserpwd);
	String[] strSport = (String[])mapParameter.Get("sport");
	out.println("喜欢的运动");
	For(String sport : strSport){
		out.println(sport);
	}
%>
</body>
</html>

2.response对象

response对象用来给客户端传送输出信息。response对象包含的方法及方法说明如下图所示:

javaweb基础系列之二_第2张图片

response实现页面跳转:

通过response对象的sendRedirect方法设置页面重定向,从而实现页面跳转,这种跳转将改变浏览器地址栏信息,所以也称为客户端跳转。

response.sendRedirect(“DirectPage.jsp”); // 进行页面跳转

response实现自动刷新

 response有以下四个方法可以实现jsp页面自动刷新,分别是addHeader(String name,String value)、addIntHeader(String name, int value);、setHeader(String name,String value);、setIntHeader(String name,int value);注意传的参数就行。
----------------------------------------------------------------------------------------------------------------------------------
另外addHeader(String name,String value)和setHeader(String name,String value)还可以实现在指定若干秒后跳转到指定页面的功能,举例如下:
response.addHeader("Refresh","3;url= http://www.baidu.com");//3秒后跳转到百度主页
response.setHeader("Refresh","5;url= http://www.baidu.com");//5秒后跳转到百度主页

3.out对象

用来向网页输出信息,out对象有的方法如下:

javaweb基础系列之二_第3张图片

out对象进行页面输出
out.print(“hello”);

out.println(“hello.world”);

使用out对象求得缓冲区使用大小

调用getBufferSize()方法可以获得缓冲区的大小,通过getRemaining()方法可以获得缓冲区的剩余大小,通过这两个值可以很容易的求得缓冲区使用过大小

int all = out.getBufferSize();//获得缓冲区总大小

int remain = out.getRemaining();//获得缓冲区剩余大小

int use = all - remain;//得到使用的缓冲区大小

4.session对象

session对象用来表示拥护的回话状况,一般用于保存用户的各种信息,知道生命周期超时或者被认为释放掉为止,session对象包含的方法:

javaweb基础系列之二_第4张图片

设置并获得session生命周期

session.setMaxInactiveInterval(60*2);//设置生命周期为120秒

int time = session.getMaxInactiveInterval();//获得生命周期时间

获得session其他信息

long createTime = session.getCreateTime();//取得session生成的时间

String sessionID = session.getId();//取得session的id

long lastTime = session.getLastAccessedTime();//取得最后session发送请求的时间

boolean isnew= session.isNew(); //判断session是否为新

5.application对象

application对象用来取得和设置servlet相关信息,application对象生命周期是从服务器启动产生知道服务器关闭为止。常见方法如下:

javaweb基础系列之二_第5张图片

6.pageContext对象

pageContext对象不但可以设置page属性,也可以设置其他范围的属性,不过需要指定范围参数,通过pageContext对象还可以获得其他内置对象,常用方法如下:

javaweb基础系列之二_第6张图片

通过 pageContext取值的时候注意带上范围:

设置:

pageContext.setAttribute("name","page_xuzhuyun");

request.setAttribute("name","request_xuzhuyun");

session.setAttribute("name","session_xuzhuyun");

application.setAttribute("name","application_xuzhuyun");

取值:

String pageStr = (String)pageContext.getAttribute("name",pageContext.PAGE_SCOPE);

String requestStr = (String)pageContext.getAttribute("name",pageContext.request_SCOPE);

String sessionStr = (String)pageContext.getAttribute("name",pageContext.SESSION_SCOPE);

String applicationStr = (String)pageContext.getAttribute("name",pageContext.application_SCOPE);

7.page对象

page对象代表JSP转译后的Servlet,通过page对象可以非常方便的调用Servlet类中定义的方法.通过page调用Servlet类中定义的方法实例:

<%@ page language="java" contentType="text/html;charset=gbk"%>
<%@ page info = "hello xuzhuyun!"%>
<html>
 <head>
  <title> page对象 </title>

 </head>

 <body>
  <%-- 通过page对象调用servlet中的getServletInfo()方法--%>
  <%
	String info = ((javax.servlet.jsp.HttpJspPage)page).getServletInfo();
  %>
  <%= info%>
 </body>
</html>

结果为:hello xuzhuyun

7.config对象

config对象可以取得servlet配置信息

javaweb基础系列之二_第7张图片

8.exception对象

exception对象用来处理错误异常,如果要使用exception对象,必需指定page中的isErrorPage属性值为true

<%@ page language="java" contentType="text/html;charset=gbk" isErrorPage="true"%>

一个小例子:通过exception对象处理错误异常

将错误结果返回的页面ExceptionDemo01.jsp

<%@ page language="java" contentType="text/html;charset=gbk" isErrorPage="true"%>
<%@ page import = "java.io.PrintStream"%>
<html>
 <head>
  <title> 处理错误异常 </title>

 </head>
 <body>
  <%=exception%><br>
  <%=exception.getMessage()%><br>
  <%=exception.getLocalizedMessage()%><br>

  <%exception.printStackTrace(new java.io.PrintWriter(out));%>
 </body>
</html>

出错的页面ExceptionDemo02.jsp

<%@ page language="java" contentType="text/html;charset=gbk" errorPage="exceptionDemo01.jsp"%>
<%@ page import = "java.io.PrintStream"%>
<html>
 <head>
  <title> 错误页面 </title>

 </head>

 <body>
  <%
	int[] arr = {1,2,3};
	out.print(arr[3]);
  %>
 </body>
</html>

很显然数组越界:

javaweb基础系列之二_第8张图片






   

你可能感兴趣的:(java,java,Web,Web)