JSP内置对象

九个内置对象

·· 在JSP中为了简化用户的开发,提供了9个内置对象,这些内置对象将由容器自动为用户进行实例化,用户直接使用即可

  1. pageContext:JSP的页面容器
  2. request:代表请求,在request对象中封装了很多的方法,可以让我们去获取请求的信息,例如发送请求的时候传递的参数等等
  3. response:代表服务器发送给页面的响应,在response对象中封装了响应的信息,例如我们可以获取到响应的内容。
  4. page:代表当前的页面信息
  5. config:代表的配置信息,我们可以通过config设置 一些初始化的参数
  6. session:代表一次会话,一个会话是指用户在第一次访问服务器到结束对服务器的访问为止的这么一个过程。在session中我们可以保存用户的信息。
  7. application:代表整个服务器,我们可以向application中设置一些信息,让所有访问这个服务器的所有的用户进行共享
  8. exception:代表页面中出现的异常的信息
  9. out:我们可以使用out进行页面的输出,此输出流是将内容输出到页面上进行显示。
    常用:request、response、session、application、pageContext常用。

四种属性得范围

  1. pageContext:只在一个页面中保存属性,跳转之后无效。
  2. request:只在一次请求中保存属性,服务器跳转后依然有效。
  3. session:在一次会话范围中保存,无论何种跳转都可以使用,但是新开浏览器无法使用。
  4. application:在整个服务器上保存,所有用户都可以使用。

统一方法

  1. public void setAttribute(String name,Object o):此方法是用来存储属性的,存储属性的时候,采用了key-value的形式进行存储,其中的参数name是字符串类型的,代表key,o是Object类型的,代表value.
  2. public Object getAttribute(String name):此方法是通过参数name去获取属性的值,此方法的返回值是一个Object类型的。
  3. public void removeAttribute(String name):此方法是通过参数name去删除对应的属性。

特殊方法

setAttribute(String name, Object value,int scope);此方法可以设置属性并且指定属性的保存方法,此方法是pageContext特有的,其他三个属性范围对象没有。其中scope代表范围,取值:

  1. APPLICATION_SCOPE:代表application范围
  2. SESSION_SCOPE:代表session范围
  3. REQUEST_SCOPE:代表request范围
  4. PAGE_SCOPE:代表page范围

最频繁得对象

  • Request内置对象是使用最多的一个对象,其主要作用是接收客户端发送而来的请求信息,如请求参数、发送的头信息等都属于客户端发来的信息。
  • Request是javax.servlet.http.HttpServletRequest接口的实例化对象,表示此对象主要是应用在HTTP协议上。Javax.servlet.http.HttpServletRequest接口的定义如下:
    public interface HttpServletRequest extends ServletRequest
    从定义中可以发现,HttpServlettRequst接口是ServletRequest接口的子接口,所以在查找request对象方法时除了要查询HttpServletReuest接口,也要查询SevletRequest接口。

pageContext客户端跳转和服务器跳转得反应

  1. 只在本页面显示,不管时服务器跳转还是客户端跳转;属性值都不会传递过去。
  2. 超连接属于客户端跳转
  3. jsp:forward</jsp:forward>属于服务器跳转,直接就跳了无需点击。
    注意:jsp:forward</jsp:forward>中不能像超链接一样写文本
 <body>
    
    <%
    	pageContext.setAttribute("info1", "pageContext.helloword");
    
     %>
     info1:<%=pageContext.getAttribute("info1") %>
     <a href="MyJsp.jsp">客户端跳转</a>
     <jsp:forward page="MyJsp.jsp"></jsp:forward>
    
  </body>

request设置属性得范围测试

只在服务器跳转内有值
本质上是因为服务器访问只响应一次;request得属性值保存在这一次响应中;客户端需要服务器两次响应;故访问不到。
index.jsp

 <body>
    
    <%
    request.setAttribute("info1", "request.helloword");
    
     %>
     info1:<%=request.getAttribute("info1") %>
     <a href="MyJsp.jsp">客户端跳转</a>
     <jsp:forward page="MyJsp.jsp"></jsp:forward>
    
  </body>

MyJsp.jsp

 <body>
 info: <%=request.getAttribute("info1") %>
  
    This is my JSP page. <br>
  </body>

session服务器跳转

限制条件是一次会话内,在一次会话内不管是服务器跳转还是客户端跳转,都能正常传值。
本页面显示和客户端跳转

<body>
    
    <%
   session.setAttribute("info1", "session.helloword");
    
     %>
     info1:<%=session.getAttribute("info1") %>
     <a href="MyJsp.jsp">客户端跳转</a>
     
    
  </body>

跳转后得页面

 <body>
 info: <%=session.getAttribute("info1") %>
  
    This is my JSP page. <br>
  </body>

服务器跳转 直接跳转;不等页面加载完

 <body>
    
    <%
   session.setAttribute("info1", "session.helloword");
    
     %>
     info1:<%=session.getAttribute("info1") %>
   	<jsp:forward page="MyJsp.jsp"></jsp:forward>
     
    
  </body>

页面Jsp.jsp

 <body>
 info: <%=session.getAttribute("info1") %>
  
    This is my JSP page. <br>
  </body>

Application上设置参数

作用范围是服务器启动;关闭后在重启参数就无效了

本页面上显示Applicatuon设置参数

<body>
 info:<%
 		application.setAttribute("info","application.hello");
 		 %>
  	innfo:<%=application.getAttribute("info") %>
    
  </body>

跳转后得 MyJsp页面上显示参数。

客户端上跳转也显示

<%@ page language="java" import="java.util.*"
                            pageEncoding="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>My JSP 'index.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"> 	<!--
                            	<link rel="stylesheet" type="text/css" href="styles.css"> 	-->  
                            </head>
                                 <body>
                                
                                <%    info:
                                		application.setAttribute("info","application.hello");  	 %>  
                     	innfo:<%=application.getAttribute("info")
                              	 %>
                              	 <a href="MyJsp.jsp">客户端跳转</a>
                               	
                                 
                                   </body> </html>

只有重启就不好使了

pageContext指导范围

setAttribute(String name,Object value,int scope);
方法可以设置属性,并且指定属性得保存方法/
scope:
1.APPLICATION_SCOPE:代表application范围
2.SESSION_SCOPE:代表session范围
3.REQUEST_SCOPE:代表request范围
4.PAGE_SCOPE:dai表page范围

<body>
    
   <%
   	pageContext.setAttribute("info","world",pageContext.APPLICATION_SCOPE);
   	
    %>

需要记住对象类型得名称

requsest对象

javax.servlet.http HTTPServletRequest接口实例化来的对象
HTTPServletRequest是ServletRequest得子接口;
看request对象得方法 需要查看俩接口得方法。

我们不需要创建对象,web容器已经帮我们创建好了

request方法

HTTPServletRequest接口 只要一个
对应于HTTP协议; 目前只要一个网络应用层协议;通用得方法都封装在ServletRest中,
在诞生新的协议,直接写一个新的子接口实现ServerRequest接口 使得程序可扩。

父接口: 一个请求对象提供了数据 包含 name parameter values attribute 和 inputStream 添加了一个协议数据,在这个接口上使用了Http,及继承子类HTTPServeletRequest后。
1.getContentLength()
请求信息得长度
2. getContentType()获取MIME类型
3. getInputStream()获得输入流
4. getAttributeNames()获取所有得Ming
5. getLocalAddr() 可以获取发送请求人得IP地址
6. getParametor() 获得请求参数

子接口HTTPServletResult
getCookies() 获取cookies
getHeader(String name);获取请求头信息

  1. 用于解决乱码:
    resuest.setConfig="";
<body>
	<%
		
		String username=request.getParameter("username");
		String password=request.getParameter("password");
	 %>
  <%=username %>
    <%=password %>
  </body>

正常是由乱码
在获得字符前
request.setCharacterEncoding(“UTF-8”);
respond.setCharacterEncoding(“UTF-8”);
2. 接受数据

复选框

//获取复选框
		/*只能获取但一值*/
		//String hobby=request.getParameter("hobby");
		String[] hobby=request.getParameterValues("hobby");
	
	
	 %>
  <%=username %>
    <%=password %>
    <%
    if(hobby!=null&&hobby.length>0){
    	for(int i=0;i<hobby.length;i++){
    		%>
    			
    		<%=hobby[i] %>
    		<% 
    	
    	}
    
    }else{
    %>
    
    <%="此人无爱好" %>
    <% 
    	
    }
<input type="checkbox" name="hobby" value="foot"/>足球
<input type="checkbox" name="hobby" value="bathcatball"/>篮球
<input type="checkbox" name="hobby" value="game"/>游戏

1.0版本使用得迭代器

获得所有得请求参数:request.getParameterNames();

<%@ page language="java" import="java.util.*" pageEncoding="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>My JSP 'receive.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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <%
  	request.setCharacterEncoding("UTF-8");
  	
  	//获取所有得请求参数得名称  返回值是枚举器
  	// 1.2版本之前 用Vector比较多  集合最开始最正确得遍历 是迭代器
  	// vector得迭代器就是Emulation  这是出现集合之前所用得迭代器
  	Enumeration<String> ens= request.getParameterNames();
  	//1.5之后才出现泛型
  	while(ens.hasMoreElements()){
  	
  		String name=ens.nextElement();
  			if(name.startsWith("args-")){
  				String[] values=request.getParameterValues(name);
  				if(values!=null&&values.length>0){
  					for(int i=0;i<values.length;i++){
  						
  						%>	
  							<%=values[i] %>
  				
  				
  						<% 
  					
  				}
  				}else{
  				
  					String value=request.getParameter(name);
  					%>
  					
  						<%=value %><br/>
  					<% 
  				
  				}
  				
  				
  				
  			}
  	}
  	
  	
  	
  	
  	
  	
  	
  	
   %>
  
    This is my JSP page. <br>
  </body>
</html>

获取头文件信息

必须在提交表单后,在提交得目标位置才有头文件
String str=request.getHeader(“Content-Type”);

<%@ page language="java" import="java.util.*" pageEncoding="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>My JSP 'index.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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
  <form action="MyJsp.jsp" method="post">
  	Username:<input type="text" name="username" /><br/>
  	Password:<input type="password" name="password"/><br/>
  	<input type="checkbox" name="hobby" value="foot"/>足球<br/>
  	<input type="checkbox" name="hobby" value="bathcatball"/>篮球<br/>
  	<input type="checkbox" name="hobby" value="game"/>游戏<br/>
  	<input type="hidden" name="userid" value="1"/>
  	个人简洁:<input type="text" name="info"/><br/>
  	
  	<input type="submit" value="提交"/>
  	
  </form>
     
    
  </body>
</html>

目标jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="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>My JSP 'MyJsp.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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
	<%
		request.setCharacterEncoding("UTF-8");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		//获取复选框
		/*只能获取但一值*/
		//String hobby=request.getParameter("hobby");
		String[] hobby=request.getParameterValues("hobby");
		
		String str=request.getHeader("Content-Type");
	
	
	 %>
  <%=username %>
    <%=password %>
    <%=str%>
   
  </body>
</html>

在这里插入图片描述
在这里插入图片描述
最后得就是头文件信息。

在这里插入图片描述

response

  1. Response对象的主要作用就是对客户端的请求进行回应,将web服务器处理后的结果发回给客户端。Response对象属于javax.servlet.http.HttpServletResponse接口的实例,HttpServletResponse接口的定义如下:public interface HttpServletResponse extends ServletResponse
    从接口定义可以发现,此接口是ServletResponse的子接口,而ServletResponse也只有HttpServletResponse一个子接口,这一点与request对象是完全一样的。

常用的方法:
1.public void addCookie(Cookie cookie):用来向客户端设置Cookie数据的
2.public void setHeader(String name,String value):设置响应信息中的响应头。
3.public void sendRedirect(String location):用来实现页面的跳转。
4.public void addCookie(Cookie cookie):向客户端设置Cookie

服务器跳转与客户端跳转的区别:

  1. 服务器跳转,跳转之后地址栏的信息并不会发生任何的改变,服务器跳转是发送一次请求,得到一次响应,而response.sendRedirect()属于客户端跳转,跳转之后地址栏会发生改变的,变为跳转之后的页面地址,客户端跳转是发送两次请求得到两次响应。
    而且在使用request属性范围时,只有服务器跳转才能够将request范围的属性保存到跳转页面;而如果客户端跳转,则无法进行request属性的传递。
    另外,如果使用的是服务器跳转,则执行到跳转语句时会立刻进行跳转;如果使用的是客户端跳转,则是整个页面执行完毕之后才执行跳转。
  2. 由于两种跳转存在这样的差异,所以在以后的代码开发中,有其是使用了JDBC的操作中,一定要在jsp:forward语句执行前关闭数据库连接操作,否则数据库连接将再也无法关闭。而如果数据连接始终没有关闭,当达到一定程度时会出现数据库连接已经达到最大的异常,此时就只能重新启动服务器了。
    而且,如果使用了jsp:forward,可以通过jsp:param方便的进行参数的传递;而如果使用了response.sendRedirect()传递参数,则只能通过地址重写的方式完成。
    从实际的开发来看,服务器端跳转要比客户端跳转常用
  3. Cookie是浏览器所提供的一种技术,这种技术让服务器端的程序能将一些只需保存在客户端,或者在客户端进行处理的数据,放在本地使用的计算机上,不需通过网络的传输,因为提高了网页处理的效率,而且也能减少服务器端的负载。但是由于Cookie是服务器端保存在客户端的信息,所以其安全性也是很差的。

接口继承ServerRespond接口

在这里插入图片描述
在这里插入图片描述

5秒后自动跳转

 <body>
 	<%!
 	int count=0;
 	 %>
 <%
 response.setHeader("refresh","5;URL=myjsp.jsp");
  %>

 
 
  </body>

JSP和HTML都可跳转,HTML跳转一般在只要HTML代码静态网页时选择HTML跳转
我门很少使用HTMl 时前台
我们Java工程师使用jsp时 都是用 respond或者jsp:forward得方式。

页面跳转

1.使用respnd跳转
response.sendRedirect(“index.jsp?username=‘张三’”);
不支持中文,支支持英文
response.sendRedirect("");

在这里插入图片描述
属于客户端跳转
在这里插入图片描述

使用response进行传值

<body>
 	<%!
 	int count=0;
 	 %>
 <%
  response.sendRedirect("myjsp.jsp?username=as");
  %>


 
 
  </body>
<body>
    <%
    String username=request.getParameter("username");
     %>
     <%=username %>
  </body>
  1. request跳转
    属于服务器跳转
    需要转型 因为getRequestDispatcher().forward()fanhui得是Object 需要强转成String类型才能获取在另一个jsp
 <%
 request.getRequestDispatcher("myjsp.jsp").forward(request,response);
 
  %>

地址栏不会发生改变。
在使用request属性范围时,只要服务器跳转才能保证参数得在多个页面跳转,且立刻跳转。客户端跳转无法进行request属性得传递

Cookie

addCookie()
第一次访问服务器,服务器会自动给你一个唯一的sessionId; session对象被赋予了一个序列值。作为kookie得形式写在浏览器中

服务器按照session ID区分每一个用户, 过失效后,服务器会分布新的id给用户。

登录于注销

在各个系统中都会有用户登录验证及注销得功能,此功能完全使用Session实现。
登陆成功后,设置一个session范围得属性,然后在其他页面中判断是否存在session范围得属性,如果存在,则表示登录过的合法用户。 不存在,跳回登录页提示用户重新登录。用户登录后可ui进行注销操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值