2014-03-19 106 views
0

我有一个servlet,我的目标是从进程请求中返回一个客户对象,然后可以在我的jquery中访问此对象。有谁知道我可以怎么做呢?将自定义对象从servlet传递给Jquery

e.g. myObject.getMethod() 

servlet代码:

Customer loginResult; 

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html;charset=UTF-8"); 
     PrintWriter out = response.getWriter(); 
     try { 
      /* TODO output your page here. You may use following sample code. */ 
      //request.setAttribute("customerFirstName", loginResult.getFirstName()); //String Value 
      //request.setAttribute("customerID", loginResult.getCustomerID()); //IntegerValue 
      out.println(loginResult); 

     } finally { 
      out.close(); 
     } 
    } 

JSP代码:

<script type="text/javascript"> 
$().ready(function() { 
    $('#submit').click(function() { 

     var dataf = 'email=' + $('#email').val() 
       + '&password=' + $('#password').val(); 
     $.ajax({ 
      url: "http://localhost:8080/RetailerGui/loginServlet", 
      type: "get", 
      data: dataf, 
      success: function(data) { 
      alert(data); 

      } 
     }); 
     return false; 
    }); 
}); 
</script> 

是否有人可以帮助我解决这个问题,感谢您对您的帮助提前。

+1

这不是JSP代码,这只是javascript ... – Virus721

+0

@ Virus721抱歉删除了标签我的代码是在jsp文件中。所以我不小心选了它。 – KSM

+0

这可能有助于http://stackoverflow.com/questions/3832792/access-request-object-in-javascript – Susie

回答

0

由于您想使用Servlet处理ajax请求,因此您最好的办法就是将自定义对象的数据写入响应中。我发现实现这个更简单的方法是使用JSON。有很多库处理从对象到字符串的JSON转换,反之亦然,我推荐使用Jackson。这就是你的代码的样子。

servlet代码:

import com.fasterxml.jackson.databind.ObjectMapper; 

@WebServlet("/loginServlet") //assuming you're using Servlet 3.0 
public class YourServlet extends HttpServlet { 

    //Jackson class that handles JSON marshalling 
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); 

    //login operation should be handled in POST 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
     Customer loginResult = ...; //process data and get the loginResult instance 
     response.setContentType("application/json"); 
     response.setCharacterEncoding("UTF-8"); 
     //marshalling the data of your loginResult in JSON format 
     String json = OBJECT_MAPPER.writeValueAsString(loginResult); 
     response.getWriter().write(json); 
    } 
} 

Javascript代码:

<script type="text/javascript"> 
$().ready(function() { 
    $('#submit').click(function() { 
     var dataf = 'email=' + $('#email').val() 
       + '&password=' + $('#password').val(); 
     $.ajax({ 
      url: "http://localhost:8080/RetailerGui/loginServlet", 
      type: "post", //login action MUST be post, NEVER a get 
      data: dataf, 
      success: function(data) { 
       //shows the relevant data of your login result object in json format 
       alert(data); 
       //parsing your data into a JavaScript variable 
       var loginResult = JSON && JSON.parse(data) || $.parseJSON(data); 
       //now you can use the attributes of your loginResult easily in JavaScript 
       //for example, assuming you have a name attribute in your Customer class 
       alert(loginResult.name); 
      } 
     }); 
     return false; 
    }); 
}); 
</script> 

更多信息:

+0

完美的让我试试这个希望它有效:) – KSM

相关问题