混合ajax和jsp以将数据导入servlet

时间:2022-03-15 11:53:35

I've got a jsp file with two text fields (signUp and post). Now, I want the post text and the signUp text to be called to a servlet. Normally its going with request.getParameter(), but now I want the post text going to the servlet with an AJAX function and the signup text with the normal way (that means the name of the input within the jsp file and then request.getParameter).

我有一个带有两个文本字段的jsp文件(signUp和post)。现在,我希望将post文本和signUp文本调用到servlet。通常它与request.getParameter()一起使用,但是现在我希望post文本使用AJAX函数进入servlet并使用正常方式注册文本(这意味着jsp文件中输入的名称,然后是request.getParameter) )。

Is it possible to mix both parts within one servlet because i have this:

是否可以在一个servlet中混合两个部分,因为我有:

<form name="form1" method="POST" action="PostWallServlet" id="form1"> 

form1 is the ajax code. I don't know how this should work. Normally there stands

form1是ajax代码。我不知道这应该如何运作。通常有立场

`<form action="PostWallServlet"  method="POST" >

and everything is callable through the Servlet. But, for now I don't know how I can mix up both components.

并且所有东西都可以通过Servlet调用。但是,现在我不知道如何混淆这两个组件。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PostWall pw=new PostWall();
    SimpleDateFormat df = new SimpleDateFormat("YYYY/MM/DD hh:mm:ss");
    Calendar cal = Calendar.getInstance();
    System.out.println("Current Date Time : " + df.format(cal.getTime()));

    String message = "";
    String sender = request.getParameter("sender");  
    String post = request.getParameter("message");
    String a= df.format(cal.getTime()).toString();

    pw.setSender(sender);
    pw.setPost(post);
    pw.setDate(a);
    if (pwi.addPost(pw)) {  
           message = "Student Successfuly Added";  
          } else {  
           message = "Student Adding Failed";  
          }  

    //RequestDispatcher rd = request.getRequestDispatcher("post.jsp");  
      //rd.forward(request, response);  
}




$(document).ready(function(){
  $('#Add').click(function(){
           sendData();
    });
   });
function sendData(){
   var mge = $('#newText').val();
    alert(mge);
    $.ajax({
        type: "POST",
        url: "PostWallServlet",
        data: { message : mge  }
      }).done(function( msg ) {
        alert( "Data Saved: " + msg );
      });
}
        </script>
      <form name="form1" method="GET" action="PostWallServlet" id="form1"></form>

      <table border="0" width="100%">  
            <tr>  
                <td colspan="3">  ${message}</td>  
            </tr>  


            <tr>  
                <td>Sender</td>  
                <td>:</td>  
                <td><input type="text" name="sender" value="" size=20/></td>  
            </tr> 
            <tr>  
                <td>Post</td>  
                <td>:</td>  
                <td><input type="text" name="post" value="" size=500 id="newText"/ ></td>  
            </tr>  



            <tr>  
                <td></td>  
                <td></td>  
                <td><input type="submit" value="Add" name="Add" /></td>  
            </tr>  
        </table>  

Any solutions?

1 个解决方案

#1


0  

Put your ending tag for form AFTER all your inputs:

在您的所有输入后放置表格的结束标记:

<form name="form1" method="GET" action="PostWallServlet" id="form1">
...
<td><input type="text" name="sender" value="" size=20 /></td>
...    
<td><input type="text" name="post" value="" size=500 id="newText" /></td>  
...
<td><input type="submit" value="Add" name="Add" /></td>  
...
</form>

Your inputs must be INSIDE the form, not after the form.

您的输入必须在表单内,而不是在表单之后。

Also make sure to end your input tags with /> not / >. You have a space between / and > on one of them.

另外,请确保使用/> not />结束输入标记。其中一个之间有/和>之间有空格。

For the Ajax part you need to give your inputs ids as well as names:

对于Ajax部分,您需要提供输入ID和名称:

<td><input type="text" name="sender" id="sender" value="" size=20 /></td>

And then in your Ajax function for data:

然后在您的Ajax函数中获取数据:

 data: { sender: $('#sender').val(), post:  $('#post').val() }

#1


0  

Put your ending tag for form AFTER all your inputs:

在您的所有输入后放置表格的结束标记:

<form name="form1" method="GET" action="PostWallServlet" id="form1">
...
<td><input type="text" name="sender" value="" size=20 /></td>
...    
<td><input type="text" name="post" value="" size=500 id="newText" /></td>  
...
<td><input type="submit" value="Add" name="Add" /></td>  
...
</form>

Your inputs must be INSIDE the form, not after the form.

您的输入必须在表单内,而不是在表单之后。

Also make sure to end your input tags with /> not / >. You have a space between / and > on one of them.

另外,请确保使用/> not />结束输入标记。其中一个之间有/和>之间有空格。

For the Ajax part you need to give your inputs ids as well as names:

对于Ajax部分,您需要提供输入ID和名称:

<td><input type="text" name="sender" id="sender" value="" size=20 /></td>

And then in your Ajax function for data:

然后在您的Ajax函数中获取数据:

 data: { sender: $('#sender').val(), post:  $('#post').val() }