今回は、「Webシステムのログイン(4)―ログインしてブックマークに遷移する―」です。
前回、直接ブックマークなどで飛んできたときに、ログイン画面へリダイレクトしました。
今回は、ログインが終わったときに、さらに飛んできたURLへ戻るようにしてみます。
■動画はこちら
■動画で使用しているソースコード
ソースコードが多いので、主要なコードのみ載せています。
他に欲しいソースコードがありましたら、youtubeのコメント欄へどうぞ。
サーブレット1(Sv1.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package yurufuwa.prog.sample; import java.io.IOException; import java.util.ArrayList; import jakarta.servlet.RequestDispatcher; import jakarta.servlet.ServletContext; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; public class Sv1 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { //地方別の人口一覧を取得 ChihouKensaku k = new ChihouKensaku(); k.execute(); ArrayList<Chihou> chihouList = k.getChihouList(); //結果をリクエストにセット req.setAttribute("chihou_list", chihouList); //検索結果を表示 ServletContext sc = getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher("/WEB-INF/jsp/chihou.jsp"); rd.forward(req, resp); } catch(Exception e) { //エラーを表示 ServletContext sc = getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher("/WEB-INF/jsp/err.jsp"); rd.forward(req, resp); } } } |
都道府県検索(TodofukenKensaku.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | package yurufuwa.prog.sample; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import javax.naming.InitialContext; import javax.sql.DataSource; public class TodofukenKensaku { private ArrayList<Todofuken> todofukenList = null; public void execute(String chihouCode) throws Exception { Connection conn = null; try { //DBに接続 InitialContext ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql"); conn = ds.getConnection(); //SQLを発行 PreparedStatement pstmt = conn.prepareStatement( "SELECT t.ken_code, t.ken_name, t.chihou_code, c.chihou_name, t.jinkou " + "FROM " + "todofuken t INNER JOIN chihou c " + "ON t.chihou_code = c.chihou_code " + "WHERE t.chihou_code=? " + "ORDER BY t.ken_code" ); pstmt.setString(1, chihouCode); ResultSet rs = pstmt.executeQuery(); todofukenList = new ArrayList<Todofuken>(); //結果を取得 while(rs.next()) { Todofuken t = new Todofuken( rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5) ); todofukenList.add(t); } rs.close(); pstmt.close(); } catch(Exception e) { e.printStackTrace(); throw e; } finally { try { //接続を閉じる conn.close(); } catch(Exception e) { } } } public ArrayList<Todofuken> getTodofukenList() { return todofukenList; } } |
都道府県レコード(Todofuken.jsp)
1 2 3 4 5 6 7 8 9 10 11 | package yurufuwa.prog.sample; public record Todofuken ( String kenCode, String kenName, String chihouCode, String chihouName, String jinkou ) { } |
JSP1(todofuken.jsp)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.ArrayList" %> <%@ page import="yurufuwa.prog.sample.Todofuken" %> <% ArrayList<Todofuken> todofukenList = (ArrayList<Todofuken>)request.getAttribute("todofuken_list"); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>都道府県別人口一覧</title> <style> table, th, td { border-collapse: collapse; border: 1px black solid; min-width: 100px;} .txt_r{ text-align: right; } </style> </head> <body> <h2>都道府県別人口一覧</h2> <% if(todofukenList != null && todofukenList.size() > 0){ %> <table> <tr><th>県コード</th><th>県名</th><th>地方コード</th><th>地方名</th><th>人口</th></tr> <% for(Todofuken t : todofukenList) { %> <tr> <td><%= t.kenCode() %></td> <td><%= t.kenName() %></td> <td><%= t.chihouCode() %></td> <td><%= t.chihouName() %></td> <td class="txt_r"><%= t.jinkou() %></td> </tr> <% } %> </table> <% } else { %> 検索結果はありません。 <% } %> <a href="./sv1">地方別人口一覧に戻る</a> </body> </html> |
サーブレット2(Sv2.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package yurufuwa.prog.sample; import java.io.IOException; import java.util.ArrayList; import jakarta.servlet.RequestDispatcher; import jakarta.servlet.ServletContext; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; public class Sv2 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String chihouCode = req.getParameter("chihou_code"); try { //都道府県別の人口一覧を取得 TodofukenKensaku t = new TodofukenKensaku(); t.execute(chihouCode); ArrayList<Todofuken> todofukenList = t.getTodofukenList(); //結果をリクエストにセット req.setAttribute("todofuken_list", todofukenList); //検索結果を表示 ServletContext sc = getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher("/WEB-INF/jsp/todofuken.jsp"); rd.forward(req, resp); } catch(Exception e) { //エラーを表示 ServletContext sc = getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher("/WEB-INF/jsp/err.jsp"); rd.forward(req, resp); } } } |
地方検索(ChihouKensaku.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | package yurufuwa.prog.sample; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import javax.naming.InitialContext; import javax.sql.DataSource; public class ChihouKensaku { private ArrayList<Chihou> chihouList = null; public void execute() throws Exception { Connection conn = null; try { //DBに接続 InitialContext ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql"); conn = ds.getConnection(); //SQLを発行 PreparedStatement pstmt = conn.prepareStatement( "SELECT " + " t.chihou_code, " + " chihou.chihou_name, " + " t.jinkou " + "FROM " + " chihou " + " INNER JOIN " + " (SELECT chihou_code,sum(jinkou) as jinkou " + " FROM todofuken " + " GROUP BY chihou_code ) t " + " ON chihou.chihou_code = t.chihou_code " + "ORDER BY chihou_code " ); ResultSet rs = pstmt.executeQuery(); chihouList = new ArrayList<Chihou>(); //結果を取得 while(rs.next()) { Chihou c = new Chihou( rs.getString(1), rs.getString(2), rs.getString(3) ); chihouList.add(c); } rs.close(); pstmt.close(); } catch(Exception e) { e.printStackTrace(); throw e; } finally { try { //接続を閉じる conn.close(); } catch(Exception e) { } } } public ArrayList<Chihou> getChihouList() { return chihouList; } } |
地方レコード(Chihou.jsp)
1 2 3 4 5 | package yurufuwa.prog.sample; public record Chihou (String chihouCode, String chihouName, String jinkou) { } |
JSP2(chihou.jsp)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.ArrayList" %> <%@ page import="yurufuwa.prog.sample.Chihou" %> <% ArrayList<Chihou> chihouList = (ArrayList<Chihou>)request.getAttribute("chihou_list"); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>地方別人口一覧</title> <style> table, th, td { border-collapse: collapse; border: 1px black solid; min-width: 100px;} .txt_r{ text-align: right; } </style> </head> <body> <h2>地方別人口一覧</h2> <% if(chihouList != null && chihouList.size() > 0){ %> <table> <tr><th>地方コード</th><th>地方名</th><th>人口</th></tr> <% for(Chihou c : chihouList) { %> <tr> <td><%= c.chihouCode() %></td> <td><a href="./sv2?chihou_code=<%= c.chihouCode() %>"><%= c.chihouName() %></a></td> <td class="txt_r"><%= c.jinkou() %></td> </tr> <% } %> </table> <% } else { %> 検索結果はありません。 <% } %> </body> </html> |
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" id="WebApp_ID" version="5.0"> <display-name>testWeb</display-name> <servlet> <servlet-name>SV1</servlet-name> <servlet-class>yurufuwa.prog.sample.Sv1</servlet-class> </servlet> <servlet> <servlet-name>SV2</servlet-name> <servlet-class>yurufuwa.prog.sample.Sv2</servlet-class> </servlet> <servlet-mapping> <servlet-name>SV1</servlet-name> <url-pattern>/sv1</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>SV2</servlet-name> <url-pattern>/sv2</url-pattern> </servlet-mapping> </web-app> |
context.xml
1 2 3 4 5 6 7 8 9 10 11 12 | <?xml version="1.0" encoding="UTF-8"?> <Context> <Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" maxActive="5" maxIdle="5" initialSize="5" username="yuruku" password="fuwatto" driverClassName="com.mysql.cj.jdbc.Driver" url="jdbc:mysql://localhost/yuruku"/> </Context> |