正在回答
参数应该用request.getParameter("");来接受,
request.getAttribute 是用来存入域中的参数的,
所以set进catgory对象中的应该是null,因此catgory.getCatgoryname()会是null。
同学可以接收参数的部分。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
1、和同学确认一下,同学说的是添加图书第一次正常,再次添加就报错,是添加图书还是添加图书分类呢?
2、空指针异常的原因是有值为null的对象去调用了方法或属性,建议同学可以自己打断点跟一下,看看if(catgory.getCatgoryname().equals(catgoryname))中catgory是null还catgory.getCatgoryname()是null呢?
3、1)可以重新一下catgory类的toString方法
2)可以在遍历前打印一下addlist,看看里边有没有名字是null的分类,
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.java.imooc.*,com.java.servlet.*,java.util.*"%>
<%
String basePath = request.getScheme() + ":" + "//"
+ request.getServerName() + ":" + request.getServerPort()
+ "/" + request.getServletContext().getContextPath() + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>图书添加</h1>
<form action="<%=basePath%>/AddBookServlet" method="post">
<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">
<%
List<Catgory> addlist=(List<Catgory>) this.getServletContext().getAttribute("addlist");
for(Catgory catgory:addlist){
%>
<option value="<%=catgory.getCatgoryname()%>"></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>
package com.java.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.java.imooc.*;
/**
* Servlet implementation class InitServlet
*/
public class InitServlet extends HttpServlet {
@Override
public void init() throws ServletException {
List<User> list=new ArrayList<User>();
List<Catgory> addlist=new ArrayList<Catgory>();
this.getServletContext().setAttribute("list",list);
this.getServletContext().setAttribute("addlist",addlist);
}
}
package com.java.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.java.imooc.Catgory;
import com.java.imooc.User;
/**
* Servlet implementation class CatgoryServlet
*/
@WebServlet("/CatgoryServlet")
public class CatgoryServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id=(String) request.getAttribute("id");
String catgoryname=(String) request.getAttribute("catgoryName");
String description=(String) request.getAttribute("description");
List<Catgory> addlist=(List<Catgory>) this.getServletContext().getAttribute("addlist");
for (Catgory catgory : addlist) {
if(catgory.getCatgoryname().equals(catgoryname)){
request.setAttribute("msg","该分类已经存在");
request.getRequestDispatcher("/catgory.jsp").forward(request, response);
return;
}
}
Catgory catgory=new Catgory();
catgory.setId(id);
catgory.setCatgoryname(catgoryname);
catgory.setDescription(description);
addlist.add(catgory);
this.getServletContext().setAttribute("addlist",addlist);
response.sendRedirect("/Book/catgory.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>图书分类添加</h1>
<form action="<%=basePath%>/CatgoryServlet" method="post">
<table width="400px" cellspacing="0px" cellpadding="0px" border="1px">
<tr>
<td>分类ID</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>名 字</td>
<td><input type="text" name="catgoryName"></td>
</tr>
<tr>
<td>描 述</td>
<td><input type="text" name="description"></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>
27行
44行
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星