Advertisement
Guest User

Untitled

a guest
Sep 24th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. @WebServlet(name = "registration servlet", urlPatterns = "/register")
  2. public class Register extends HttpServlet{
  3.  
  4. PrintWriter output;
  5. private final static int EMPTY = 0;
  6. private static int byteContainer = 0;
  7. private static int lines = 0;
  8. private static String charContainer = "";
  9.  
  10. @Override
  11. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  12.  
  13. output = resp.getWriter();
  14. output.print("The Do Get METHOD");
  15.  
  16. }
  17.  
  18. @Override
  19. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  20.  
  21. output = resp.getWriter();
  22.  
  23. ServletInputStream servletInputStream = req.getInputStream();
  24.  
  25. if(servletInputStream.isReady()){
  26.  
  27. byte [] bytes = new byte[4096];
  28.  
  29. while((byteContainer = servletInputStream.read()) != -1){
  30.  
  31. bytes [lines] = (byte)byteContainer;
  32. lines++;
  33.  
  34. if((servletInputStream.read()) == -1){
  35.  
  36. char [] chars = new char[4096];
  37.  
  38. for(int i = 0, k = 0 ; i < lines; i++, k++){
  39.  
  40. chars[i] = (char) bytes [k];
  41. charContainer += chars[i];
  42. }
  43. }
  44. }
  45. }
  46.  
  47. lines = EMPTY;
  48. output.print(charContainer);
  49. servletInputStream.close();
  50.  
  51. Запрос Ajax:
  52.  
  53. $("#register-button").click(function () {
  54.  
  55. var user = {
  56.  
  57. firstName:$("#first-name").val(),
  58. lastName:$("#last-name").val(),
  59. nickName:$("#nick_name").val(),
  60. email:$("#email_").val(),
  61. password:$("#password_").val(),
  62. customerType:$("#cust-type").val()
  63. }
  64.  
  65. console.log(user);
  66.  
  67. var JSONString = JSON.stringify(user);
  68.  
  69. var JSONObject = JSON.parse(JSONString);
  70.  
  71. console.log(JSONObject);
  72.  
  73. var url = "http://localhost:8080/CouponProject/register";
  74.  
  75. $.ajax({
  76.  
  77. url:url,
  78. method:"post",
  79. data:JSONObject,
  80. contentType:"application/json",
  81. error:function (message) {
  82. console.log(message+" In the error function!")
  83. },
  84. success:function (data) {
  85.  
  86. console.log(data);
  87.  
  88. },
  89. headers:
  90. {
  91. "Accept":"application/json",
  92. "Accept-Language":"en",
  93. "Cache-Control":"max-age=3600"
  94. }
  95. });
  96.  
  97. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement