---
title: "jsp登录简单实例"
author: "accpxudajian"
date: "2013-01-08T10:49:35Z"
tags: []
categories: ["Web前端"]
slug: "jsp"
draft: false
lastmod: "2013-01-08T10:49:35Z"
id: 1766009
url: "https://angrycoder.iteye.com/blog/1766009"
---

```
用户通过密码aaa进入用户或管理页面，拒绝绕过登录页面进入用户或管理页面。
一、session_login.html
<html> <body>
<form method=post action="check_login.jsp">
<table>
<tr><td>name:</td><td>
  <input type=text name=name> </td></tr>
<tr><td>password:</td><td> 
  <input type=text name=password> </td></tr>
<tr colspan=2><td>
登录类型：
<input name=type type=radio value=manager checked>
管理员
<input type=radio name=type value=user>
普通用户
</td></tr>
<tr colspan=2> <td> <input type=submit value=login> </td> </tr>
</table>
</body> </html>
二、check_login.jsp

<%@ page contentType="text/html; charset=gb2312" %>

<%
//获取session_login.html提交来的信息
String name=request.getParameter("name");
String password=request.getParameter("password");
String type=request.getParameter("type");
//检查用户登录是否成功,这里假设用户名密码为aaa就表示登录成功，

if(password.equals("aaa")) {
  ///验证通过后，将用户信息写入session对象，
  session.setAttribute("name",name);
  session.setAttribute("password",password);
  session.setAttribute("type",type);
  ///根据用户选择的权限类型跳转页面，
if(type.equals("manager"))
   response.sendRedirect("manager.jsp");
else if(type.equals("user"))
   response.sendRedirect("user.jsp");
else {
    out.print("<script language='JavaScript' type='text/JavaScript'>alert('异常！请重新登录！');</script>");
    response.sendRedirect("session_login.html");
}
}
else
//登录失败，回到session_login.html页面。
{

out.print("<script>alert('请正确填写信息！');window.location.href='session_login.html'</script>");

//out.print("<script language='JavaScript' type='text/JavaScript'>alert('请正确填写信息！');</script>");
//response.sendRedirect("session_login.html");
}
%>

三、manager.jsp

<%@ page contentType="text/html; charset=gb2312" %>
<%
//由于password是跳转的依据，因此借助session中是否有password信息来判断用户是否有登录，

if(session.getAttribute("password")==null)
{out.print("<script>alert('请管理员先登录！');window.location.href='session_login.html'</script>");}
//由于管理员与用户登录后session中都会有信息且相同，会有以user权限登录后向该管理页面跳转的可能，所以要进行权限判断，

if(session.getAttribute("type")==null||!session.getAttribute("type").equals("manager"))
{out.print("<script>alert('你不是管理员，请重新登录！');window.location.href='session_login.html'</script>");}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>管理员登录</title>

</head>
<body>

欢迎管理员登录！
</body>
</html>

四、user.jsp

<%@ page contentType="text/html; charset=gb2312" %>
<%
//由于password是跳转的依据，因此借助session中是否有password信息来判断用户是否有登录，

if(session.getValue("password")==null)
out.print("<script>alert('请用户先登录！');window.location.href='session_login.html'</script>");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>用户登录</title>

</head>
<body>

欢迎用户登录！
</body>
</html>
```

 

 

摘自：[http://www.java3z.com/cwbwebhome/article/article5/5538.html?id=751](http://www.java3z.com/cwbwebhome/article/article5/5538.html?id=751)

 

 

愤怒的coder