9.12 综合练习
1.创建管理员表
代码语言:javascript复制CREATE TABLE Manager(
username VARCHAR(20) PRIMARY KEY,
password VARCHAR(20) NOT NULL
)charset=utf8;
2.登录页面
代码语言:javascript复制<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>管理员登录</title>
</head>
<body>
<form action="/WebProject_war_exploded/loginMgr" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password" /><br/>
<input type="submit" value="登录">
</form>
</body>
</html>
3.LoginMgrController
代码语言:javascript复制package com.qf.servletProject.servlet.controller;
import com.qf.servletProject.entity.Manager;
import com.qf.servletProject.service.ManagerService;
import com.qf.servletProject.service.impl.ManagerServiceImpl;
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 java.io.IOException;
@WebServlet(name = "LoginMgrController",value = "/loginMgr")
public class LoginMgrController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.处理乱码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
//2.收参
String username = request.getParameter("username");
String password = request.getParameter("password");
//3.调用业务方法
ManagerService managerService = new ManagerServiceImpl();
Manager mgr = managerService.login(username,password);
//4.处理结果,流程跳转
if(mgr!=null){
//登录成功
//将管理员信息存储在Session里
HttpSession session = request.getSession();
session.setAttribute("mgr",mgr);
//跳转 目标、方式
response.sendRedirect("/WebProject_war_exploded/showallcontroller");
}else{
//登录失败
response.sendRedirect("/WebProject_war_exploded/loginMgr.html");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
4.ShowAllAdminController
代码语言:javascript复制package com.qf.servletProject.servlet.controller;
import com.qf.servletProject.entity.Admin;
import com.qf.servletProject.entity.Manager;
import com.qf.servletProject.service.AdminService;
import com.qf.servletProject.service.impl.AdminServiceImpl;
import javax.servlet.RequestDispatcher;
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 java.io.IOException;
import java.util.List;
@WebServlet(value = "/showallcontroller")
public class ShowAllAdminController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//通过HttpSession完成权限控制
HttpSession session = req.getSession();
Manager mgr =(Manager)session.getAttribute("mgr");
if(mgr !=null){
//只负责调用业务逻辑功能
AdminService adminService = new AdminServiceImpl();
List<Admin> adminList = adminService.showAllAdmin();
//request作用域存储数据
req.setAttribute("admins",adminList);
//通过转发 跳转到显示结果servlet
req.getRequestDispatcher("/showalljsp").forward(req,resp);
}else{
resp.sendRedirect("/WebProject_war_exploded/loginMgr.html");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
9.13 Session实战保存验证码
1.创建验证码
- 导入ValidateCode.jar
- 创建生成验证码的Servlet
/**
* Servlet implementation class CreateCode
* 验证码的生成
*/
@WebServlet("/createcode")
public class CreateCode extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ValidateCode vc=new ValidateCode(200, 30, 4, 10);
String code=vc.getCode();
System.out.println(request.getRemoteAddr() ":生成:" code);
//使用Session存储生成的验证码
HttpSession session=request.getSession();
session.setAttribute("code",code);
//响应给客户端
vc.write(response.getOutputStream());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
2.登录页面
代码语言:javascript复制<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>管理员登录</title>
</head>
<body>
<form action="/WebProject_war_exploded/loginMgr" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password" /><br/>
验证码:<input type="text" name="inputVcode"/> <img src="/WebProject_war_exploded/createcode" /><br/>
<input type="submit" value="登录">
</form>
</body>
</html>
3.LoginMgrController
代码语言:javascript复制package com.qf.servletProject.servlet.controller;
import com.qf.servletProject.entity.Manager;
import com.qf.servletProject.service.ManagerService;
import com.qf.servletProject.service.impl.ManagerServiceImpl;
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 java.io.IOException;
@WebServlet(name = "LoginMgrController", value = "/loginMgr")
public class LoginMgrController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.处理乱码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
//2.收参
String username = request.getParameter("username");
String password = request.getParameter("password");
String inputVcode = request.getParameter("inputVcode");
String codes = (String) request.getSession().getAttribute("codes");
if (!inputVcode.isEmpty() && inputVcode.equalsIgnoreCase(codes)) {
//3.调用业务方法
ManagerService managerService = new ManagerServiceImpl();
Manager mgr = managerService.login(username, password);
//4.处理结果,流程跳转
if (mgr != null) {
//登录成功
//将管理员信息存储在Session里
HttpSession session = request.getSession();
session.setAttribute("mgr", mgr);
//跳转 目标、方式
response.sendRedirect("/WebProject_war_exploded/showallcontroller");
} else {
//登录失败
response.sendRedirect("/WebProject_war_exploded/loginMgr.html");
}
}else{
response.sendRedirect("/WebProject_war_exploded/loginMgr.html");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}