Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Servlet;
- import muffon.constructor.Profile;
- import muffon.dao.ProfileDAO;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.ResourceBundle;
- @WebServlet(name = "Servlet", urlPatterns = {"/Servlet"})
- public class Servlet extends HttpServlet{
- private static final String INSERT = "/profile.jsp";
- private static final String EDIT = "/edit.jsp";
- private static final String PROFILE_RECORD = "/listProfile.jsp";
- private static final String ERROR = "/error.jsp";
- private String userName;
- private String nickName;
- private String userMail;
- private String password;
- private HashMap<Object, Object> errors = new HashMap<>();
- protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("text/html; charset=UTF-8");
- request.setCharacterEncoding("UTF-8");
- super.service(request, response);
- }
- public boolean validate(){
- boolean noError = true;
- if (userName.equals("")){
- errors.put("userName","Пожалуйста заполните ваше имя");
- noError = false;}
- if (nickName.equals("")){
- errors.put("nickName","Пожалуйста заполните ваш ник");
- noError = false;}
- if (userMail.equals("")||(userMail.indexOf('@') == -1)) {
- errors.put("userMail","Пожалуйста заполните ваш адрес email");
- noError = false;}
- if (password.equals("")) {
- errors.put("password","Пожалуйста заполните пароль");
- noError = false;
- } return noError;
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- String redirect="";
- String profileId = request.getParameter("id");
- String action = request.getParameter("action");
- try {
- HttpSession session = request.getSession();
- ProfileDAO instance = (ProfileDAO) session.getAttribute("DAO");
- Profile profile = new Profile();
- if(!((profileId)== null) && action.equalsIgnoreCase("insert")){
- int id = Integer.parseInt(profileId);
- userName = request.getParameter("userName");
- nickName = request.getParameter("nickName");
- userMail = request.getParameter("userMail");
- password = request.getParameter("password");
- if(validate()){
- profile.setId(id);
- profile.setUserName(userName);
- profile.setNickName(nickName);
- profile.setUserMail(userMail);
- profile.setPassword(password);
- instance.insertProfile(profile);
- request.setAttribute("profile", instance.selectAllProfiles());
- redirect = PROFILE_RECORD;
- } else {
- session.setAttribute("messageError", errors);
- redirect = PROFILE_RECORD;
- }
- } else if (action.equalsIgnoreCase("delete")){
- int profileDelete = Integer.parseInt(profileId);
- instance.deleteProfileId(profileDelete);
- redirect = PROFILE_RECORD;
- request.setAttribute("profile", instance.selectAllProfiles());
- } else if (action.equalsIgnoreCase("editForm")){
- redirect = EDIT;
- } else if (action.equalsIgnoreCase("edit")){
- String Id = request.getParameter("id");
- int profileEdit = Integer.parseInt(Id);
- profile.setId(profileEdit);
- userName = request.getParameter("userName");
- nickName = request.getParameter("nickName");
- userMail = request.getParameter("userMail");
- password = request.getParameter("password");
- if(validate()) {
- profile.setUserName(userName);
- profile.setNickName(nickName);
- profile.setUserMail(userMail);
- profile.setPassword(password);
- request.setAttribute("profile", profile);
- instance.updateProfile(profile);
- redirect = PROFILE_RECORD;
- }
- } else if (action.equalsIgnoreCase("listProfile")) {
- redirect = PROFILE_RECORD;
- request.setAttribute("profile", instance.selectAllProfiles());
- } else if (action.equalsIgnoreCase("error")){
- redirect = ERROR;
- } else {
- redirect = INSERT;
- }
- } catch (Exception e){
- e.printStackTrace();
- redirect = ERROR;
- }
- RequestDispatcher rd = request.getRequestDispatcher(redirect);
- rd.forward(request, response);
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doGet(request, response);
- }
- }
- JSP
- <%@ page import="java.util.HashMap" %>
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <title>Добавление профиля</title>
- </head>
- <body>
- <%
- HashMap<String,String> errors = (HashMap<String, String>) session.getAttribute("errors");
- %>
- <form method="post" action='Servlet' name="frmAddUser">
- <input type="hidden" name="action" value="insert" />
- <input type="hidden" name="id" value="0">
- <p><b>Заполните форму</b></p>
- <table>
- <tr>
- <td>Имя</td>
- <td><input type="text" name="userName" /><%=errors.get("userName")%></td>
- </tr>
- <tr>
- <td>Ник</td>
- <td><input type="text" name="nickName"/><%=errors.get("nickName")%></td>
- </tr>
- <tr>
- <td>Email</td>
- <td><input type="text" name="userMail"/><%=errors.get("userMail")%></td>
- </tr>
- <tr>
- <td>Пароль</td>
- <td><input type="text" name="password"/><%=errors.get("password")%></td>
- </tr>
- <tr>
- <td></td>
- <td><input type="submit" value="Добавить" /></td>
- </tr>
- </table>
- </form>
- <p><a href="Servlet?action=listProfile">Просмотреть таблицу</a></p>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement