将JSON对象传递给HttpServlet的问题

时间:2014-08-14 15:47:53

标签: java jquery json servlets

我有一个html表单,我想在提交表单时传递给servlet。表格信息是:

<form method="post" action = "/directory" name="dirinit" id="srchform"> 

我试图发布的jQuery代码是:

 $(document).ready(function(){
        $("form").on("submit", function(event){
            event.preventDefault();

            var formData = JSON.stringify(jQuery("form").serializeArray());
            $.post("/directory", formData)
            });
        });

servlet设置为:

public class NewDirectory extends HttpServlet{


  public void init(ServletConfig config) throws ServletException 
    {
        super.init(config);
    }

  public void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException 
  {

    doPost(request, response);
  }


  public void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException 
{
      response.setContentType("text/json");
      String form = request.getParameter("formData");
      System.out.println(form);
}
}

我的web.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>NewDirectory</display-name>
  <servlet>
    <servlet-name>newdirectory</servlet-name>
    <servlet-class>edu.msu.is.directory.newdirectory</servlet-class>

 </servlet>
<servlet-mapping>
  <servlet-name>newdirectory</servlet-name>
  <url-pattern>/directory</url-pattern>
</servlet-mapping>
  <welcome-file-list>
   <welcome-file>index.html</welcome-file>
   <welcome-file>index.htm</welcome-file>
   <welcome-file>index.jsp</welcome-file>
   <welcome-file>default.html</welcome-file>
   <welcome-file>default.htm</welcome-file>
   <welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

当我尝试发布表单数据时,收到404错误,指出未找到该网址。我对servlet很陌生,所以我甚至不确定我是否正确设置了servlet。

2 个答案:

答案 0 :(得分:0)

<servlet-class>edu.msu.is.directory.newdirectory</servlet-class>

区分大小写,应该是

<servlet-class>edu.msu.is.directory.NewDirectory</servlet-class>

答案 1 :(得分:0)

您的代码应该是这样的:

Servlet:

package edu.msu.is.directory;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class NewDirectory extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    public NewDirectory()
    {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType("text/json");
        String form = request.getParameter("formData");
        System.out.println(form);
    }
}

<强>的web.xml

...
<servlet>
    <servlet-name>NewDirectory</servlet-name>
    <servlet-class>edu.msu.is.directory.NewDirectory</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>NewDirectory</servlet-name>
    <url-pattern>/directory</url-pattern>
</servlet-mapping>
...

Html表单:

<form method="post" action="directory" name="dirinit" id="srchform">

Javascript:

$(document).ready(function(){
    $("form").on("submit", function(event){
        event.preventDefault();

        var formData = JSON.stringify(jQuery("form").serializeArray());
        $.post("directory", formData)
        });
    });    

注意:如果您收到404错误,则表示您正在访问其他网址,或者您的servlet / jsp未正确映射。

您可以在此处将操作网址设置为/directory,该网址应该只有directory/YourProjectContextRootPath/directory