星空网 > 软件开发 > Java

session 对象的简单实例

一个session对象的简单实例:

1、登录界面:使用简单的html表单提交界面。

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
    <form action="do_long.jsp" method="post">
    用户名:<input type="text" name="usename"/><br><br>
    密码:<input type="password" name="password"/><br><br>
       <input type="submit" value="提交"/>
       <input type="reset" value="重置"/>
    </form>
</body>
</html>

2、request对象获取用户名和密码,判断是否为空,使用session的setattribute储存用户名信息。跳转到欢迎界面。

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<% 
 String usename=request.getParameter("usename");
 String password=request.getParameter("password");
 if(usename!=null&&password!=null){
  session.setAttribute("usename", usename);
  response.setHeader("refresh", "2;URL=welcome.jsp");
 }
 else{
 %>

 用户名,密码不能为空。<a href="long.jsp">登录</a>
 <%}%>

3、欢迎界面

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
 <%if(session.getAttribute("usename")!=null){%>
 欢迎:<%=session.getAttribute("usename")%>  
 <a href="longout.jsp">注销</a><br>
 <%}else {%>
 请先登录
 <a href="long.jsp">登录</a><br>
 <%} %>
 <%if(session.isNew()){ %>
 欢迎新用户
 <%}else{ %>
 欢迎老用户
 <%} %>
</body>
</html>

4、退出界面

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%
 session.invalidate();
 response.setHeader("refresh", "2;URL=welcome.jsp");
%>

 




原标题:session 对象的简单实例

关键词:session

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流