访问服务器报错,系统打印却没有报错

访问服务器报错,系统打印却没有报错

package com.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.alibaba.fastjson.JSON;
import com.web.domain.Book;
import com.web.domain.Category;
import com.web.service.BookService;
import com.web.service.impl.BookServiceImpl;
import com.web.utils.CategoryChangeIdUtils;

@WebServlet("/searchBook")
public class SearchBook extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String searchContent=request.getParameter("searchContent");
		List<Book> books=(List<Book>) request.getServletContext().getAttribute("books");
		List<Category> categories=(List<Category>) request.getServletContext().getAttribute("categories");
		BookService bookService=new BookServiceImpl();
		Book serchBook=bookService.getBookById(searchContent, books);
		System.out.println(searchContent.equals(""));
		if(searchContent.equals("")) {
			System.out.println("running null");
			String json=JSON.toJSONString(books);
			System.out.println(json);
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().println(json);
			return;
		}
		if(serchBook!=null) {	
			String json=JSON.toJSONString(serchBook);
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().println(json);
		}else {
			String CategoryId=CategoryChangeIdUtils.getCategoryIdByName(searchContent, categories);
			List<Book> searchList=bookService.getBookByCatgoryName(CategoryId, books);
			String json=JSON.toJSONString(searchList);
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().println(json);
		}
	}

	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"%>
<%@ 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">
        <script type="text/javascript" src="js/jquery-3.4.1.js"></script>
      	<script type="text/javascript">
      		
      		$(function(){
      			document.getElementById("checkBtn").onclick=function(){
      				var searchContent=document.getElementById("searchContent").value;
      				console.log(searchContent+"success");
      				$.ajax({
      					"url":"/book_manager/searchBook",
      					"type":"get",
						"data":{"searchContent":searchContent},
						"dataType":"json",
						"success":function(json){
							var html=""
							if(json!=null){
								for(var i=0;i<json.length;i++){
									html=html+"<tr>";
									html=html+"<td>"+(i+1)+"</td>";
									html=html+"<td>"+json[i].bookId+"</td>";
									html=html+"<td>"+json[i].bookName+"</td>";
									html=html+"<td>"+json[i].categoryId+"</td>";
									html=html+"<td>¥"+json[i].bookPrice+"</td>";
									html=html+"<td><img src=\""+json[i].path+"\"</td>";
									html=html+"<td>";
									html=html+"<a href=\"/book_manager/updateBook?bookid="+json[i].bookId+"\">修改</a>";
									html=html+"<a href=\"/book_manager/deleteBook?bookid="+json[i].bookId+"\">删除</a>";
									html=html+"</td>";
									html=html+"</tr>";
								}
								$("#tr1").append(html);
							}
						}
      				})
      			}
      		})
      	</script>
    </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" 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" id="checkBtn">查询</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="${applicationScope.books}" var="b" varStatus="idx">
                            <tr id="tr1">
                                <td>${idx.index+1 }</td>
                                <td>${b.bookId }</td>
                                <td>${b.bookName }</td>
                                <c:forEach items="${applicationScope.categories}" var="c">
                                <c:if test="${b.categoryId==c.categoryId}">
                                <td>${c.categoryName }</td>
                                </c:if>
                                </c:forEach>
                                <td>¥${b.bookPrice}</td>
                                <td><img src="${b.path}"></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="addBook.jsp"><button>新建</button></a>
                </div>
            </div>
        </section>
        <footer>
            copy@慕课网
        </footer>
    </body>
</html>

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

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


访问服务器提示我空指针异常,然后我不访问服务器,通过后台打印都能打印出来,找了好久没找到原因


正在回答

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

2回答

同学你好!

同学通过ajax访问searchContent是可以获取到的,如果同学直接输入地址访问servlet,searchContent是获取不到的。因为没有传递searchContent参数,同学也可以在访问的地址后面看到没有searchContent参数:

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

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

好帮手慕柯南 2019-08-13 18:44:19

同学你好!

同学是说直接输入地址访问servlet,不通过页面来操作会报错吗?如果是:直接通过地址访问servlet,这样searchContent参数的值为NUll,所以会报空指针呢。所以同学需要通过页面的来访问这个地址。

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

  • 提问者 慕设计2030095 #1
    因为之前ajax,是需要打印到servlet上面的,现在访问servlet报错,现在有点摸不清头脑
    2019-08-13 18:47:13
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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