###質問の前提
Servlet+JSPを用いてアプリを開発中です。
環境構築まで終えて、いよいよログイン機能を作ろうかな~という矢先でした。
ディレクトリ構成は以下の画像のような形です。
###エラーの内容
JSPからサーブレット側にフォワードされないのです。
該当のソースコード
Servlet
1package tool; 2 3import java.io.IOException; 4import java.io.PrintWriter; 5 6import javax.servlet.ServletException; 7import javax.servlet.annotation.WebServlet; 8import javax.servlet.http.HttpServlet; 9import javax.servlet.http.HttpServletRequest; 10import javax.servlet.http.HttpServletResponse; 11 12import util.StringUtil; 13 14/** 15 * Servlet implementation class FrontController 16 * MVCモデルにおける唯一の入り口で、ここから全ての処理を割り振る 17 */ 18 19//hogehoge.actionと名付けられたようなURLを開くと、FrontControllerが実行される。 20@WebServlet(urlPatterns={"*.action"}) 21public class FrontController extends HttpServlet { 22 23 private static final long serialVersionUID = 1L; 24 25 private String classPath = StringUtil.PACKAGE; 26 27 /** 28 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 29 */ 30 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 31 ServletException, IOException { 32 33 PrintWriter out = response.getWriter(); 34 35 try{ 36 37 /** 38 * リクエストのURLに含まれるサーブレットのパスを取得する 39 * -> substringメソッドにより先頭の1文字を削除する 40 * 41 * URLを /hoge/hogehoge ->hoge.hogehoge の形に加工する処理を行う 42 * 43 */ 44 45 String path = request.getServletPath().substring(1); 46 String className = classPath + path.replace(StringUtil.BEFORE_REPLACE_a,StringUtil.AFTER_REPLACE_A) 47 .replace(StringUtil.SLASH,StringUtil.DOT); 48 49 System.out.println(className); 50 51 /** 52 * forNameメソッドにより、指定した名前のClassオブジェクトを返す 53 * newInstanceメソッドにより、新しいインスタンスを作成する 54 */ 55 Action action = (Action)Class.forName(className).newInstance(); 56 57 //作成したクラスオブジェクトのexcuteメソッドを実行する 58 String url=action.execute(request, response); 59 60 System.out.println(url); 61 62 //戻り値によりフォワード 63 request.getRequestDispatcher(url).forward(request,response); 64 65 System.out.println("forwardされていない"); 66 67 }catch(Exception error){ 68 69 System.out.println("通った"); 70 71 error.printStackTrace(out); 72 73 } 74 } 75 76 77 /** 78 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 79 * Getメソッドを用いていようと、内部的にはすべてPOSTとして処理をする。 80 */ 81 protected void doGet(HttpServletRequest request, HttpServletResponse response) 82 throws ServletException, IOException { 83 doPost(request,response); 84 } 85 86 87} 88
Java
1 2package action; 3 4import javax.servlet.http.HttpServletRequest; 5import javax.servlet.http.HttpServletResponse; 6import javax.servlet.http.HttpSession; 7 8import bean.Menber; 9import dao.MenberDAO; 10import tool.Action; 11 12public class LoginAction extends Action{ 13 14 public String execute( 15 HttpServletRequest request, HttpServletResponse response) throws Exception{ 16 17 HttpSession session=request.getSession(); 18 19 String userId = request.getParameter("userId"); 20 String password = request.getParameter("password"); 21 MenberDAO dao = new MenberDAO(); 22 Menber menber = dao.search(userId,password); 23 24 if(menber!=null){ 25 26 session.setAttribute("userId", userId); 27 return "/view/menu.jsp"; 28 } 29 30 return "/view/error.jsp"; 31 32 } 33 34} 35
JSP
1<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4<head> 5 <title>Top</title> 6 7 <link href='../stylesheet/login.css' rel='stylesheet' type='text/css' /> 8 <script type="text/javascript" src="../stylesheet/main.js" ></script> 9 <meta charset="UTF-8" /> 10 <meta name="viewport" content="width=device-width, initial-scale=1.0" /> 11 12</head> 13 14<body> 15<div class="container"> 16<div id="login-form"> 17 <div id="box"> 18 19 <div> 20 <label for="form-userid">userID</label> 21 <input type="text" name="userId" id="form-userid" placeholder="userID"/> 22 <p class="note">Please your ID.</p> 23 </div> 24 25 <div> 26 <label for="form-password">PassWord</label> 27 <input type="password" name="password" id="form-password" placeholder="PassWord" /> 28 <p class="note">Please your Pass.</p> 29 </div> 30 31 <div class="btn"> 32 <form action="../Login.action" method="post"> 33 <input type="submit" name="submit" value="Login"/> 34 </form> 35 </div> 36 37</div> 38 </div> 39 </div> 40 41</body> 42</html> 43
試したこと
systemoutでどのようなコードの通り方をしているのか調べたところ、
catchまでは入っていないようなので、forwardされないままサーブレット内で
処理が終了してしまっているようなのです。
が、どうしてforwardされないかが分かりません。
初歩的で恥ずかしい質問内容なのですが、どうかご教授お願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/06 14:28
2018/11/06 14:29
2018/11/06 15:06
2018/11/07 01:44
2018/11/07 04:11
2018/11/07 12:02
2018/11/07 14:31