关于分类添加和图书添加报错的问题

关于分类添加和图书添加报错的问题

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ page import="java.util.*"%>

<%

String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";

%>


<!DOCTYPE html>

<html>

<head>

<base href="<%=basePath%>">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>图书添加</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">

<option value=""></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.vincent.system;


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;


/**

 * Servlet implementation class AddBookServlet

 */

@WebServlet("/AddBookServlet")

public class AddBookServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doPost(request,response);

}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

request.setCharacterEncoding("utf-8");

response.setCharacterEncoding("utf-8");

        LibSeviceImpl lsi = new LibSeviceImpl();

String id =request.getParameter("id");

String bookName=request.getParameter("bookName");

String catgoryName=request.getParameter("catgoryName");

int price=Integer.parseInt(request.getParameter("price"));

String description = request.getParameter("description");

Book book = new Book(id,catgoryName,bookName,price,description);

lsi.addBook(book);

request.getRequestDispatcher("/addBook.jsp").forward(request, response);

//response.sendRedirect(request.getContextPath()+"/server.jsp");

}


}


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

 <%

    String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";

%>

<%

String msg="";

if(request.getAttribute("msg")!=null){

msg=(String)request.getAttribute("msg");

}

%>

<!DOCTYPE html>

<html>

<head>

<base href="<%=basePath%>">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>图书分类添加</title>

</head>

<body>

<center>

<h1>图书分类添加</h1>

<form action="<%=basePath%>CatgoryServlet" method="post">

<h3><font color="red"><%=msg %></font></h3>

<table width="400px" cellspacing="0px" cellpadding="0px" border="1px">

<tr>

<td>分类ID</td>

<td><input type="text" name="id"></td>

</tr>

<tr>

<td>名&nbsp;字</td>

<td><input type="text" name="catgoryName"></td>

</tr>

<tr>

<td>描&nbsp;述</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>


package com.vincent.system;


import java.io.IOException;

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;


/**

 * Servlet implementation class CategoryServlet

 */

@WebServlet("/CategoryServlet")

public class CategoryServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public CategoryServlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//接收来自category.jsp的数据

request.setCharacterEncoding("utf-8");

response.setCharacterEncoding("utf-8");

        Long id = Long.valueOf(request.getParameter("id"));

        String categoryName = request.getParameter("categoryName");

        String description = request.getParameter("description");

        LibDaoImpl ldi = new LibDaoImpl();

        List<Map<String, Object>> catgoryList = ldi.getAllBookCatgory();

        for(Map<String, Object> map:catgoryList) {

        if(map.get("id").equals(id)||map.get("catgoryName").equals(categoryName)) {

        request.setAttribute("msg","id或类名已存在,请重新输入!");

        response.sendRedirect(request.getContextPath()+"/catgory.jsp");

        return;

        }

        }

        ldi.addBookCatgory(id,categoryName,description);

        request.getRequestDispatcher("/server.jsp").forward(request,response);

}


}

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

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


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

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

1回答
好帮手慕柯南 2019-06-03 18:00:11

同学你好!

  1. 出现404的问题:首先查看跳转的地址是够正确,同学这里跳转的地址是否写错了呢,同学的servlet中写的是CategoryServlet

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

    这里返回catgory.jsp就行

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

  2. 报了500,首先要根据报错的信息,来定位哪里出错了

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

    根据报错信息出错的地方时AddBookServlet.java的第52行。

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

    这里跳转页面就跳转在AddBookServlet中,此时没有信息传过来,所以无法转为数字。

    解决方法直接跳转静态页面。

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

  • 提问者 浮生Y一梦 #1
    老师,修改后还是报错
    2019-06-03 19:35:59
  • 提问者 浮生Y一梦 #2
    老师,第一次分类添加完之后,第二次添加就报错
    2019-06-03 19:41:29
  • 好帮手慕柯南 回复 提问者 浮生Y一梦 #3
    同学报什么错呢,贴一下报错的信息呢,以及代码呢,祝学习愉快~
    2019-06-04 09:40:18
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
从网页搭建入门Java Web2018版
  • 参与学习           人
  • 提交作业       1088    份
  • 解答问题       10205    个

如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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