用户登录页面一般会包含一个表单,用来接收用户输入的登录信息,并提交给用户登录处理页面。
1、新建用户登录界面,Login.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<!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=ISO-8859-1">
<title>用户登录</title>
</head>
<body>
<center>
<h2>用户登录</h2>
<form action="LoginConf" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="userpassword"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登录">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
2、创建用户登录成功页面,Login_success.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<!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=ISO-8859-1">
<title>登录成功</title>
</head>
<body>
<center>
<%
if(session.getAttribute("login")!=null && session.getAttribute("login").equals("true")){
%>
<h2>登录成功</h2>
<%
}else{
%>
<jsp:forward page="Login.jsp"></jsp:forward>
<%
}
%>
</center>
</body>
</html>
3、创建用户登录失败页面,Login_failure.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<!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=ISO-8859-1">
<title>登录失败</title>
</head>
<body>
<center>
<h2>登录失败</h2>
</center>
</body>
</html>
4、新建业务逻辑组件,用来负责业务逻辑的处理,LoginCheck.java
package web_project;
public class LoginCheck {
//判断是否为合法用户
public boolean isLogin(String username,String userpassword){
if("zch".equals(username)&&"1234".equals(userpassword)){
System.out.print("access"+username+userpassword);
return true;
}else{
System.out.print("denied:"+username+userpassword);
return false;
}
}
}
5、创建Servlet控制器,LoginConf.java
负责接收用户参数,并调用业务逻辑组件进行判断,最后还负责页面的跳转。Servlet控制器是整合MVC设计模式的核心。
package web_project;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class LoginConf
*/
public class LoginConf extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 接收用户参数
String username = request.getParameter("username");
String userpassword = request.getParameter("userpassword");
//调试查看参数是否传进来
System.out.print(username+userpassword);
//获得session对象
HttpSession session = request.getSession();
//new一个LoginCheck对象
LoginCheck lc= new LoginCheck();
//调用业务逻辑组件的判断功能来判断
if(lc.isLogin(username,userpassword)){
//如果为合法用户,在request范围中添加属性login,其属性值为true,并跳转到登录成功页
session.setAttribute("login", "true");
request.getRequestDispatcher("Login_success.jsp").forward(request, response);
}else{
//如果非法用户跳转到登录失败页
request.getRequestDispatcher("Login_failure.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
6、配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>LoginConf</servlet-name>
<servlet-class>web_project.LoginConf</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginConf</servlet-name>
<url-pattern>/LoginConf</url-pattern>
</servlet-mapping>
</web-app>