Advertisement
Guest User

Untitled

a guest
Jul 15th, 2016
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.96 KB | None | 0 0
  1. package Servlet;
  2.  
  3. import muffon.constructor.Profile;
  4. import muffon.dao.ProfileDAO;
  5.  
  6. import javax.servlet.RequestDispatcher;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.annotation.WebServlet;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import javax.servlet.http.HttpSession;
  13. import java.io.IOException;
  14. import java.util.HashMap;
  15. import java.util.ResourceBundle;
  16.  
  17. @WebServlet(name = "Servlet", urlPatterns = {"/Servlet"})
  18. public class Servlet extends HttpServlet{
  19.  
  20.     private static final String INSERT = "/profile.jsp";
  21.     private static final String EDIT = "/edit.jsp";
  22.     private static final String PROFILE_RECORD = "/listProfile.jsp";
  23.     private static final String ERROR = "/error.jsp";
  24.     private String userName;
  25.     private String nickName;
  26.     private String userMail;
  27.     private String password;
  28.     private HashMap<Object, Object> errors = new HashMap<>();
  29.  
  30.     protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  31.         response.setContentType("text/html; charset=UTF-8");
  32.         request.setCharacterEncoding("UTF-8");
  33.         super.service(request, response);
  34.     }
  35.     public boolean validate(){
  36.         boolean noError = true;
  37.         if (userName.equals("")){
  38.             errors.put("userName","Пожалуйста заполните ваше имя");
  39.             noError = false;}
  40.         if (nickName.equals("")){
  41.             errors.put("nickName","Пожалуйста заполните ваш ник");
  42.             noError = false;}
  43.         if (userMail.equals("")||(userMail.indexOf('@') == -1)) {
  44.             errors.put("userMail","Пожалуйста заполните ваш адрес email");
  45.             noError = false;}
  46.         if (password.equals("")) {
  47.             errors.put("password","Пожалуйста заполните пароль");
  48.             noError = false;
  49.         } return noError;
  50.     }
  51.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  52.         String redirect="";
  53.         String profileId = request.getParameter("id");
  54.         String action = request.getParameter("action");
  55.         try {
  56.             HttpSession session = request.getSession();
  57.             ProfileDAO instance = (ProfileDAO) session.getAttribute("DAO");
  58.             Profile profile = new Profile();
  59.             if(!((profileId)== null) && action.equalsIgnoreCase("insert")){
  60.                 int id = Integer.parseInt(profileId);
  61.                 userName = request.getParameter("userName");
  62.                 nickName = request.getParameter("nickName");
  63.                 userMail = request.getParameter("userMail");
  64.                 password = request.getParameter("password");
  65.                     if(validate()){
  66.                         profile.setId(id);
  67.                         profile.setUserName(userName);
  68.                         profile.setNickName(nickName);
  69.                         profile.setUserMail(userMail);
  70.                         profile.setPassword(password);
  71.                         instance.insertProfile(profile);
  72.                         request.setAttribute("profile", instance.selectAllProfiles());
  73.                         redirect = PROFILE_RECORD;
  74.                     } else {
  75.                         session.setAttribute("messageError", errors);
  76.                         redirect = PROFILE_RECORD;
  77.                     }
  78.             } else if (action.equalsIgnoreCase("delete")){
  79.                 int profileDelete = Integer.parseInt(profileId);
  80.                 instance.deleteProfileId(profileDelete);
  81.                     redirect = PROFILE_RECORD;
  82.                 request.setAttribute("profile", instance.selectAllProfiles());
  83.             } else if (action.equalsIgnoreCase("editForm")){
  84.                     redirect = EDIT;
  85.             } else if (action.equalsIgnoreCase("edit")){
  86.                 String Id = request.getParameter("id");
  87.                 int profileEdit = Integer.parseInt(Id);
  88.                     profile.setId(profileEdit);
  89.                     userName = request.getParameter("userName");
  90.                     nickName = request.getParameter("nickName");
  91.                     userMail = request.getParameter("userMail");
  92.                     password = request.getParameter("password");
  93.                 if(validate()) {
  94.                     profile.setUserName(userName);
  95.                     profile.setNickName(nickName);
  96.                     profile.setUserMail(userMail);
  97.                     profile.setPassword(password);
  98.                     request.setAttribute("profile", profile);
  99.                     instance.updateProfile(profile);
  100.                     redirect = PROFILE_RECORD;
  101.                 }
  102.             } else if (action.equalsIgnoreCase("listProfile")) {
  103.                 redirect = PROFILE_RECORD;
  104.                 request.setAttribute("profile", instance.selectAllProfiles());
  105.             } else if (action.equalsIgnoreCase("error")){
  106.                 redirect = ERROR;
  107.             } else {
  108.                 redirect = INSERT;
  109.             }
  110.         } catch (Exception e){
  111.           e.printStackTrace();
  112.             redirect = ERROR;
  113.         }
  114.         RequestDispatcher rd = request.getRequestDispatcher(redirect);
  115.         rd.forward(request, response);
  116.     }
  117.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  118.         doGet(request, response);
  119.     }
  120. }
  121.  
  122.  
  123. JSP
  124.  
  125. <%@ page import="java.util.HashMap" %>
  126. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  127. <html>
  128. <head>
  129.     <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  130.     <title>Добавление профиля</title>
  131. </head>
  132. <body>
  133. <%
  134.     HashMap<String,String> errors = (HashMap<String, String>) session.getAttribute("errors");
  135. %>
  136. <form method="post" action='Servlet' name="frmAddUser">
  137.     <input type="hidden" name="action" value="insert" />
  138.     <input type="hidden" name="id" value="0">
  139.     <p><b>Заполните форму</b></p>
  140.     <table>
  141.         <tr>
  142.             <td>Имя</td>
  143.             <td><input type="text" name="userName" /><%=errors.get("userName")%></td>
  144.         </tr>
  145.         <tr>
  146.             <td>Ник</td>
  147.             <td><input type="text" name="nickName"/><%=errors.get("nickName")%></td>
  148.         </tr>
  149.         <tr>
  150.             <td>Email</td>
  151.             <td><input type="text" name="userMail"/><%=errors.get("userMail")%></td>
  152.         </tr>
  153.         <tr>
  154.             <td>Пароль</td>
  155.             <td><input type="text" name="password"/><%=errors.get("password")%></td>
  156.         </tr>
  157.         <tr>
  158.             <td></td>
  159.             <td><input type="submit" value="Добавить" /></td>
  160.         </tr>
  161.     </table>
  162. </form>
  163. <p><a href="Servlet?action=listProfile">Просмотреть таблицу</a></p>
  164. </body>
  165. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement