<!--
登录系统的主界面
-->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>学生管理系统-管理登录</title>
<style type="text/css">
h2, #login_button, div {
text-align: center;
}
table {
background-color: #c1e4e9;
padding: 8px;
border: 2px;
}
#login_button {
padding: 20px;
}
div {
text-color: red;
}
</style>
</head>
<body>
<form action="login" method="post">
<table align="center">
<tr>
<td colspan="2"><h2>学生管理系统</h2></td>
</tr>
<tr>
<td align="right">用户名:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td align="right">密码:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td id="login_button" colspan="2"><input type="submit"
name="submit" value="登录" /></td>
</tr>
</table>
</form>
<div>${massage }</div>
</body>
</html>
package fuz.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import fuz.object.User;
import fuz.service.UserService;
/**
* 登录(管理员)
*/
@WebServlet(name="/LoginServlet", value="/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.设置请求编码,目的是为了处理中文乱码
request.setCharacterEncoding("UTF-8");
//2.获取表单提交
String username = request.getParameter("username");
String password = request.getParameter("password");
HttpSession session = request.getSession();
User user = new User(username, password);
//3.处理登录业务,也就是调用业务层
UserService userService = new UserService();
boolean bool = userService.login(user);
//4.根据返回结果跳转页面
if(bool) {
//登录成功 就会来到学生管理系统的首页
session.setAttribute("user", user);
response.sendRedirect("main.jsp");
}else {
//登录失败 说明用户名和密码不正确 从而提示错误信息
request.setAttribute("massage", "用户名和密码不正确");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
}