doPostメソッドで、新規登録で入力した値がnullでないか?
またはその値が、すでにデータベースにないか?
そのどちらでもない場合に、新規登録を実行して、データベースに登録したいです。
どのように書けば、if else がしっかり組み立てられるのでしょうか?
java
1package servlet; 2 3import java.io.IOException; 4 5import java.util.List; 6import javax.servlet.RequestDispatcher; 7import javax.servlet.ServletException; 8import javax.servlet.annotation.WebServlet; 9import javax.servlet.http.HttpServlet; 10import javax.servlet.http.HttpServletRequest; 11import javax.servlet.http.HttpServletResponse; 12import javax.servlet.http.HttpSession; 13 14import model.Mutter; 15import model.User; 16import model.SetLogic; 17import model.LoginLogic; 18 19/** 20 * Servlet implementation class Set 21 */ 22@WebServlet("/Set") 23public class Set extends HttpServlet { 24 private static final long serialVersionUID = 1L; 25 26 /** 27 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 28 */ 29 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 30 31 32 RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/set.jsp"); 33 dispatcher.forward(request, response); 34 35 } 36 37 /** 38 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 39 */ 40 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 41 42 request.setCharacterEncoding("UTF-8"); 43 String name=request.getParameter("name"); 44 String pass=request.getParameter("pass"); 45 46 47 User user=new User(name,pass); 48 49 if(name.length()==0 || pass.length()==0){ 50 request.setAttribute("newerrorMsg", "未入力があります。"); 51 52 RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/set.jsp"); 53 dispatcher.forward(request, response); 54 55 } 56 57 //LoginLogicを使ってmutterListを取得 58 LoginLogic loginLogic=new LoginLogic(); 59 60 List<Mutter>mutterList=loginLogic.execute(user); 61 62 63 if(mutterList.size()!=0){ 64 request.setAttribute("setErrorMsg","すでに登録されています。"); 65 66 RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/set.jsp"); 67 dispatcher.forward(request, response); 68 } 69 //mutter1とする 70 for(Mutter mutter1:mutterList){ 71 if(mutter1.getUserName().length()!=0||mutter1.getUserPass().length()!=0){ 72 request.setAttribute("setErrorMsg", "すでに登録されています。"); 73 74 RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/set.jsp"); 75 dispatcher.forward(request, response); 76 77 }else{ 78 HttpSession session=request.getSession(); 79 session.setAttribute("loginUser",user); 80 81 User loginUser=(User)session.getAttribute("loginUser"); 82 83 Mutter mutter=new Mutter(loginUser.getName(),loginUser.getPass()); 84 SetLogic setLogic=new SetLogic(); 85 86 try{ 87 setLogic.execute(mutter); 88 }catch(ClassNotFoundException e){ 89 e.printStackTrace(); 90 } 91 92 RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/setResult.jsp"); 93 dispatcher.forward(request, response); 94 } 95 } 96 } 97} 98 99
回答2件
あなたの回答
tips
プレビュー