H_Franky
2019-12-13 16:29
采纳率: 27.8%
浏览 278
已采纳

java web的jsp里text获取不到数据,只获取到null!求解,相关代码如下!

我屏蔽掉那里则能存到数据库,但是是null,已经填了东西上去了,如果不屏蔽的话输出“书名不能为空”,没有报错!System.out.println(sql);了一下是输出insert into book(bookname,bookauthor,bookpress,bookedition,booklocation) values('null','null',null,'null','null'),这要怎么解决,求大佬帮忙!

//添加书本(控制部分)
public boolean addBook(String bookname, String bookauthor, String bookpress,String bookedition,String booklocation)
{
    Connection conn = null;
    Statement stmt = null;
    try {
        conn = Database.getConnection();
        stmt = conn.createStatement();
        String sql = "insert into book(bookname,bookauthor,bookpress,bookedition,booklocation) "
                + "values('"+bookname+"','"+bookauthor+"',"+bookpress+",'"+bookedition+"','"+booklocation+"')";
        //System.out.println(sql);
        stmt.execute(sql);
        return true;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    } finally {
        Database.close(conn, stmt);
    }
}

//用的是servlet的doget和dopost,下面为servlet部分
            String bookname = request.getParameter("bookname");//初始化图书的各个字段
        String bookauthor = request.getParameter("bookauthor");
        String bookpress = request.getParameter("bookpress");
        String bookedition = request.getParameter("bookedition");
        String booklocation = request.getParameter("booklocation");

// if(bookname==null||bookname.trim().isEmpty()){

// request.setAttribute("msg", "书名不能为空");

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

// return;

// }

// if(bookauthor==null||bookauthor.trim().isEmpty()){

// request.setAttribute("msg", "作者不能为空");

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

// return;

// }

// if(bookpress==null||bookpress.trim().isEmpty()){

// request.setAttribute("msg", "出版社不能为空");

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

// return;

// }
// if(bookedition==null||bookedition.trim().isEmpty()){

// request.setAttribute("msg", "版本不能为空");

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

// return;

// }
// if(booklocation==null||booklocation.trim().isEmpty()){

// request.setAttribute("msg", "出版存放位置不能为空");

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

// return;

// }

  Operator operate = new Operator();
        boolean isSuccess = operate.addBook(bookname, bookauthor, bookpress, bookedition,booklocation);
        if(isSuccess){
            request.setAttribute("msg", "增加成功");
            request.getRequestDispatcher("/addbook.jsp").forward(request, response);  
            return;
        }else{
            request.setAttribute("msg", "增加失败");
            request.getRequestDispatcher("/addbook.jsp").forward(request, response);  
            return;
        }

        //下面是jsp部分
        <form action="AddBookServlet" method="post" enctype="multipart/form-data">
    <div>
    <input type="text" name="bookname" placeholder="请输入书名(如:三国演义)"></div>     
    <div>
    <input type="text" name="bookauthor" placeholder="请输入作者(如:张三)"></div> 
    <div>
    <input type="text" name="bookpress" placeholder="请输入出版社(如:中山大学出版社)"></div>
    <div>
    <input type="text" name="bookedition" placeholder="请输入版本(如:第一版)"></div> 
    <div>
    <input type="text" name="booklocation" placeholder="请输入存放位置(如:1楼101室2019)"></div>     
    <!--  <div>
    <a href="javascript:;" class="a-upload">点击这里上传文件(.txt文件)
    <input type="file" name="file" id="file">
    </a></div>  -->      
    <div>   
    <input type="submit" value="增加图书" id="addbook">
    </div>       
    <div>       
    <a href="welcome.jsp" id="back">返回菜单</a></div></br>
    </form>

    <font color="red" size="2"> ${msg }</font>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题