项目概况
该用户登录系统的结构由login.jsp、dologin.jsp、login_success.jsp、login_failure.jsp等jsp页面组成,在界面中插入了一些HTML代码和CSS代码美化界面。实现逻辑是当登录成功则使用服务器转发到login_success.jsp页面,并提示登录成功的用户名。如果失败则请求重定向到login_failure.jsp页面。使用内置字符串验证登录而不是通过连接数据库验证用户名和密码。用户名:admin,密码:123。
页面设计
login.jsp页面
login.jsp是登录界面,由HTML+CSS代码组成。主体由一张图片和form登录表单构成,用div和style使登录表单居中。form表单提交到dologin.jsp页面,提交方式是post。
<body>
<img alt="" src="http://pic.58pic.com/58pic/11/68/41/78858PICYv3.jpg" width="2000" height="250">
<hr>
<div class="divForm">
<form action="dologin.jsp" method="post">
<p>
<h2>欢迎登录</h2>
</p>
<p>
<b>账 号:</b><input type="text" name="username"/>
</p>
<p>
<b>密 码:</b><input type="password" name="password"/>
</p>
<input type="submit" value="确定"/>
<input type="reset" value="清除"/>
</form>
</div>
</body>
<style type="text/css">
.divForm{
position: absolute;/*绝对定位*/
width: 300px;
height: 200px;
border: 1px solid blue;
text-align: center;/*(让div中的内容居中)*/
top: 70%;
left: 50%;
margin-top: -200px;
margin-left: -150px;
}
</style>
</html>
页面效果:
dologin.jsp页面
dologin页面主要的功能是对输入的用户名和密码进行验证,并进行逻辑判断。验证通过则转发到login_success.jsp页面,失败则请求重定向到login_failure.jsp页面。主要运用到request内置对象和session内置对象:
- request.getParameter方法获取用户名和密码
- request.setCharacterEncoding方法防止出现中文乱码
- request.getRequestDispatcher使用服务器内部转发
- response.sendRedirect请求重定向
- session.setAttribute获取用户名并保存
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String username = "";//创建字符串类型username并初始化
String password = "";
request.setCharacterEncoding("utf-8");//防止中文乱码
username = request.getParameter("username");//用request.getParameter方法获得输入的用户名
password = request.getParameter("password");
if("admin".equals(username)&&"123".equals(password))//对输入的用户名和密码进行验证
{
session.setAttribute("loginUser", username);//用session.setAttribute保存用户名
request.getRequestDispatcher("login_success.jsp").forward(request, response);//成功则使用服务器内部转发
}
else
{
response.sendRedirect("login_failure.jsp");//失败则使用请求重定向
}
%>
login_success.jsp页面
login_success页面的功能是当用户登录成功时提示欢迎信息,并显示用户名。用session内置对象的session.getAttribute获得用户名,并用<%=loginUser %>显示用户名。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login_success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String loginUser = "";
if(session.getAttribute("loginUser")!=null)//判断输入的用户名是否为空
{
loginUser = session.getAttribute("loginUser").toString();//获取用户名
}
%>
<h1>登录成功!</h1>
<hr>
<font color="red">
<%=loginUser %>
</font>,欢迎你!<br>
</body>
</html>
页面效果:
login_failure.jsp页面
login_failure页面功能是当验证不通过时提示登录失败信息,并提供跳转到登录界面的URL进行重新登录,用“< a href=“login.jsp”>返回登录< /a>”提供跳转URL。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login_success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>登陆失败!请检查用户名或者密码是否正确。</h1><br>
<hr>
<a href="login.jsp">返回登录</a>
</body>
</html>
页面效果: