いつもお世話になっております。
プルダウンで項目名を選択後にボタンのクリックで送信し、その項目名に合致する情報を表示するプログラムを作成しております。
送信後のプルダウンを、選択した項目が初期状態になり、他の項目名もプルダウンにて確認できるようにしたいです。
追加したコードで初期値を設定したのですが、これではプルダウンに1件しか表示されないため、初期値を設定しつつ他の項目をプルダウン内に表示したいのですが、
どのように修正、記述をすればよいのかが分からないためご教示いただければ幸いです。
よろしくお願いいたします。
###表示画面
jsp
1<%@ page contentType="text/html; charset=Windows-31J"%> 2<%@ page import="jp.co.keyaki.bean.KuyakushoBean"%> 3<%@ page import="java.util.List"%> 4<%@ page import="java.util.ArrayList"%> 5<% @SuppressWarnings("unchecked") 6List<KuyakushoBean> list = (List<KuyakushoBean>) request.getAttribute("LIST"); 7%> 8<% @SuppressWarnings("unchecked") 9List<KuyakushoBean> name = (List<KuyakushoBean>) request.getAttribute("ku"); 10%> 11<html> 12<head> 13<meta charset="UTF-8"> 14<title>23区区役所一覧</title> 15</head> 16<body> 17 <div align="center"> 18 23区区役所一覧 19 <form action="list" method="post"> 20 <select name="ku"> 21 <%if (list != null ) { %> 22 <% for (int i = 0; i < list.size(); i++) { %> 23 <%KuyakushoBean KuyakushoBean= (KuyakushoBean)list.get(i);%> 24 <option value=<%=KuyakushoBean.getid() %>><%=KuyakushoBean.getku() %></option> 25 <% } %> 26 <% } %> 27 //追加した部分 28 <%if (name != null ) { %> 29 <% for (int i = 0; i < name.size(); i++) { %> 30 <%KuyakushoBean KuyakushoBean= (KuyakushoBean)name.get(i);%> 31 <option value=<%=KuyakushoBean.getid() %>selected><%=KuyakushoBean.getku() %></option> 32 <% } %> 33 <% } %> 34 </select> <input type="submit" value="送信"> 35 </form> 36 <table border="1"> 37 <tr> 38 <th>番号</th> 39 <th>区</th> 40 <th>郵便番号</th> 41 <th>住所</th> 42 <th>電話番号</th> 43 </tr> 44 <%if (list != null ) { %> 45 <% for (int j = 0; j < list.size(); j++) { %> 46 <%KuyakushoBean KuyakushoBean= (KuyakushoBean)list.get(j);%> 47 <tr> 48 <td><%=KuyakushoBean.getid()%></td> 49 <td><%=KuyakushoBean.getku()%></td> 50 <td><%=KuyakushoBean.getzip()%></td> 51 <td><%=KuyakushoBean.getaddress()%></td> 52 <td><%=KuyakushoBean.gettel()%></td> 53 </tr> 54 <% } %> 55 <% } %> 56 <%if(name != null) {%> 57 <% for (int i = 0; i < name.size(); i++) { %> 58 <%KuyakushoBean KuyakushoBean= (KuyakushoBean)name.get(i);%> 59 <tr> 60 <td><%=KuyakushoBean.getid()%></td> 61 <td><%=KuyakushoBean.getku()%></td> 62 <td><%=KuyakushoBean.getzip()%></td> 63 <td><%=KuyakushoBean.getaddress()%></td> 64 <td><%=KuyakushoBean.gettel()%></td> 65 </tr> 66 <% } %> 67 <% } %> 68 </table> 69 <input type="hidden" name="ku2"> 70 </div> 71</body> 72</html>
###サーブレット
java
1package jp.co.keyaki.controller; 2import java.io.IOException; 3import java.util.List; 4import javax.servlet.RequestDispatcher; 5import javax.servlet.ServletContext; 6import javax.servlet.ServletException; 7import javax.servlet.http.HttpServlet; 8import javax.servlet.http.HttpServletRequest; 9import javax.servlet.http.HttpServletResponse; 10import jp.co.keyaki.bean.KuyakushoBean; 11import jp.co.keyaki.service.KuyakushoSearch; 12public class KuyakushoController extends HttpServlet { 13 public void doGet(HttpServletRequest request, HttpServletResponse response) 14 throws ServletException, IOException { 15 String target = null; 16 try { 17 KuyakushoSearch kuyakushoSearch = new KuyakushoSearch(); 18 List<KuyakushoBean> list = kuyakushoSearch.list(); 19 request.setAttribute("LIST", list); 20 target = "/kuyakusho.jsp"; 21 } catch (Exception e) { 22 e.printStackTrace(); 23 target = "/systemError.jsp"; 24 } finally { 25 ServletContext context = this.getServletContext(); 26 RequestDispatcher dispatcher = context.getRequestDispatcher(target); 27 dispatcher.forward(request, response); 28 } 29 } 30 public void doPost(HttpServletRequest request, HttpServletResponse response) 31 throws ServletException, IOException { 32 String target = null; 33 try{ 34 int id = Integer.parseInt(request.getParameter("ku")); 35 KuyakushoSearch kuyakushoSearch = new KuyakushoSearch(); 36 List<KuyakushoBean> name = kuyakushoSearch.name(id); 37 request.setAttribute("ku", name); 38 if(name == null) { 39 request.setAttribute("ku", name); 40 } 41 target = "/kuyakusho.jsp"; 42 } catch (Exception e) { 43 e.printStackTrace(); 44 target = "/systemError.jsp"; 45 } finally { 46 ServletContext context = this.getServletContext(); 47 RequestDispatcher dispatcher = context.getRequestDispatcher(target); 48 dispatcher.forward(request, response); 49 } 50 } 51}
###検索処理
java
1package jp.co.keyaki.service; 2import java.sql.Connection; 3import java.sql.DriverManager; 4import java.sql.PreparedStatement; 5import java.sql.ResultSet; 6import java.sql.SQLException; 7import java.util.ArrayList; 8import java.util.List; 9import org.postgresql.util.PSQLException; 10import jp.co.keyaki.bean.KuyakushoBean; 11public class KuyakushoSearch { 12 public List<KuyakushoBean> list() 13 throws ClassNotFoundException, SQLException { 14 List<KuyakushoBean> list = new ArrayList<>(); 15 Connection connection = null; 16 PreparedStatement preparedStatement = null; 17 ResultSet resultSet = null; 18 KuyakushoBean KuyakushoBean = null; 19 try { 20 Class.forName("org.postgresql.Driver"); 21 connection = DriverManager.getConnection( 22 jdbc:postgresql://【ホスト名】/【データベース名】", "【ユーザ名】", "【パスワード】"); 23 String SQL = "SELECT id, ku, zip, address, tel FROM t_kuyakusho"; 24 preparedStatement = connection.prepareStatement(SQL); 25 ResultSet rs = preparedStatement.executeQuery(); 26 while (rs.next()) { 27 KuyakushoBean = new KuyakushoBean(); 28 KuyakushoBean.setid(rs.getInt("id")); 29 KuyakushoBean.setku(rs.getString("ku")); 30 KuyakushoBean.setzip(rs.getString("zip")); 31 KuyakushoBean.setaddress(rs.getString("address")); 32 KuyakushoBean.settel(rs.getString("tel")); 33 list.add(KuyakushoBean); 34 } 35 } catch (ClassNotFoundException e) { 36 e.printStackTrace(); 37 } catch (PSQLException e) { 38 e.printStackTrace(); 39 } finally { 40 if (resultSet != null) { 41 resultSet.close(); 42 } 43 if (preparedStatement != null) { 44 preparedStatement.close(); 45 } 46 if (connection != null) { 47 connection.close(); 48 } 49 } 50 return list; 51 } 52 public List<KuyakushoBean> name(int id) 53 throws ClassNotFoundException, SQLException { 54 List<KuyakushoBean> name = new ArrayList<>(); 55 Connection connection = null; 56 PreparedStatement preparedStatement = null; 57 ResultSet resultSet = null; 58 KuyakushoBean KuyakushoBean = null; 59 try { 60 Class.forName("org.postgresql.Driver"); 61 connection = DriverManager.getConnection( 62 "jdbc:postgresql://localhost:5432/lessondb", "postgres", "jobsupport"); 63 String SQL = "SELECT id, ku, zip, address, tel FROM t_kuyakusho WHERE id = ?"; 64 preparedStatement = connection.prepareStatement(SQL); 65 preparedStatement.setInt(1, id); 66 ResultSet rs = preparedStatement.executeQuery(); 67 if (rs.next()) { 68 KuyakushoBean = new KuyakushoBean(); 69 KuyakushoBean.setid(rs.getInt("id")); 70 KuyakushoBean.setku(rs.getString("ku")); 71 KuyakushoBean.setzip(rs.getString("zip")); 72 KuyakushoBean.setaddress(rs.getString("address")); 73 KuyakushoBean.settel(rs.getString("tel")); 74 name.add(KuyakushoBean); 75 } 76 } catch (ClassNotFoundException e) { 77 e.printStackTrace(); 78 } catch (PSQLException e) { 79 e.printStackTrace(); 80 } finally { 81 if (resultSet != null) { 82 resultSet.close(); 83 } 84 if (preparedStatement != null) { 85 preparedStatement.close(); 86 } 87 if (connection != null) { 88 connection.close(); 89 } 90 } 91 return name; 92 } 93}
###Bean
java
1package jp.co.keyaki.bean; 2public class KuyakushoBean { 3 private int id; 4 private String ku; 5 private String zip; 6 private String address; 7 private String tel; 8 public void setid(int id) { 9 this.id = id; 10 } 11 public int getid() { 12 return id; 13 } 14 public void setku(String ku) { 15 this.ku = ku; 16 } 17 public String getku() { 18 return ku; 19 } 20 public void setzip(String zip) { 21 this.zip = zip; 22 } 23 public String getzip() { 24 return zip; 25 } 26 public void setaddress(String address) { 27 this.address = address; 28 } 29 public String getaddress() { 30 return address; 31 } 32public void settel(String tel) { 33 this.tel = tel; 34} 35public String gettel() { 36 return tel; 37} 38}
あなたの回答
tips
プレビュー