访问服务器报错,系统打印却没有报错
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>
</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>

访问服务器提示我空指针异常,然后我不访问服务器,通过后台打印都能打印出来,找了好久没找到原因
0
收起
正在回答
2回答
同学你好!
同学通过ajax访问searchContent是可以获取到的,如果同学直接输入地址访问servlet,searchContent是获取不到的。因为没有传递searchContent参数,同学也可以在访问的地址后面看到没有searchContent参数:

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题
登录后可查看更多问答,登录/注册
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星