登録画面(entry.jsp)で、DepartServletからデータを取得し表示させ、
登録画面で、入力した情報を登録ボタンを押して、
一覧画面(employee.jsp)で、登録した情報を表示させたいのですが、
入力して登録ボタンを押しても遷移されず、登録もできません。
よろしくお願いします。
【登録画面(entry.jsp)】
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>社員登録</h2> <form action="EntryServlet"> <table border="1"> <tr bgcolor="#afeeee"> <td>社員コード</td> <td><input type="text" name="entry_id" maxlength='7' style="ime-mode:disabled"></td> </tr> <tr bgcolor="#afeeee"> <td>氏名(姓)</td> <td><input type="text" name="entry_name1" maxlength='20'></td> <td>氏名(名)</td> <td><input type="text" name="entry_name2" maxlength='20'></td> </tr> <tr bgcolor="#afeeee"> <td>氏名カナ(姓)</td> <td><input type="text" name="entry_name_kana1" maxlength='100'></td> <td>氏名カナ(名)</td> <td><input type="text" name="entry_name_name2" maxlength='100'></td> </tr> <tr > <td bgcolor="#afeeee">性別</td> <td><input type="radio" name="sex" value="1" id="man" checked><label for="man">男</label></td> <td><input type="radio" name="sex" value="2" id="woman"><label for="woman">女</label></td> <td><input type="radio" name="sex" value="3" id="other"><label for="other">その他</label></td> </tr> <tr> <td bgcolor="#afeeee">役職名</td> <td><select name="entry_class"> <option value="部長">部長 <option value="課長">課長 <option value="主任">主任 <option value="担当">担当 <option value="アシスタント">アシスタント </select></td> </tr> <tr> <td bgcolor="#afeeee">所属部署名</td> <td><select name="entry_depart_name"> <c:forEach var="item" items="${list1}"> <option value="${item.depart_id}">${item.depart_name} </c:forEach> </select></td> </tr> </table> <input type="submit" name="registration" value="登録"> <input type="button" name="back" value="戻る" onclick="location.href='KintaikanrisystemServlet'"> </form> </body> </html>
【DepartServlet】
package
1 2import java.io.IOException; 3import java.sql.Connection; 4import java.sql.ResultSet; 5import java.sql.SQLException; 6import java.sql.Statement; 7import java.util.ArrayList; 8import java.util.List; 9 10import javax.servlet.ServletException; 11import javax.servlet.http.HttpServlet; 12import javax.servlet.http.HttpServletRequest; 13import javax.servlet.http.HttpServletResponse; 14 15/** 16 * Servlet implementation class Depart 17 */ 18public class DepartServlet extends HttpServlet { 19 private static final long serialVersionUID = 1L; 20 21 /** 22 * @see HttpServlet#HttpServlet() 23 */ 24 public DepartServlet() { 25 super(); 26 // TODO Auto-generated constructor stub 27 } 28 29 /** 30 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 31 */ 32 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 33 34 String sql="select * from depart"; 35 //String sql="select depart_id,depart_name from depart"; 36 37 Connection con =null; 38 Statement stmt=null; 39 40 try{ 41 con=DBManager.getConnection(); 42 stmt=con.createStatement(); 43 ResultSet rs =stmt.executeQuery(sql); 44 45 List<Tableclass> list1=new ArrayList<Tableclass>(); 46 47 while(rs.next()){ 48 Tableclass tableclass=new Tableclass(); 49 50 tableclass.setDepart_id(rs.getString("depart_id")); 51 tableclass.setDepart_name(rs.getString("depart_name")); 52 53 54 list1.add(tableclass); 55 } 56 57 request.setAttribute("list1",list1); 58 59 }catch(SQLException e){ 60 throw new ServletException(e); 61 }finally{ 62 if(stmt != null){ 63 try{stmt.close();}catch(SQLException ignore){} 64 }if(stmt != null){ 65 try{con.close();}catch(SQLException ignore){} 66 } 67 } 68 request.getRequestDispatcher("/entry.jsp").forward(request,response); 69 70 71 } 72 73 74 /** 75 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 76 */ 77 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 78 // TODO Auto-generated method stub 79 doGet(request, response); 80 } 81 82}
【EntryServlet】
package kadai; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.GregorianCalendar; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class EntryServlet */ public class EntryServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public EntryServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html:charset=UTF-8"); Connection con =null; PreparedStatement stmt=null; String message1=null; String message2=null; int a=0; int b=0; request.setCharacterEncoding("UTF-8"); try{ con=DBManager.getConnection(); String s_id=request.getParameter("entry_id"); String l_name=request.getParameter("entry_name1"); String f_name=request.getParameter("entry_name2"); String l_name_kana=request.getParameter("entry_name_kana1"); String f_name_kana=request.getParameter("entry_name_kana2"); String sex1=request.getParameter("sex"); int sex=Integer.parseInt(sex1); String entry_class=request.getParameter("entry_class"); String depart_id=request.getParameter("entry_depart_name"); String boss=null; Calendar cd=new GregorianCalendar(); SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd"); String sd1=sd.format(cd.getTime()); int retired=0; if(s_id.length()<7){ message1="社員コードを半角で7桁入力してください。"; a=1; request.setAttribute("message",message1); } if(l_name==null || f_name==null || l_name_kana==null || f_name_kana==null || l_name=="" || f_name=="" || l_name_kana=="" || f_name_kana=="") { message2="氏名・氏名カナを入力してください。"; b=1; request.setAttribute("message2",message2); } if(a==0 && b==0){ String sql="insert into employee values(?, ?, ? ,? ,? ,? ,? ,? ,?, ? ,?)"; stmt=con.prepareStatement(sql); stmt.setString(1, s_id); stmt.setString(2, l_name); stmt.setString(3, f_name); stmt.setString(4, l_name_kana); stmt.setString(5, f_name_kana); stmt.setInt(6, sex); stmt.setString(7, entry_class); stmt.setString(8, depart_id); stmt.setInt(11, retired); } }catch(SQLException e){ throw new ServletException(e); }finally{ if(stmt != null){ try{stmt.close();}catch(SQLException ignore){} }if(stmt != null){ try{con.close();}catch(SQLException ignore){} } }if(a==0 && b==0){request.getRequestDispatcher("KintaikanrisystemServlet").forward(request,response);} else if(a==1 || b==1){request.getRequestDispatcher("/entry.jsp").forward(request,response);} } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
【一覧画面(employee.jsp)】
<%@ page contentType="text/html; charset=UTF-8" %> <%@ page import="java.util.List,kadai.Tableclass" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>社員一覧</title> </head> <body> <h2>社員一覧</h2> <table border="1"> <!-- border 外枠線の幅 --> <tr bgcolor="#afeeee"> <!-- th 表の横一行 --> <th></th> <!-- th 見出し --> <th>社員コード</th> <th>氏名</th> <th>氏名カナ</th> <th>性別</th> <th>役職名</th> <th>所属部署名</th> <th>在職情報</th> <!-- c:forEach 繰り返し処理 --> <c:forEach var="item" items="${list}" varStatus="num"> <!-- 「var」 itemから取り出した要素を格納する変数名 --> <!-- 「items」 ループする配列(list) --> <!-- 「varStatus」 現在のループの状態を表すステータス変数 --> <tr> <td><input type="radio" name="radio" value="${num.index}"></td> <td>${item.s_id}</td> <td>${item.name }</td> <td>${item.name_kana }</td> <td>${item.sex }</td> <td>${item.clas }</td> <td>${item.depart_name }</td> <td>${item.retired }</td> </tr> </c:forEach> </table> <!-- ボタン生成 --> <input type="submit" value="削除" onclick="delete_test();"> <input type="button" value="登録" onclick="location.href='DepartServlet'"> <input type="submit" value="更新" > <input type="button" value="戻る" onclick="location.href='kintaikanrisystem.jsp'"> </body> </html>
回答1件
あなたの回答
tips
プレビュー