ServletとJSPを利用してログインフォームを作成しております。
気になったことがあるので、質問させてください。
【処理の流れ】
1
ログイン画面(Login.jsp)でログイン情報を入力
2
LoginServlet(@WebServlet("/Login"))に処理を委譲し、成功した場合はメンバーページ(Memeber.jsp)へフォワード。
失敗した場合はログイン画面(Login.jsp)にフォワード。
【結果】
ログイン画面のURLが「https://localhost8080/Pot/jsp/Login.jsp」
となっています。
ログイン成功、失敗共に画面遷移は出来ているのですが
どちらのURLも「https://localhost8080/Pot/Login」となってしまいます。
【気になっていること】
1つ目は
ログイン画面は「https://localhost8080/Pot/jsp/Login.jsp」 で失敗した場合は再度
同じURLである「https://localhost8080/Pot/jsp/Login.jsp」
に「ログインに失敗しました」のメッセージを付け加えて画面を戻したいのですが
現状URLが「https://localhost8080/Pot/Login」 となっている。
成功時もURLがサーブレットの名前になってしまっています。
2つ目は
ログイン画面の場合に、そもそもURLが「https://~xxx.jsp」となっているのが不自然に思う
イメージとして「https:// ~ /login」みたいなURLがログイン画面にふさわしいのではないかと思うのですが、
一般的にどういった感じなのかご教授頂ければ幸いです。
また、それを実現するにはどうすればよいでしょうか?
よろしくお願いします。
Java
1package Servlet.login; 2 3import java.io.IOException; 4 5import javax.servlet.RequestDispatcher; 6import javax.servlet.ServletException; 7import javax.servlet.annotation.WebServlet; 8import javax.servlet.http.HttpServlet; 9import javax.servlet.http.HttpServletRequest; 10import javax.servlet.http.HttpServletResponse; 11import javax.servlet.http.HttpSession; 12 13import model.AccountBeans; 14import utilty.AccountUtilty; 15 16/** 17 * ログインする際のクラス. 18 * 19 */ 20@WebServlet("/Login") 21public class LoginServlet extends HttpServlet { 22 23 @Override 24 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 25 // TODO 自動生成されたメソッド・スタブ 26 super.doGet(req, resp); 27 } 28 29 @Override 30 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 31 try { 32 //フォームに入力された値でAccountオブジェクトを生成する 33 String id = request.getParameter("id"); 34 String password = request.getParameter("password"); 35 AccountBeans account = AccountUtilty.createAccount(id, password); 36 37 LoginDAO login = new LoginDAO(); 38 boolean isCheck = login.checkAccount(account); 39 if (isCheck) { 40 //成功 41 HttpSession session = request.getSession(); 42 System.out.println(session.getMaxInactiveInterval()); 43 session.setAttribute("account", account); 44 session.setAttribute("isLogin", true); 45 RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/MemberPage.jsp"); 46 dispatcher.forward(request, response); 47 } else { 48 //失敗したらログイン画面に戻す 49 request.setAttribute("errorMessage", "ログインに失敗しました。"); 50 RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/Login.jsp"); 51 dispatcher.forward(request, response); 52 } 53 } catch (Exception e) { 54 e.printStackTrace(); 55 } 56 57 } 58}
JSP
1<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3<!DOCTYPE html> 4 5<html lang="en" dir="ltr"> 6 7<head> 8<meta charset="utf-8"> 9<link rel="stylesheet" href="/Pot/css/Login.css"> 10<link href="https://fonts.googleapis.com/css?family=Teko" 11 rel="stylesheet"> 12<title>ログインフォーム</title> 13</head> 14 15<header> Pot </header> 16 17<body> 18 <h3>ようこそ!</h3> 19 <% 20 if (request.getAttribute("errorMessage") != null) { 21 out.println(request.getAttribute("errorMessage")); 22 } 23 %> 24 <form class="" action="/Pot/Login" method="post"> 25 <p> 26 <input type="text" class="textbox" placeholder="ID" name="id"> 27 </p> 28 <p> 29 <input type="text" class="textbox" placeholder="Password" 30 name="password"> 31 </p> 32 <div> 33 <input type="submit" class="button" id="loginButton" value="ログイン"> 34 </div> 35 </form> 36 <hr width=300em;> 37 38 <p> 39 <button type="button" class="button" id="createButton" 40 onclick="location.href='CreateAccount.jsp'">アカウントを作成する</button> 41 </p> 42 43 <canvas id="waveCanvas"></canvas> 44 <script type="text/javascript" src="LoginJS.js"></script> 45</body> 46 47</html>
JSP
1<%@page import="model.AccountBeans"%> 2<%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4<!DOCTYPE html> 5<html> 6 7<head> 8<meta charset="UTF-8"> 9<title>POT</title> 10</head> 11<header> 12 <ul> 13 <li> 14 <% 15 String name; 16 AccountBeans account = (AccountBeans) session.getAttribute("account"); 17 if (session != null) { 18 %> <%=account.getName()%> <% 19 } 20 %> 21 </li> 22 <li><a href="/Pot/Logout">ログアウト</a></li> 23 </ul> 24</header> 25 26<body> 27 28</body> 29</html>
回答1件
あなたの回答
tips
プレビュー