JSP简单登录系统

Login登陆界面

<body>
登陆
<%
session.invalidate();

%>
<form action="TestPW.jsp" method="post">

用户名<input type="text" name="username">
密码<input type="password" name="password">
<input type="submit" value="提交">

</form>
</body>

验证页面

<body>
<%
//验证登录信息
String un=request.getParameter("username");
String pw=request.getParameter("password");

if(un!=null&&pw!=null)
{
	if(un.equals("tom")&&pw.equals("123"))
	{
		//记录用户信息
		session.setAttribute("username", un);
		
		response.sendRedirect("Main.jsp");
	}
	else
	{
		out.print("用户名或密码错误");
	}
}
else
{
	out.println("请以正常的方式访问系统");
}

//否则提示错误

%>

</body>

  

Main主页面

<body>
<%
//检查session 取得session信息

Object obj=session.getAttribute("username");
if(obj!=null)
{
	
	out.print("欢迎"+obj.toString()+"登陆");
}
else
{
	out.print("登陆超时,请重新登录系统");
	
	response.setHeader("refresh", "3;URL=Login.jsp");
}

%>

主页面

<br>
<a href="Login.jsp">退出系统</a>

</body>

验证情况

1、当用户名输入错误时

2、直接输入主页面访问时

3、直接输入验证页面地址访问

4、成功登陆系统时

5、退出系统后再输入主页面网址

原文地址:https://www.cnblogs.com/mutougezi/p/5625854.html