读取数据会产生null

时间:2019-04-09 10:00:48

标签: javascript java jsp

我有一张包含数据的表。我要添加一条新记录。

enter image description here

但是在某些时候,不可能考虑我介绍的内容 控制台显示为空。读取数据会产生空值

enter image description here

post.jsp

 <h3 align="left">
        <label id="name_form_genre">Добавить должность</label>
    </h3>

    id Должности:
    <input type="text" id="post_id" style="width: 270px;" maxlength="15" value=""/>

    Название:
    <input type="text" id="post_name" style="width: 270px;" maxlength="15" value=""/>

    <div align="left"> <button onclick="add_post()" id="button_form_post">Добавить</button></div>

sample.js:

function add_post() {

var post_id = document.getElementById("post_id");
var post_name = document.getElementById("post_name");

if(post_id.value.toString().trim()=="" || post_name.value.toString().trim()=="")
{
    alert("Поля не должны быть пустые!")
}else {
    var xhr = new XMLHttpRequest();
    var body = 'action=add' + '&id=' + post_id.value + '&name=' + post_name.value;
    xhr.open("POST", "post", false);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    xhr.send(body);
    if (xhr.status != 200)
        alert(xhr.status);
    window.location = "/post";
}}

PostServlet.java:

@WebServlet("/post")
public class PostServlet extends HttpServlet{
private PostService postService = new PostService();
public PostServlet() throws SQLException, ClassNotFoundException { }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getRequestDispatcher("post.jsp").forward(request, response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) {
    try {
        PostEntity postEntity = new PostEntity();
        String operation = request.getParameter("action");
        switch (operation) {
            case "add":
                System.out.println("=====================");
                System.out.println("ADD");
                System.out.println("post_id: " + request.getParameter("post_id"));
                System.out.println("post_name: " + request.getParameter("post_name"));

                postEntity.setPost_id(Integer.parseInt(request.getParameter("post_id")));
                postEntity.setPost_name(request.getParameter("post_name"));
                System.out.println("=====================");
                postService.insert(postEntity);
                break;
        }
        request.getRequestDispatcher("post.jsp").forward(request, response);
    } catch (Exception e) {
    e.printStackTrace();
}
}}

有人可以帮助确定问题。预先感谢。

1 个答案:

答案 0 :(得分:0)

您正在id文件中将请求参数设置为name.js,但是在Java中却获得了请求参数post_idpost_name。您可以在请求中验证吗? 错误消息显示post_idpost_name为空。

改为使用idname

postEntity.setPost_id(Integer.parseInt(request.getParameter("id")));
postEntity.setPost_name(request.getParameter("name"));