<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>登录</title>
</head>
<body>
登录页面<br>
<%
//销毁session
session.invalidate();%>
<form action="testPW.jsp" method="post">
用户名:<input type="text" name="username"/>
密码:<input type="password" name="password"/>
<input type="submit" value="登录"/></form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//验证 用户登录信息是否正确
String un= request.getParameter("username");
String pw= request.getParameter("password");
if (un!=""&&pw!="")
{
//如果正确,就创建session,并跳转到main.jsp
if(un.equals("tom")&&pw.equals("123"))
{
session.setAttribute("username", un);
//跳转到系统主页面
response.sendRedirect("Main.jsp");
}
else
{
//否则提示登陆错误
out.print("用户名或密码错误");
}
}
else
{
out.print("请以正确的方式登录");
}
//%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<% //检查session,取得session信息
Object obj=session.getAttribute("username");
if(obj!=null)
{
out.print("欢迎登陆");
}
else
{
out.print("会话超时,请重新登录系统");
response.setHeader("refResh", "3;URL=Login.jsp");
}%>
主页面
<br>
<a href="Login.jsp">退出登录</a>
</body>
</html>
![](https://images2015.cnblogs.com/blog/917837/201606/917837-20160629084145577-1828191380.png)
![](https://images2015.cnblogs.com/blog/917837/201606/917837-20160629084150734-6726590.png)