点击添加图书报错
package com.imooc.servlet;
import java.io.IOException;
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.bean.Book;
/**
* 添加图书的Servlet
*/
@WebServlet("/AddBookServlet")
public class AddBookServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//定义一个Map集合,用于存放接收到的数据
Map<String,String> map=new HashMap<String,String>();
//创建一个磁盘文件项工厂
DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
//创建一个核心解析类
ServletFileUpload servletFileUpload=new ServletFileUpload(diskFileItemFactory);
//解析request请求,返回List对象,List中存放的是FileItem
List<FileItem> list=servletFileUpload.parseRequest(request);
//遍历集合,获得每个FileItem
for(FileItem fileItem:list) {
if(fileItem.isFormField()) {
//获得表单项name值
String name=fileItem.getFieldName();
//获得表单项value值
String value=fileItem.getString("UTF-8");
System.out.println(name+""+value);
map.put(name, value);
}
}
List<Book> bookList=(List<Book>) this.getServletContext().getAttribute("list");
//判断书名是否存在
for(Book bo:bookList) {
if(bo.getBookName().equals(map.get("bookName"))) {
request.setAttribute("msg", "书名已存在!");
request.getRequestDispatcher("/addBook.jsp").forward(request, response);
}
}
//封装数据到Book当中
Book book=new Book();
book.setId(map.get("id"));
book.setBookName(map.get("bookName"));
book.setCatgoryName(map.get("catgoryName"));
book.setPrice(map.get("price"));
book.setDescription(map.get("description"));
//将注册用户的信息存入到List集合中
bookList.add(book);
for(Book b:bookList) {
System.out.println(b);
}
//添加成功,跳转到显示界面
this.getServletContext().setAttribute("bookList", bookList);
request.getSession().setAttribute("bookName", book.getBookName());
response.sendRedirect(request.getContextPath()+"/showBooks.jsp");
System.out.println(map);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
0
收起
正在回答
12回答
在域中要用不同的key值来存放userList和bookList。否则,域中存的key为“list”就会被最近使用的覆盖。
例如,初始化的时候,bookList集合就覆盖了原来存进去的userList集合,
当存储用户时,去取域中初始化的集合,此时域中key为“list”中存的是bookList集合。
public void init() throws ServletException {
List<User> userList=new ArrayList<User>();
this.getServletContext().setAttribute("list", userList);
List<Book> bookList=new ArrayList<Book>();
this.getServletContext().setAttribute("list", bookList);
}如上边同学的代码可以改成:
public void init() throws ServletException {
List<User> userList=new ArrayList<User>();
this.getServletContext().setAttribute("userList", userList);
List<Book> bookList=new ArrayList<Book>();
this.getServletContext().setAttribute("bookList", bookList);当存用户的时候,在域中把userList集合取出来:
this.getServletContext().getAttribute("userList");
当存图书的时候,在域中把bookList集合取出来:
this.getServletContext().getAttribute("bookList");
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
精慕门6573819
2018-10-31 16:58:59
package com.imooc.servlet;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import com.imooc.bean.Book;
import com.imooc.bean.User;
/**
* 用于保存信息的Servlet
*/
public class InitServlet extends HttpServlet {
@Override
public void init() throws ServletException {
List<User> userList=new ArrayList<User>();
this.getServletContext().setAttribute("list", userList);
List<Book> bookList=new ArrayList<Book>();
this.getServletContext().setAttribute("list", bookList);
}
}
精慕门6573819
2018-10-31 16:58:17
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + request.getServletContext().getContextPath(); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>图书添加</title> </head> <body> <center> <h1>图书添加</h1> <form action="<%=basePath%>/AddBookServlet" method="post" enctype="multipart/form-data"> <table width="400px" cellspacing="0px" cellpadding="0px" border="1px"> <tr> <td>图书ID</td> <td><input type="text" name="id" placeholder="请输入数字" pattern="\d+" required="required"></td> </tr> <tr> <td>图书名</td> <td><input type="text" name="bookName"></td> </tr> <tr> <td>图书分类</td> <td><select name="catgoryName"> <option value="" name="catgoryName">言情</option> <option value="" name="catgoryName">武侠</option> <option value="" name="catgoryName">都市</option> </select></td> </tr> <tr> <td>价格</td> <td><input type="text" name="price" placeholder="请输入价格"></td> </tr> <tr> <td>描述</td> <td><input type="text" name="description" placeholder="请输入描述信息"></td> </tr> <tr> <td colspan="2" style="text-align: center"><input type="submit" value="添加"> <input type="reset" value="重置"> </td> </tr> </table> </form> </center> </body> </html>
精慕门6573819
2018-10-31 16:57:50
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + request.getServletContext().getContextPath(); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>图书后台管理</title> </head> <body> <p><a href="<%=basePath%>/catgory.jsp" target="main">分类添加</a></p> <p><a href="<%=basePath%>/addBook.jsp" target="main">图书添加</a></p> <p><a href="<%=basePath%>/SelectBookServlet" target="main">图书查询</a></p> </body> </html>
精慕门6573819
2018-10-31 16:56:30
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>图书后台管理系统</title> </head> <frameset rows="20%,*"> <frame src="./top.jsp"></frame> <frameset cols="10%,*"> <frame src="./left.jsp"></frame> <frame name="main"></frame> </frameset> </frameset> </html>
精慕门6573819
2018-10-31 16:55:46
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.bean.User;
/**
* 登陆的Servlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接收用户输入的数据
String username=request.getParameter("username");
String password=request.getParameter("password");
//从ServletContext作用域中获得保存用户信息的集合
List<User> list=(List<User>) this.getServletContext().getAttribute("list");
//遍历集合
for(User u:list) {
//判断用户名是否正确
if(username.equals(u.getUsername())) {
//判断密码是否正确
if(password.equals(u.getPassword())) {
//登陆成功
//将用户的信息保存到session中
request.getSession().setAttribute("user", u);
//跳转
response.sendRedirect(request.getContextPath()+"/server.jsp");
return;
}
}
}
//登陆失败
request.setAttribute("msg", "用户名密码不正确");
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
精慕门6573819
2018-10-31 16:55:13
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ "/" + request.getServletContext().getContextPath();
%>
<%
String username="";
if(session.getAttribute("username")!=null){
username=(String)session.getAttribute("username");
}
%>
<%
String msg="";
if(request.getAttribute("msg")!=null){
msg=(String)request.getAttribute("msg");
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书后台管理登录界面</title>
</head>
<body>
<center>
<h1>登录 | <a href="<%=basePath%>/regist.jsp">注册</a></h1>
<form action="<%=basePath%>/LoginServlet" method="post">
<table width="350px" cellspacing="0px" cellpadding="0px" border="1px">
<h3><%=msg %></h3>
<tr>
<td>用户名</td>
<td><input type="text" name="username" placeholder="用户名为3-12位字母数字或下划线组合" value="<%=username %>"></td>
</tr>
<tr>
<td>密 码</td>
<td><input type="password" name="password" placeholder="长度为6-12位的纯数字" ></td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="登录">
<input type="reset" value="取消">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
精慕门6573819
2018-10-31 08:43:02
错误代码图中红框
精慕门6573819
2018-10-31 08:39:15
package com.imooc.servlet;
import java.io.IOException;
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.bean.Book;
/**
* 添加图书的Servlet
*/
@WebServlet("/AddBookServlet")
public class AddBookServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//定义一个Map集合,用于存放接收到的数据
Map<String,String> map=new HashMap<String,String>();
//创建一个磁盘文件项工厂
DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
//创建一个核心解析类
ServletFileUpload servletFileUpload=new ServletFileUpload(diskFileItemFactory);
//解析request请求,返回List对象,List中存放的是FileItem
List<FileItem> list=servletFileUpload.parseRequest(request);
//遍历集合,获得每个FileItem
for(FileItem fileItem:list) {
if(fileItem.isFormField()) {
//获得表单项name值
String name=fileItem.getFieldName();
//获得表单项value值
String value=fileItem.getString("UTF-8");
System.out.println(name+""+value);
map.put(name, value);
}
}
List<Book> bookList=(List<Book>) this.getServletContext().getAttribute("list");
//封装数据到Book当中
Book book=new Book();
book.setId(map.get("id"));
book.setBookName(map.get("bookName"));
book.setCatgoryName(map.get("catgoryName"));
book.setPrice(map.get("price"));
book.setDescription(map.get("description"));
//将注册用户的信息存入到List集合中
bookList.add(book);
for(Book b:bookList) {
System.out.println(b);
}
//添加成功,跳转到显示界面
this.getServletContext().setAttribute("bookList", bookList);
request.getSession().setAttribute("bookName", book.getBookName());
response.sendRedirect(request.getContextPath()+"/showBooks.jsp");
System.out.println(map);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
精慕门6573819
2018-10-31 08:38:31
package com.imooc.bean;
/**
*
* 用户类
*
*/
public class User {
private String username;
private String password;
private String checkPWD;
private String phone;
private String email;
public User() {
}
public User(String username, String password, String checkPWD, String phone, String email) {
super();
this.username = username;
this.password = password;
this.checkPWD = checkPWD;
this.phone = phone;
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public String getCheckPWD() {
return checkPWD;
}
public void setCheckPWD(String checkPWD) {
this.checkPWD = checkPWD;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", checkPWD=" + checkPWD + ", phone=" + phone
+ ", email=" + email + "]";
}
}
精慕门6573819
2018-10-31 08:37:56
package com.imooc.bean;
/**
*
* 图书类
*
*/
public class Book {
private String id;
private String bookName;
private String catgoryName;
private String price;
private String description;
public Book() {
}
public Book(String id, String bookName, String catgoryName, String price, String description) {
super();
this.id = id;
this.bookName = bookName;
this.catgoryName = catgoryName;
this.price = price;
this.description = description;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getCatgoryName() {
return catgoryName;
}
public void setCatgoryName(String catgoryName) {
this.catgoryName = catgoryName;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Book [id=" + id + ", bookName=" + bookName + ", catgoryName=" + catgoryName + ", price=" + price
+ ", description=" + description + "]";
}
}
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程

恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星