1. …………………………….page对象
:只在一个页面中保存属性,跳转之后无效
2. …………………………..request对象
:只在一次请求中保存,服务器跳转后依然有效
3. …………………………..session对象
:再一次会话范围中,无论何种跳转都可以使用,但是新开浏览器无法使用
4. …………..application对象(设置不能太多,否则影响性能.)
:在整个服务器上保存,所有用户都可以使用。
以上四个内置对象支持的操作方法有:setAttribute,getAttribute,removeAttribute
5…………………………….解决中文乱码
6…………………………..页面定时刷新和定时跳转
7………………………….Cookie设置问题
8------------------------------用户注销与登录。。。。。。。。。。
9-----------------------------判断是新旧用户-------------------------
1.Page实例:
A:
注:引入java.util.Date包
<body>
<%
pageContext.setAttribute("name", "离心故哈");
pageContext.setAttribute("birthday",new Date());
%>
<%
Stringusername=(String)pageContext.getAttribute("name");
Dateuserbirthday=(Date)pageContext.getAttribute("birthday");
%>
<h2><%=username %></h2>
<h2><%=userbirthday %></h2>
</body>
B:
a.jsp:
<body>
<%
pageContext.setAttribute("name", "理性化");
pageContext.setAttribute("birthday",new Date());
%>
<jsp:forward page="MyJsp.jsp"></jsp:forward>
</body>
MyJsp.jsp:
<body>
<%
String username=(String)pageContext.getAttribute("name");
Dateuserbirthday=(Date)pageContext.getAttribute("birthday");
%>
<h2>姓名:<%=username %></h2><h2>birthday::<%=userbirthday %></h2>
</body>
结果显示:姓名与生日为null.因为出现了页面的跳转。
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
2.request属性范围:
代码将pageContext改为request.。只有在一次跳转时才有效。超连接是无效的。
3.session属性范围
a.jsp:
<body>
<%
session.setAttribute("name","张高凯");
session.setAttribute("birthday",new Date());
%>
<a href="b.jsp">session</a>
</body>
b.jsp:
<body>
<%
Stringusername=(String)session.getAttribute("name");
Dateuserbirthday=(Date)session.getAttribute("birthday");
%>
<h2>姓名:<%=username %></h2>
<h2>生日:<%=userbirthday %></h2>
</body>
c.jsp:获取session的id
<%
String id=session.getId();
%>
<%=id %>
<%=id.length() %>
4.application属性范围
a.jsp:
<body>
<%
application.setAttribute("name", "张高凯");
application.setAttribute("birthday", new Date());
%>
<a href="b.jsp">application</a>
</body>
b.jsp:
<body>
<%
Stringusername=(String)application.getAttribute("name");
Dateuserbirthday=(Date)application.getAttribute("birthday");
%>
<h2>姓名:<%=username %></h2>
<h2>生日:<%=userbirthday %></h2>
</body>
5. 解决中文乱码
a. jsp:
<body>
<form action="b.jsp"method="post">
请输入信息:<input type="text"name="info">
<input type="submit"value="提交">
</form>
</body>
b. jsp:
<body>
<%
request.setCharacterEncoding("gb2312");
String content=request.getParameter("info");
%>
<h2><%=content %></h2>
</body>
6. 页面定时刷新问题:
response.setHeader("refresh","2");
response.setHeader("refresh","2;URL=b.jsp");
7. Cookie设置问题
a. jsp:
<%
Cookiec1=new Cookie("lxh","LIXINGHUA");
Cookiec2=new Cookie("mldn","www.MLDNJAVA.cn");
response.addCookie(c1);
response.addCookie(c2);
%>
b. jsp:
<%
Cookie c[]=request.getCookies();//获取全部Cookies
for(int x=0;x<c.length;x++)
{
%>
<h3><%=c[x].getName() %><%=c[x].getValue() %></h3>
<%
}
%>
c. jsp:设置Cookie的周期
<%
Cookie c1=new Cookie("lxh","LiXingHua");
Cookie c2=new Cookie("mldn","www.mldn.cn");
c1.setMaxAge(60);
c2.setMaxAge(60);
response.addCookie(c1);response.addCookie(c2);
%>
8. 用户登录与注销操作:
Login.jsp:
<body>
<form action="login.jsp"method="post">
用户名:<input type="text"name="uname"><br>
密 码:<input type="password"name="upass"><br>
<input type="submit"value="登录">
<input type="reset"value="重置">
</form>
<%
String name=request.getParameter("uname");
String password=request.getParameter("upass");
if("zgk".equals(name)&&"zgk".equals(password)){
response.setHeader("refresh","2;URL=welcome.jsp");
session.setAttribute("userid",name);
%>
<h3>用户登录成功,两秒后跳转到欢迎页面!</h3>
<h3>如果没有跳转,请按<a href="welcome.jsp">这里</a></h3>
<%
}
%>
</body>
Welcome.jsp:
<body>
<%
if(session.getAttribute("userid")!=null){
%>
<h3>welcome <%=session.getAttribute("userid") %>to system!<a href="logout.jsp">注销</a></h3>
<%
}
else
{
%>
<h3>请先进行系统的<a href="login.jsp">登录</a></h3>
<%
}
%>
</body>
Logout.jsp:
<body>
<%
response.setHeader("refresh","2;URL=login.jsp");
session.invalidate();
%>
<h3>您已成功推出系统,了秒后调回首页!</h3>
<h3>如果美誉跳转,请点<a href="login.jsp">这里</a>!</h3>
</body>
9. 判断是否是新用户
if(session.isNew())
{
%>
<h2>您是新用户!</h2>
<%
}