l老师,出现个500的错误

l老师,出现个500的错误

Filter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
@WebFilter("/AuthFilter")
public class AuthFilter implements Filter {
 
     
    public AuthFilter() {
         
    }
 
    public void destroy() {
         
    }
 
     
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest hrequest=(HttpServletRequest) request;
        HttpServletResponse hresponse=(HttpServletResponse) response;
        String loginUser=(String) hrequest.getSession().getAttribute("loginUser");
        if(loginUser==null) {
            hresponse.sendRedirect(hrequest.getContextPath()+"/login.jsp?flag=1");
            return;
        }else {
            chain.doFilter(request, response);
            return;
        }
         
    }
 
     
    public void init(FilterConfig fConfig) throws ServletException {
         
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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.annotation.WebFilter;
 
 
@WebFilter("/EncodeFilter")
public class EncodeFilter implements Filter {
 
    private FilterConfig config;
     
    public EncodeFilter() {
        
    }
 
     
    public void destroy() {
         
    }
 
     
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding(config.getInitParameter("charset"));
        chain.doFilter(request, response);
    }
 
    public void init(FilterConfig config) throws ServletException {
           this.config=config;  
    }
 
 
    public FilterConfig getConfig() {
        return config;
    }
 
 
    public void setConfig(FilterConfig config) {
        this.config = config;
    }
     
}

mainclass

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.imooc.mainclass;
 
public class Book {
    private String bookId;
    private String bookName;
    private String categoryBook;
    private Float price;
    private String path;
    private String description;
 
    public Book() {
        super();
    }
 
    public Book(String bookId, String bookName, String categoryBook, Float price, String path, String description) {
        super();
        this.bookId = bookId;
        this.bookName = bookName;
        this.categoryBook = categoryBook;
        this.price = price;
        this.path = path;
        this.description = description;
    }
 
    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 getCategoryBook() {
        return categoryBook;
    }
 
    public void setCategoryBook(String categoryBook) {
        this.categoryBook = categoryBook;
    }
 
    public Float getPrice() {
        return price;
    }
 
    public void setPrice(Float price) {
        this.price = price;
    }
 
    public String getPath() {
        return path;
    }
 
    public void setPath(String path) {
        this.path = path;
    }
 
    public String getDescription() {
        return description;
    }
 
    public void setDescription(String description) {
        this.description = description;
    }
 
    @Override
    public String toString() {
        return "Book [bookId=" + bookId + ", bookName=" + bookName + ", categoryBook=" + categoryBook + ", price="
                + price + ", path=" + path + ", description=" + description + "]";
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.imooc.mainclass;
 
public class Category {
    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 + "]";
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.imooc.mainclass;
 
public class User {
 
    private String username;
    private String password;
 
    public User() {
 
    }
 
    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 + "]";
    }
 
}

service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.imooc.service;
 
import com.imooc.mainclass.Category;
import com.imooc.service.impl.CategoryServiceImpl;
 
public interface CategoryService {
 
    public void addCategory(String categoryId, String categoryName);
 
    public Category check(Category category);
 
     
    public void deleteCategory(String categoryId);
     
     
}
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.imooc.service;
 
import java.util.List;
 
import com.imooc.mainclass.User;
 
public interface UserService {
 
    User login(List<User> userList, User user);
 
     
 
}

serviceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.imooc.service.impl;
 
import java.util.ArrayList;
import java.util.List;
 
import com.imooc.mainclass.Category;
import com.imooc.service.CategoryService;
 
public class CategoryServiceImpl implements CategoryService {
    private static final List<Category> categoryDb = new ArrayList<Category>();
 
    public void addCategory(String categoryId, String categoryName) {
        categoryDb.add(new Category(categoryId, categoryName));
    }
 
    public void deleteCategory(String categoryId) {
        Category cgy = null;
        for (Category category : categoryDb) {
            if (category.getCategoryId().equals(categoryId)) {
                cgy = category;
            }
        }
        categoryDb.remove(cgy);
    }
 
    @Override
    public Category check(Category category) {
        for (Category existCategory : categoryDb) {
            if (existCategory.getCategoryId().equals(category.getCategoryId())
                    || existCategory.getCategoryName().equals(category.getCategoryName())) {
                   return existCategory;  
            }
 
        }
        return null;
 
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.imooc.service.impl;
 
import java.util.List;
 
import com.imooc.mainclass.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;
    }
 
}

utils

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.imooc.utils;
 
import java.util.UUID;
 
public class UploadUtils {
    public static String getUuidFileName(String fileName) {
        int idx=fileName.lastIndexOf(".");
        String exName=fileName.substring(idx);
        String UUidFileName=UUID.randomUUID().toString().replace("-", "")+exName;
        return UUidFileName;
    }
 
}

Listener

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.imooc.web.listener;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
 
import com.imooc.mainclass.User;
 
@WebListener
public class InitServletContextListener implements ServletContextListener {
 
    public InitServletContextListener() {
         
    }
 
     
    public void contextDestroyed(ServletContextEvent sce)  { 
          
    }
 
     
    public void contextInitialized(ServletContextEvent sce)  { 
         //创建一个用于保存用户信息的List集合
        List<User> userList=new ArrayList<User>();
        userList.add(new User("admin","123456"));
        userList.add(new User("user","654321"));
        userList.add(new User("aaa","123123"));
        userList.add(new User("bbb","456456"));
        userList.add(new User("ccc","123456"));
        sce.getServletContext().setAttribute("userList", userList);
    }
     
}

servlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.imooc.web.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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
 
import com.imooc.mainclass.Book;
import com.imooc.utils.UploadUtils;
 
@WebServlet("/addBookServlet")
public class addBookServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
        
  
    public addBookServlet() {
        super();
        
    }
 
     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        Map<String,String> map=new HashMap<String,String>();
        //创建磁盘文件项工厂
        DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
        //创建核心解析类
        ServletFileUpload fileUpload=new ServletFileUpload(diskFileItemFactory);
        //解析请求对象,将请求分成几个部分FileItem
        try {
            List<FileItem> list=fileUpload.parseRequest(request);
            //遍历集合,获得各个部分的对象
            for(FileItem fileItem:list) {
                if(fileItem.isFormField()) {
                    String name=fileItem.getName();
                    String value=fileItem.getString("UTF-8");
                    map.put(name, value);
                }else {
                    String fileName=fileItem.getName();
                    String uuidFileName=UploadUtils.getUuidFileName(fileName);
                    InputStream is=fileItem.getInputStream();
                    String path=getServletContext().getRealPath("/upload");
                    String url= path+"\\"+uuidFileName;
                    map.put("path", request.getContextPath()+"/upload"+uuidFileName);
                    OutputStream os=new FileOutputStream(url);
                    int len=0;
                    byte[] b=new byte[1024];
                    while((len=is.read(b))!=-1) {
                        os.write(b, 0, len);
                    }
                    is.close();
                    os.close();
                }
            }
        } catch (FileUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         
        Book book=new Book();
        book.setBookId(map.get("bookId"));
        book.setBookName(map.get("bookName"));
        book.setCategoryBook(map.get("categoryId"));
        book.setPrice(Float.parseFloat(map.get("bookPrice")));
        book.setDescription(map.get("remarks"));
        book.setPath(map.get("path"));
        response.sendRedirect(request.getContextPath()+"/bookList.jsp");
     
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.imooc.web.servlet;
 
import java.io.IOException;
import java.util.ArrayList;
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.mainclass.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;
     
    public addCategoryServlet() {
        super();
         
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取数据
        String categoryId=request.getParameter("categoryId");
        String categoryName=request.getParameter("categoryName");
        Category category=new Category(categoryId,categoryName);
        CategoryService categoryService=new CategoryServiceImpl();
        List<Category> categoryList=(List<Category>)request.getSession().getAttribute("categoryList");
        if(categoryList==null) {
            categoryList=new ArrayList();
        }
        //将输入的数据与后台比对
        Category existCategory=categoryService.check(category);
        if(existCategory==null) {
            //如果后台数据中为空,则添加数据,同时将分类加入到分类列表中
            categoryService.addCategory(categoryId, categoryName);
            categoryList.add(category);
            //将列表的集合存入session中
            request.getSession().setAttribute("categoryList", categoryList);
            response.sendRedirect(request.getContextPath()+"/categoryList.jsp");
        }else {
            response.sendRedirect(request.getContextPath()+"/addCategory.jsp?flag=1");
        }
         
                 
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.imooc.web.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.mainclass.Category;
import com.imooc.service.CategoryService;
import com.imooc.service.impl.CategoryServiceImpl;
 
 
@WebServlet("/deleteCategoryServlet")
public class deleteCategoryServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
     
    public deleteCategoryServlet() {
        super();
       
    }
     
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String categoryId=request.getParameter("categoryId");
        CategoryService categoryService=new CategoryServiceImpl();
        categoryService.deleteCategory(categoryId);
        List<Category> categoryList=(List<Category>) request.getSession().getAttribute("categoryList");
        Category cate=null;
        for(Category category:categoryList) {
            if(category.getCategoryId().equals(categoryId)) {
                cate=category;
            }
        }
        categoryList.remove(cate);
        request.getSession().setAttribute("categoryList", categoryList);
        response.sendRedirect(request.getContextPath()+"/categoryList.jsp");
         
    }
 
     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         
        doGet(request, response);
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.imooc.web.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.mainclass.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;
        
    
    public LoginServlet() {
        super();
        
    }
 
     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String code1=(String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
        String code2=request.getParameter("checkCode");
        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(username,password);
        //处理数据,完成登录
        UserService userService=new UserServiceImpl();
        //获取用户列表的集合
        List<User> userList=(List<User>)getServletContext().getAttribute("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);
            request.getSession().setAttribute("loginUser", username);
            response.sendRedirect(request.getContextPath()+"/categoryList.jsp");
        }
    }
 
}

jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>        
<% 
String loginUser=(String)request.getSession().getAttribute("loginUser");
%>    
<!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, <%=loginUser%>!</h1>
                <p>请小心地新增图书信息,要是建了一个错误的就不好了。。。</p>
            </div>
            <div class="page-header">
                <h3><small>新建</small></h3>
            </div>
            <form class="form-horizontal" action="${pageContext.request.contextPath}/addBookServlet" method="post" enctype="multipart/form-data">
 
                <div class="form-group">
                    <label for="name" 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="name" 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">
                       <c:forEach items="${sessionScope.categoryList}" var="cList" varStatus="idx">
                       <option value="${cList.categoryId}" selected="">${cList.categoryName}</option>
                       </c:forEach>
                       <!-- 下拉列表的内容要从分类中进行读取,value值是分类id -->
                    </select>
                </div>
 
                 <div class="form-group">
                    <label for="name" 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="name" 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="name" 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>&nbsp;&nbsp;&nbsp;
                    </div>
                </div>
            </form>
        </div>
        <footer class="text-center" >
            copy@imooc
        </footer>
    </body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% 
String loginUser=(String)request.getSession().getAttribute("loginUser");
String flag=(String)request.getParameter("flag");
%>
<!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">
        <script type="text/javascript">
        var flag='<%=flag%>';
        if(flag=="1"){
            alert("您输入的分类已存在,请重新输入");
        }
        </script>
    </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, <%=loginUser%>!</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="name" 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="name" 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>&nbsp;&nbsp;&nbsp;
                    </div>
                </div>
            </form>
        </div>
        <footer class="text-center" >
            copy@imooc
        </footer>
    </body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<%@ 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="${pageContext.request.contextPath}/bookList.jsp" >图书信息管理</a>
                    </nav>
                    <nav>
                            <a href="${pageContext.request.contextPath}/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>&nbsp;&nbsp;&nbsp;
                    </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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<%@ 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="${pageContext.request.contextPath}/bookList.jsp" >图书信息管理</a>
                    </nav>
                    <nav>
                            <a href="${pageContext.request.contextPath}/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="${sessionScope.categoryList}" var="cList" varStatus="idx">
                        <c:if test="${sessionScope.categoryList!=null}">
                            <tr>
                                <td>${idx.index+1}</td>
                                <td>${cList.categoryId}</td>
                                <td>${cList.categoryName}</td>
                                <td><a href="${pageContext.request.contextPath}/deleteCategoryServlet?categoryId=${cList.categoryId}">删除</a></td>
                                <!--在循环显示数据时,此处的ca0001可以用EL表达式进行替换-->
 
                            </tr>
                           </c:if>
                        </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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String flag=request.getParameter("flag");
%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>登录</title>
        <link rel="stylesheet" href="css/login.css">
         <script type="text/javascript">
          function changeImg(){
              var codeImg=document.getElementById("codeImg");
              codeImg.src="${pageContext.request.contextPath}/KaptchaServlet?time="+new Date().getTime();
          }
          var flag='<%=flag%>';
          if(flag=="1"){
              alert("请登录系统");
          
        </script>
    </head>
    <body>
        <div class="login">
            <div class="header">
                <h1>
                    <a href="/login.do">登录</a>
                </h1>
                <button></button>
            </div>
            <form action="${pageContext.request.contextPath}/LoginServlet" method="post">
                <div class="name">
                    <input type="text" id="name" name="username" placeholder="用户名">
                    <p>${msg}</p>
                </div>
                <div class="pwd">
                    <input type="password" id="pwd" name="password" placeholder="密码">
                    <p></p>
                </div>
                <div class="code">
                    <input type="text" id="code" name="checkCode" placeholder="验证码" style="width: 150px">
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <img id="codeImg" onclick="changeImg()" 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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<%@ 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="name" 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="name" 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="name" 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="name" 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="name" 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>&nbsp;&nbsp;&nbsp;
                    </div>
                </div>
            </form>
        </div>
        <footer class="text-center" >
            copy@imooc
        </footer>
    </body>
</html>

xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>test6_2</display-name>
  <servlet>
    <servlet-name>KaptchaServlet</servlet-name>
    <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
    <init-param>
     <param-name>kaptcha.textproducer.char.length</param-name>
     <param-value>4</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
  <servlet-name>KaptchaServlet</servlet-name>
  <url-pattern>/KaptchaServlet</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>EncodeFilter</filter-name>
    <filter-class>com.imooc.filter.EncodeFilter</filter-class>
    <init-param>
    <param-name>charset</param-name>
    <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
  <filter-name>EncodeFilter</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter>
  <filter-name>AuthFilter</filter-name>
  <filter-class>com.imooc.filter.AuthFilter</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>AuthFilter</filter-name>
  <url-pattern>/categoryList.jsp</url-pattern>
  </filter-mapping>
 
</web-app>

jar包和js

http://img1.sycdn.imooc.com//climg/5d689f910001a66203170281.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

4回答
好帮手慕柯南 2019-09-01 09:48:30

同学你好!
1.clean一下项目,重新启动测试一下

2.如果不可以,同学找到项目在tomcat下部署的位置,删除,重新编译一下项目

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

  • 提问者 Mr__Gao #1
    我按照这些做了,项目也重新编译了,还是出现这个错误
    2019-09-01 10:21:05
好帮手慕阿莹 2019-08-31 11:22:12

同学你好,如下报错的意思是没有加载到这DeferrdeFileOutputStream,有可能是jar包没有加载好

http://img1.sycdn.imooc.com//climg/5d69e30a0001c5a207040350.jpg

建议同学在jar包上右键,看看有没有如下图这个选项:

http://img1.sycdn.imooc.com//climg/5d69e79e00011cfd05100049.jpg

如果有,请同学把所有的jar包都 add 一下,重启项目后在试试。

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 提问者 Mr__Gao #1
    老师,我全部都重新添加了一下,可是还是不行,还是这个错误
    2019-08-31 22:43:29
  • 好帮手慕柯南 回复 提问者 Mr__Gao #2
    同学重新创建一个项目测试看一下。祝学习愉快~
    2019-09-01 11:21:25
  • 提问者 Mr__Gao 回复 好帮手慕柯南 #3
    问题解决了 ,我上传了2个commons-fileupload,没上传io
    2019-09-01 11:51:03
提问者 Mr__Gao 2019-08-30 19:05:46

http://img1.sycdn.imooc.com//climg/5d6902bb000140bd07870687.jpg


再新增图书信息点击保存完以后就这样了

好帮手慕小班 2019-08-30 16:43:50

同学你好,这里建议同学能够将具体的报错信息贴出,是哪里的页面在跳转时报出错误,具体报错了500中的什么错误,还有建议同学将jsp页面的名称具体标识一下,便于我们复制运行代码。

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 提问者 Mr__Gao #1
    老师,图和问题发在下边了
    2019-08-30 19:06:16
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码