2014-03-19 86 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

相關問題