Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // JSON SCRIPT:
- $('#reg_btn1').on('click', function(e) {
- e.preventDefault();
- var objectArray = $("#register-form :input").serializeArray(); // Make an array of input vals
- var data, objectData = {};
- action = 'new', name = null;
- for (var i in objectArray)
- {
- name = objectArray[i].value;
- objectData[objectArray[i].name] = objectArray[i].value;
- };
- // Delete stuff that's not needed for processing
- delete objectData.confirm_password;
- delete objectData.cbTos;
- data = { 'action' : action, 'data' : objectData };
- data = JSON.stringify(data);
- data = "reg=" + data;
- $.ajax({
- type: "POST",
- url: "<%=request.getContextPath()%>/RegisterServlet",
- data: data,
- dataType: "json"
- }).
- success(function(res){
- if(res.url != null)
- {
- alert(res.url);
- window.location.href = res.url;
- }
- }).
- error(function(res){
- });
- });
- });
- // SERVLET
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- System.out.println("doso na REGSERVLET - POST");
- response.setHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store");
- getServletContext().removeAttribute("data");
- WebShop ws = (WebShop)this.getServletContext().getAttribute("webShop");
- String jsonData = request.getParameter("reg");
- System.out.println("jsonData: " + jsonData);
- ObjectMapper mapper = new ObjectMapper();
- JsonNode rootNode = mapper.readTree(jsonData);
- JsonNode actionNode = rootNode.path("action");
- System.out.println(actionNode.toString());
- if (actionNode.toString().replace("\"", "").equals("new"))
- {
- JsonNode dataNode = rootNode.path("data");
- User u = mapper.readValue(dataNode.toString(), User.class);
- u.setRole(UserRole.USER.getValue()); // We assume that reg'd user is a normal user..it can be later be promoted by an admin to a mod/admin.
- System.out.println("Username: " + u.getUsername());
- System.out.println("USERS: " + ws.getUsers().toString());
- if (u.getUsername().isEmpty() || u.getEmail().isEmpty() || u.getLastName().isEmpty() || u.getName().isEmpty() || u.getPassword().isEmpty() || u.getPhone().isEmpty())
- {
- outputMessage(request, response, new Message("All fields must contain data! Please try again.", "danger"));
- return;
- }
- if (ws.getUsers().containsKey(u.getUsername()))
- {
- outputMessage(request, response, new Message("Account with that username already exists, please choose another username.", "danger"));
- return;
- }
- ws.addUser(u);
- System.out.println("user: " + u.toString());
- // RequestDispatcher disp = request.getRequestDispatcher("views/main.jsp");
- // disp.forward(request, response);
- }
- else if (actionNode.toString().replace("\"", "").equals("edit"))
- {
- // TODO: fixme
- // For editing user
- }
- else if (actionNode.toString().replace("\"", "").equals("delete"))
- {
- // TODO: fixme
- // Deleting a user
- }
- // Make a new JSON object for a response, we can pass here anything we want. Feedback message, redirect URL, apple pie...
- response.setContentType("application/json");
- JSONObject obj = new JSONObject();
- try {
- obj.put("message", "User successfully.");
- obj.put("url", "test.jsp");
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- PrintWriter pw = response.getWriter();
- pw.print(obj);
- System.out.println("Poslao response: " + pw.toString());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement