jsp webcontent中网页共有的Session 练习题
作者:互联网
目录
题目:
创建三个页面,
第一个页面输入姓名、密码;
第二个页面判断姓名、密码是否为空,为空则返回第一个页面,不为进入第三个界面;
第三个页面实现姓名和提交时间的输出,设置直接运行第三个界面返回第一个界面,设置5秒后刷新自动返回第一个页面
第一个页面:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String noname = (String)session.getAttribute("NONAME");
String nopassword = (String)session.getAttribute("NOPASSWORD");
if(noname == "NONAME"){
%>
<p>姓名为空,请重新输入
<%
}
else if(nopassword == "NOPASSWORD"){
%>
<p>密码为空,请重新输入
<%
}
//销毁
session.invalidate();
%>
<p>输入您的姓名、密码
<form action="judge_name.jsp" method=post name=form>
用户 : <input type = "text" name = "name"><br>
密码 : <input type = "password" name = "password"><br><br>
<input type="submit" value="提交" name ="submit">
</form>
</body>
</html>
第二个页面:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
//获取submit.jsp传过来的数据
String userName = request.getParameter("name");
String password = request.getParameter("password");
//System.out.println(userName+":name");
//System.out.println(password+":password");
//判断输入过来的数据不为空
if(userName.length()>0&&password.length()>0){
session.setAttribute("status", "Login");
session.setAttribute("name", userName);
session.setAttribute("password", password);
response.sendRedirect("output_name.jsp");
}
else{
if(userName.length()<=0){
session.setAttribute("NONAME", "NONAME");
//System.out.println("empty:xm");
response.sendRedirect("submit_name.jsp");
}
else if(password.length()<=0){
session.setAttribute("NOPASSWORD", "NOPASSWORD");
//System.out.println("empty:mm");
response.sendRedirect("submit_name.jsp");
}
}
%>
</body>
</html>
第三个页面:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.util.*;"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%! //处理字符串的方法
public String getString(String s)
{
if(s==null)
{
s="";
}
try{
byte b[]=s.getBytes("utf-8");
s=new String(b);
}
catch(Exception e)
{
}
return s;
}
%>
<%
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String name = (String)session.getAttribute("name");
String status = (String)session.getAttribute("status");
if(status=="Login"){
%>
<p>------登录成功 ------
<p>姓名:<%=name %>
<p>登录时间:<%=new Date() %>
<%
}
else{
response.sendRedirect("submit_name.jsp");
}
session.setMaxInactiveInterval(5);
%>
</body>
</html>
标签:练习题,session,webcontent,title,第三个,密码,Session,姓名,页面 来源: https://blog.csdn.net/weixin_54348877/article/details/120874264