<%@include file="jsp页面"%>
将需要重用的代码放在一个页面 再用上方法调用。
application对象代表WEB应用,整个Web应用共享一个application对象,因此,application对象类似于系统的“全局变量”,用于同一个Web应用内的所有用户之间的数据共享。
application对象常用方法
![](https://img-blog.csdnimg.cn/547bedd6a8874f0b98c0d43b4301bc5e.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5r2H5rSS77yIc2HvvInkuIDngrk=,size_20,color_FFFFFF,t_70,g_se,x_16)
登录页面
<form action="do_login.jsp">
用户名:<input type="text" name="username">
<p>
密码:<input type="text" name="password">
</p>
<p>
<input type="submit" value="登录">
</p>
</form>
跳转到do_login.jsp
<%
Integer count=(Integer) application.getAttribute("count");
if (count!=null){
count=count+1;
}else {
count=1;
}
application.setAttribute("count",count);
request.getRequestDispatcher("index.jsp").forward(request,response);
%>
在跳转到index.jsp页面:
<h1>您是第:<%=(Integer)application.getAttribute("count")%>位到访的客户</h1>
代码实现结果:
小结eiJSP内置对象:
--------------------------------------
JSP对象作用域:
Pgae作用域:
建立两个jsp:one/two
one.jsp
<%
String name="库里";
pageContext.setAttribute("name",name);
request.setAttribute("reqname",name);
session.setAttribute("sessname",name);
application.setAttribute("applicname",name);
System.out.println("第一个页面:"+pageContext.getAttribute("name")+"page");
System.out.println("第一个页面:"+request.getAttribute("reqname")+"request");
System.out.println("第一个页面:"+session.getAttribute("sessname")+"session");
System.out.println("第一个页面:"+application.getAttribute("applicname")+"session");
//转发
// request.getRequestDispatcher("two.jsp").forward(request,response);
//重定向性
response.sendRedirect("two.jsp");
%>
two.jsp
<%
System.out.println("第二个页面:"+pageContext.getAttribute("name")+"page作用域");
System.out.println("第二个页面:"+request.getAttribute("reqname")+"request作用域");
System.out.println("第二个页面:"+request.getAttribute("sessname")+"session作用域");
System.out.println("第二个页面:"+request.getAttribute("applicname")+"application作用域");
%>
如果选用转发数据结果为:
![](https://img-blog.csdnimg.cn/d218b027df184bedad1d2dc61c03a8ba.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5r2H5rSS77yIc2HvvInkuIDngrk=,size_14,color_FFFFFF,t_70,g_se,x_16)
如果使用重定向性发送数据结果为:
![](https://img-blog.csdnimg.cn/9766fe82f902497c9dd4b4a82aafbb59.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5r2H5rSS77yIc2HvvInkuIDngrk=,size_15,color_FFFFFF,t_70,g_se,x_16)
总结: