登录页面格式失效
之前的格式都是对分,但是最近在改增加书类的servlet,今天不知道怎么回事,登录页面的格式什么的全部失效了
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新建图书信息</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/add.css">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/dept/list.do"> 图书信息管理 </a>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Hello, ${existUser.username}!</h1>
<p>请小心地新增图书信息,要是建了一个错误的就不好了。。。</p>
</div>
<div class="page-header">
<h3>
<small>新建</small>
</h3>
</div>
<form class="form-horizontal" action="${pageContext.request.contextPath }/AddBookServlet" method="post">
<div class="form-group">
<label for="bookId" class="col-sm-2 control-label">图书编号 :</label>
<div class="col-sm-8">
<input name="bookId" class="form-control" id="bookId">
</div>
</div>
<div class="form-group">
<label for="bookName" class="col-sm-2 control-label">图书名称 :</label>
<div class="col-sm-8">
<input name="bookName" class="form-control" id="bookName">
</div>
</div>
<div class="form-group">
<label for="categoryId" class="col-sm-2 control-label">分类 :</label>
<select id="categoryId" name="categoryId"
class="col-sm-2 form-control"
style="width: auto; margin-left: 15px">
<option value="ca0001" selected="">计算机</option>
<option value="ca0002">文学</option>
<option value="ca0003">历史</option>
<!-- 下拉列表的内容要从分类中进行读取,value值是分类id -->
</select>
</div>
<div class="form-group">
<label for="bookPrice" class="col-sm-2 control-label">价格 :</label>
<div class="col-sm-8">
<input name="bookPrice" class="form-control" id="bookPrice">
</div>
</div>
<div class="form-group">
<label for="bookPic" class="col-sm-2 control-label">图书封面 :</label> <input
type="file" id="bookPic" name="bookPic" style="padding-left: 15px">
</div>
<div class="form-group">
<label for="remarks" class="col-sm-2 control-label">备注 :</label>
<div class="col-sm-8">
<input name="remarks" class="form-control" id="remarks">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">保存</button>
</div>
</div>
</form>
</div>
<footer class="text-center"> copy@imooc </footer>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新建图书分类</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/add.css">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href=""> 图书分类管理 </a>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Hello,${existUser.username}!</h1>
<p>请小心地新增图书分类,要是建了一个错误的就不好了。。。</p>
</div>
<div class="page-header">
<h3>
<small>新建</small>
</h3>
</div>
<form class="form-horizontal" action="${pageContext.request.contextPath}/AddCategoryServlet" method="post">
<div class="form-group">
<label for="categoryId" class="col-sm-2 control-label">分类ID
:</label>
<div class="col-sm-8">
<input name="categoryId" class="form-control" id="categoryId">
</div>
</div>
<div class="form-group">
<label for="categoryName" class="col-sm-2 control-label">分类名称
:</label>
<div class="col-sm-8">
<input name="categoryName" class="form-control" id="categoryName">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">保存</button>
</div>
</div>
</form>
</div>
<footer class="text-center"> copy@imooc </footer>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书后台管理</title>
<link rel="stylesheet" href="css/index.css">
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<header>
<div class="container">
<nav>
<a href="bookList.jsp">图书信息管理</a>
</nav>
<nav>
<a href="categoryList.jsp">分类管理</a>
</nav>
</div>
</header>
<section class="banner">
<div class="container">
<div>
<h1>图书管理系统</h1>
<p>图书信息管理</p>
</div>
</div>
</section>
<section class="main">
<div class="container">
<form class="form-horizontal" action="/searchBook" method="post">
<div class="form-group" style="float: right;">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">查询</button>
</div>
</div>
<div class="form-group" style="float: right; width: 300px;">
<div class="col-sm-8">
<input name="searchContent" class="form-control"
id="searchContent" placeholder="输入要查询的分类" style="width: 250px">
</div>
</div>
</form>
</div>
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>序号</th>
<th>图书编号</th>
<th>图书名称</th>
<th>分类</th>
<th>价格</th>
<th>图书封面</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="" var="">
<tr id="tr1">
<td>1</td>
<td>book0001</td>
<td>Java基础</td>
<td>计算机类</td>
<td>¥29</td>
<td><img src="img/g1.jpg"></td>
<td><a href="/updateBook?bookId=book0001">修改</a> <a
href="/deleteBook?bookId=book0001">删除</a></td>
<!--在循环显示数据时,此处的book0001可以用EL表达式进行替换-->
</tr>
</c:forEach>
</tbody>
</table>
</div>
</section>
<section class="page">
<div class="container">
<div id="fatie">
<a href="${pageContext.request.contextPath }/addBook.jsp"><button>新建</button></a>
</div>
</div>
</section>
<footer> copy@慕课网 </footer>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书后台管理</title>
<link rel="stylesheet" href="css/index.css">
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<header>
<div class="container">
<nav>
<a href="bookList.jsp">图书信息管理</a>
</nav>
<nav>
<a href="categoryList.jsp">分类管理</a>
</nav>
</div>
</header>
<section class="banner">
<div class="container">
<div>
<h1>图书管理系统</h1>
<p>图书分类管理</p>
</div>
</div>
</section>
<section class="main">
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>序号</th>
<th>分类编号</th>
<th>分类名称</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${applicationScope.categorys}" var="category"
varStatus="idx">
<!-- <tr>
<td>1</td>
<td>ca0001</td>
<td>计算机类</td>
<td><a href="/deleteCategory?categoryId=ca0001">删除</a></td>
<!--在循环显示数据时,此处的ca0001可以用EL表达式进行替换-->
</tr>-->
<tr>
<td>${idx.idx+1}</td>
<td>${category.categoryId}</td>
<td>${category.categoryName}</td>
<td><a href="/deleteCategory?categoryId=ca0001">删除</a></td>
<!--在循环显示数据时,此处的ca0001可以用EL表达式进行替换-->
</tr>
</c:forEach>
</tbody>
</table>
</div>
</section>
<section class="page">
<div class="container">
<div id="fatie">
<a href="${pageContext.request.contextPath }/addCategory.jsp"><button>新建</button></a>
</div>
</div>
</section>
<footer> copy@慕课网 </footer>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
<link rel="stylesheet" href="css/login.css">
<!-- 实现点击更换验证码图片 -->
<script type="text/javascript">
//注册页面为空时,不能提交
function validate_form() {
//获取用户名
var username = document.getElementById("name").value;
if (username == null || username == "") {
alert("用户名不能为空");
return false;
}
//获取密码
var password = document.getElementById("pwd").value;
if (password == null || password == "") {
alert("密码不能为空");
return false;
}
//获取验证码
var verifyCode = document.getElementById("code").value;
if (verifyCode == null || verifyCode == "") {
alert("验证码不能为空");
return false;
}
return true;
}
//实现点击验证码更换图片
function changeImage() {
var verifyCodeImage = document.getElementById("verifyCodeImage");
verifyCodeImage.src = "${pageContext.request.contextPath}/KaptchaServlet?time="
+ new Date().getTime();
}
</script>
</head>
<body>
<div class="login">
<div class="header">
<h1>
<a href="/login.do">登录</a>
<p>${ msg }</p>
</h1>
<button></button>
</div>
<form action="${pageContext.request.contextPath }/LoginServlet"
onsubmit="return validate_form()" method="post">
<div class="name">
<input type="text" id="name" name="username">
<p></p>
</div>
<div class="pwd">
<input type="password" id="pwd" name="password">
<p></p>
</div>
<div class="code">
<input type="text" id="code" name="verifyCode" style="width: 150px">
<img id="verifyCodeImage"
onclick="changeImage()"
src="${pageContext.request.contextPath }/KaptchaServlet"
style="width: 150px; height: 42px; vertical-align: middle;">
<p></p>
</div>
<div class="btn-red">
<input type="submit" value="登录" id="login-btn">
</div>
</form>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改图书信息</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/add.css">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/dept/list.do"> 图书信息管理 </a>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Hello, XXX!</h1>
<p>请小心的修改图书信息。。。</p>
</div>
<div class="page-header">
<h3>
<small>修改</small>
</h3>
</div>
<form class="form-horizontal" action="/dept/add.do" method="post">
<div class="form-group">
<label for="bookId" class="col-sm-2 control-label">图书编号 :</label>
<div class="col-sm-8">
<input name="bookId" class="form-control" id="bookId"
readonly="readonly">
</div>
</div>
<div class="form-group">
<label for="bookName" class="col-sm-2 control-label">图书名称 :</label>
<div class="col-sm-8">
<input name="bookName" class="form-control" id="bookName">
</div>
</div>
<div class="form-group">
<label for="categoryId" class="col-sm-2 control-label">分类 :</label>
<select id="categoryId" name="categoryId"
class="col-sm-2 form-control"
style="width: auto; margin-left: 15px">
<option value="ca0001" selected="">计算机</option>
<option value="ca0002">文学</option>
<option value="ca0003">历史</option>
<!-- 下拉列表的内容要从分类中进行读取,value值是分类id -->
</select>
</div>
<div class="form-group">
<label for="bookPrice" class="col-sm-2 control-label">价格 :</label>
<div class="col-sm-8">
<input name="bookPrice" class="form-control" id="bookPrice">
</div>
</div>
<div class="form-group">
<label for="bookPic" class="col-sm-2 control-label">图书封面 :</label> <input
type="file" id="bookPic" name="bookPic" style="padding-left: 15px">
</div>
<div class="form-group">
<label for="remarks" class="col-sm-2 control-label">备注 :</label>
<div class="col-sm-8">
<input name="remarks" class="form-control" id="remarks">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">修改</button>
</div>
</div>
</form>
</div>
<footer class="text-center"> copy@imooc </footer>
</body>
</html>
package com.imooc.domain;
public class Book {
// 图书ID
private String bookId;
// 图书名字
private String bookName;
// 图书分类
private String bookCategory;
// 图书价格
private double bookPrice;
// 图书封面上传路径
private String bookPath;
// 图书备注
private String bookRemarks;
// 无参构造方法
public Book() {
super();
}
// 带参构造方法
public Book(String bookId, String bookName, String bookCategory, double bookPrice, String bookPath,
String bookRemarks) {
super();
this.bookId = bookId;
this.bookName = bookName;
this.bookCategory = bookCategory;
this.bookPrice = bookPrice;
this.bookPath = bookPath;
this.bookRemarks = bookRemarks;
}
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookCategory() {
return bookCategory;
}
public void setBookCategory(String bookCategory) {
this.bookCategory = bookCategory;
}
public double getBookPrice() {
return bookPrice;
}
public void setBookPrice(double bookPrice) {
this.bookPrice = bookPrice;
}
public String getBookPath() {
return bookPath;
}
public void setBookPath(String bookPath) {
this.bookPath = bookPath;
}
public String getBookRemarks() {
return bookRemarks;
}
public void setBookRemarks(String bookRemarks) {
this.bookRemarks = bookRemarks;
}
}
package com.imooc.domain;
public class Category {
//图书id号
private String categoryId;
//图书分类名称
private String categoryName;
//无参构造方法
public Category() {
super();
}
//带参数构造方法
public Category(String categoryId, String categoryName) {
super();
this.categoryId = categoryId;
this.categoryName = categoryName;
}
public String getCategoryId() {
return categoryId;
}
public void setCategoryId(String categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
@Override
public String toString() {
return "Category [categoryId=" + categoryId + ", categoryName=" + categoryName + "]";
}
}
package com.imooc.domain;
public class User {
// 用户名
private String username;
// 密码
private String password;
//无参构造方法
public User() {
super();
}
//带参构造方法
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
}
package com.imooc.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.imooc.domain.User;
public class AuthFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
User user = (User) req.getSession().getAttribute("existUser");
String reqURL = req.getRequestURI().toString();
// 设置不拦截的URL
String[] noURL = { "login", "login.jsp", ".js", ".css", ".png", ".jpg","LoginServlet","KaptchaServlet"};
for (String url : noURL)
if (reqURL.endsWith(url)) {
chain.doFilter(request, response);
return;
}
// 判断用户是否登录
if (user != null) {
chain.doFilter(request, response);
} else {
res.sendRedirect(req.getContextPath() + "/login.jsp");
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
package com.imooc.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.net.httpserver.Filter.Chain;
public class EncodingFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
req.setCharacterEncoding("UTF-8");
HttpServletResponse res = (HttpServletResponse)response;
res.setContentType("text/html;charset=UTF-8");
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
package com.imooc.service;
import java.util.List;
import com.imooc.domain.Book;
public interface BookService {
public static final List<Book> books = null;
public void addBook(Book book) ;
public void updateBook(Book book) ;
public void deleteBook(String bookId) ;
public Book getBooksById(String bookID) ;
public List<Book> getBooksByCatgoryName(String catgoryName) ;
}
package com.imooc.service;
import java.util.ArrayList;
import java.util.List;
import com.imooc.domain.Category;
public interface CategoryService {
public static final List<Category> categoryDb =new ArrayList<Category>();
public void addCatgory(String categoryId, String catgoryName);
public void deleteCatgory(String categoryId);
}
package com.imooc.service;
import java.util.List;
import com.imooc.domain.User;
public interface UserService {
//将登录页面提交的用户信息(user)与userList中的进行对比
public User login(List<User> userList,User user);
}
package com.imooc.service.impl;
import java.util.List;
import com.imooc.domain.Book;
import com.imooc.service.BookService;
public class BookServiceImpl implements BookService {
@Override
public void addBook(Book book) {
// TODO Auto-generated method stub
}
@Override
public void updateBook(Book book) {
// TODO Auto-generated method stub
}
@Override
public void deleteBook(String bookId) {
// TODO Auto-generated method stub
}
@Override
public Book getBooksById(String bookID) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Book> getBooksByCatgoryName(String catgoryName) {
// TODO Auto-generated method stub
return null;
}
}
package com.imooc.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.imooc.domain.Category;
import com.imooc.service.CategoryService;
public class CategoryServiceImpl implements CategoryService {
private static final List<Category> categoryDb = new ArrayList<Category>();
@Override
public void addCatgory(String categoryId, String categoryName) {
categoryDb.add(new Category(categoryId, categoryName));
System.out.println("测试输出图书类集合的信息");
System.out.println(categoryDb);
}
@Override
public void deleteCatgory(String categoryId) {
}
public static List<Category> getCategorydb() {
return categoryDb;
}
}
package com.imooc.service.impl;
import java.util.List;
import com.imooc.domain.User;
import com.imooc.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public User login(List<User> userList, User user) {
for (User existUser : userList) {
if (existUser.getUsername().equals(user.getUsername())
&& existUser.getPassword().equals(user.getPassword())) {
return existUser;
}
}
return null;
}
}
package com.imooc.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
@WebServlet("/AddBookServlet")
public class AddBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 接收数据
// 创建Map集合保存数据,第一个为key为bookId,value为其他属性
Map<String, String[]> map = new HashMap<String, String[]>();
// 1、创建磁盘文件项工厂
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
// 2、创建核心解析类
ServletFileUpload fileupload = new ServletFileUpload(diskFileItemFactory);
// 3、解析请求对象,将请求分成几个部分(FileItem)
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package com.imooc.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletContext;
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 com.imooc.domain.Category;
import com.imooc.service.CategoryService;
import com.imooc.service.impl.CategoryServiceImpl;
@WebServlet("/AddCategoryServlet")
public class AddCategoryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@SuppressWarnings("static-access")
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// // 接收页面提交的图书类的ID-bookId,图书类的名字-categoryName
// String categoryId = request.getParameter("categoryId");
// String categoryName = request.getParameter("categoryName");
// // 处理数据
// CategoryService category = new CategoryServiceImpl();
// category.addCatgory(categoryId, categoryName);
// request.setAttribute("categorys", category.categoryDb);
// System.out.println("保存书类的数据");
// System.out.println(category.categoryDb);
// // 显示数据处理结果,跳转至categoryList.jsp
// response.sendRedirect(request.getContextPath() + "/categoryList.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// // 接收页面提交的图书类的ID-bookId,图书类的名字-categoryName
// String categoryId = request.getParameter("categoryId");
// String categoryName = request.getParameter("categoryName");
// // 处理数据
// CategoryService category = new CategoryServiceImpl();
// category.addCatgory(categoryId, categoryName);
// System.out.println("servlet中的图书类集合");
// List categorys=category.categoryDb;
// System.out.println(categorys);
//
// ServletContext context=request.getServletContext();
//// List categorys=(List)context.
//// List categorys=(List)context.getAttribute(category.categoryDb);
//
//
//
// request.setAttribute("categorys", categorys);
//
//
// request.getRequestDispatcher("/categoryList.jsp").forward(request, response);
doGet(request, response);
}
}
package com.imooc.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 com.imooc.service.CategoryService;
import com.imooc.service.impl.CategoryServiceImpl;
@WebServlet("/deleteCategory")
public class deleteCategory extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 接收页面提交的图书类的ID-bookId
String categoryId = request.getParameter("categoryId");
// 处理数据
CategoryService categoryservice = new CategoryServiceImpl();
categoryservice.deleteCatgory(categoryId);
// 显示数据处理结果,跳转至categoryList.jsp
response.sendRedirect(request.getContextPath() + "/categoryList.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package com.imooc.servlet;
import java.io.IOException;
import java.util.List;
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 com.imooc.domain.User;
import com.imooc.service.UserService;
import com.imooc.service.impl.UserServiceImpl;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 完成校验码的校验
// 获得session中保存的验证码的信息
String code1 = (String) request.getSession()
.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
// 接收前台表单提交的验证码的信息
String code2 = request.getParameter("verifyCode");
if (code2 == null || !code2.equalsIgnoreCase(code1)) {
request.setAttribute("msg", "验证码输入不正确!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
return;
}
// 接收页面提交数据:
String username = request.getParameter("username");
String password = request.getParameter("password");
// 封装数据:
User user = new User();
user.setUsername(username);
user.setPassword(password);
System.out.println("页面提交的用户信息" + user);
// 处理数据完成登录
UserService userService = new UserServiceImpl();
// 获得用户列表的集合:
List<User> userList = (List<User>) getServletContext().getAttribute("userList");
System.out.println("LoginServlet获得的用户集合" + userList);
User existUser = userService.login(userList, user);
// 显示结果:
if (existUser == null) {
// 登录失败
request.setAttribute("msg", "用户名或密码错误!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
} else {
// 登录成功
// 将用户信息保存:
request.getSession().setAttribute("existUser", existUser);
response.sendRedirect(request.getContextPath() + "/categoryList.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
前段时间都和项目的效果一样啊,不知道是不是增加书类的改晕了,改到哪里了,实在找不出问题了,老师
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星