Guest User

Untitled

a guest
May 19th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. <script>
  2. function getCustomerDetailsAjax(str) {
  3. str = $('#customerId').val();
  4.  
  5. if (document.getElementById('customerId').value <= 0) {
  6. document.getElementById('firstName').value = " ";
  7. document.getElementById('telephone').value = " ";
  8. document.getElementById('vehicleMake').value = " ";
  9. document.getElementById('vehicleModel').value = " ";
  10. document.getElementById('vehicleColor').value = " ";
  11. } else {
  12. $.ajax({
  13. url: "GetCustomerDetails",
  14. type: 'POST',
  15. data: {customerId: str},
  16. success: function (data) {
  17. alert(data); //I want to get 2 servlet values and alert them here. How can I do that?
  18. }
  19. });
  20. }
  21. }
  22. </script>
  23.  
  24. public class GetCustomerDetails extends HttpServlet {
  25.  
  26. @Override
  27. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  28. throws ServletException, IOException {
  29. PrintWriter out=response.getWriter();
  30. int customerId = Integer.valueOf(request.getParameter("customerId"));
  31. try {
  32. Class.forName("com.mysql.jdbc.Driver");
  33. Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Vehicle", "root", "");
  34. PreparedStatement ps = con.prepareStatement("SELECT fistname,telephone FROM customers WHERE customerid=?");
  35. ps.setInt(1, customerId);
  36. ResultSet result=ps.executeQuery();
  37. if(result.next()){
  38. out.print(result.getString("firstname")); //I want to send this value
  39. out.print(result.getString("telephone")); //and this value
  40.  
  41. }
  42.  
  43. } catch (ClassNotFoundException ex) {
  44. Logger.getLogger(GetCustomerDetails.class.getName()).log(Level.SEVERE, null, ex);
  45. } catch (SQLException ex) {
  46. Logger.getLogger(GetCustomerDetails.class.getName()).log(Level.SEVERE, null, ex);
  47. }
  48.  
  49. }
  50.  
  51. @Override
  52. public String getServletInfo() {
  53. return "Short description";
  54. }// </editor-fold>
  55.  
  56. success: function (data) {
  57. alert(data); //I want to get 2 servlet values and alert them here. How can I do that?
  58. }
Add Comment
Please, Sign In to add comment