dis.forward(request,response)报空指针问题
在写图书管理系统的时候,登录、借书和还书都报了同样的错误,错误和目录结构如图这里我列出我觉得和借书有关的代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>一般用户页面</title>
</head>
<body>
<label><a href="chabook.jsp" style="text-decoration: none;">查询所有图书信息</a></label><br><br>
<label><a href="jiebook.jsp" style="text-decoration: none">借阅图书</a></label><br><br>
<label><a href="huanbook.jsp" style="text-decoration: none">归还图书</a></label>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head><title></title></head>
<!-- 根据图书编号和图书名字借阅图书 -->
<body>
<form action="borrow" method="post">
请输入所借阅书籍的编号<input type="text" name="bid"><br><br>
请输入所借阅书籍的书名<input type="text" name="bname"><br><br>
请输入借阅本数(注意,你总共只能借阅8本)<input type="text" name="bbeizhu"><br><br>
<input type="submit" value="确定">
<input type="reset" value="重填">
</form>
</body>
</html>
这里是web.xml的部分配置
<servlet>
<servlet-name>Borrow</servlet-name>
<servlet-class>servlets.Borrow</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Borrow</servlet-name>
<url-pattern>/borrow</url-pattern>
</servlet-mapping>
servlets包下的Borrow.java类
/**
*
*/
package servlets;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import beans.JdbcUtil;
/**
* @author SwordJM
* @date:2019-5-3
* @ClassName
* @Description 借书,相应书籍本数减
*/
public class Borrow extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException{
request.setCharacterEncoding("UTF-8");
int bid = 0;
int bbeizhu = 0;
try {
bid = Integer.parseInt(request.getParameter("bid"));
bbeizhu = Integer.parseInt(request.getParameter("bbeizhu"));//书籍本数
} catch (NumberFormatException e) {
// TODO: handle exception
e.printStackTrace();
}
String bname = request.getParameter("bname");
String sql = "update book set bbeizhu = bbeizhu - ? where bid = ? and bname = ?";
int i = 0;
try {
i = JdbcUtil.executeDML(sql,bbeizhu,bid,bname);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
RequestDispatcher dis = null;
if(i > 0){
dis = request.getRequestDispatcher("../../WebRoot/success.jsp");//提示操作成功
}
else {
dis = request.getRequestDispatcher("../../WebRoot/fail.jsp");//提示操作失败
}
dis.forward(request, response);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doPost(request, response);
}
}
这里是Borrow.java中用到的beans报下的JdbcUtil.java类的部分函数
//封装DML(针对增删改)
public static int executeDML(String sql,Object...objs) throws SQLException, ClassNotFoundException{
Connection conn = Connect.getConnection();
PreparedStatement ps = getPreparedStatement(conn, sql);
//给占位符赋值
try {
conn.setAutoCommit(false);
for(int i = 0;i < objs.length;i++){
ps.setObject(i+1, objs);
}
int i = ps.executeUpdate();
conn.commit();
return i;
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}finally{
//关闭资源
Connect.closeAll(null, ps, conn);
}
return -1;
}
页:
[1]