jspファイル内(SearchSubmition.jsp)で値を入力させ、サーブレット(Search.java)に渡す
↓
サーブレットで受け取って、別のjsp(ResultOutput.jsp)に渡して出力
ということやりたいのですがエラーが出ます。
jspで所得した値をjspに渡して出力はできましたが
間にサーブレットを挟むとうまく動作しません。
Formタグのactionの指定が悪いと思い
./Search
Search.java
などいくつか試してみたのですがダメでした。
なお、サーブレット(Search.java)と出力用のjsp(ResultOutput.jsp)の接続は確認済みです。
ミスのご指摘願います。
SearchSubmition.jsp
java
1<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3<%@ page import="javax.servlet.http.*,javax.servlet.*"%> 4<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 5<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%> 6 7<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 8<html> 9<head> 10<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 11<title>Insert title here</title> 12</head> 13<body> 14 <form method="post" action="./Search"> 15 <div>検索したい名前を入力してください</div> 16 <div><input type="text" name="name" maxlength="20" size="20"></div> 17 <input type="submit" value="検索"> 18 </form> 19</body> 20 21</html> 22
サーブレット(Search.java)
java
1 2import java.io.IOException; 3import java.sql.Connection; 4import java.sql.DriverManager; 5import java.sql.PreparedStatement; 6import java.sql.ResultSet; 7import java.sql.SQLException; 8import java.sql.Statement; 9import java.util.ArrayList; 10import java.util.List; 11 12import javax.servlet.RequestDispatcher; 13import javax.servlet.ServletException; 14import javax.servlet.annotation.WebServlet; 15import javax.servlet.http.HttpServlet; 16import javax.servlet.http.HttpServletRequest; 17import javax.servlet.http.HttpServletResponse; 18 19/** 20 * Servlet implementation class DataBaseServer 21 */ 22 23@WebServlet("/Serch") 24public class Search extends HttpServlet { 25 private static final long serialVersionUID = 1L; 26 27 /** 28 * @see HttpServlet#HttpServlet() 29 */ 30 public Search() { 31 super(); 32 // TODO Auto-generated constructor stub 33 } 34 35 /** 36 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 37 * response) 38 */ 39 protected void doGet(HttpServletRequest request, HttpServletResponse response) 40 throws ServletException, IOException { 41 try { 42 Class.forName("com.mysql.jdbc.Driver"); 43 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/record", "root", "xxxxxx"); 44 45 // データの出力 46 Statement st = conn.createStatement(); 47 ResultSet rs = st.executeQuery("select * from first"); 48 49 String name = request.getParameter("name"); 50 51 while (rs.next()) { 52 53 } 54 55 56 request.setAttribute("data", name); 57 RequestDispatcher dispatcher = request.getRequestDispatcher("/ResultOutput.jsp"); 58 dispatcher.forward(request, response); 59 60 61 } catch (ClassNotFoundException e) { 62 System.out.println("ドライバを読み込めませんでした " + e); 63 } catch (SQLException e) { 64 System.out.println("データベース接続エラー" + e); 65 } 66 } 67 68 /** 69 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 70 * response) 71 */ 72 protected void doPost(HttpServletRequest request, HttpServletResponse response) 73 throws ServletException, IOException { 74 // TODO Auto-generated method stub 75 doGet(request, response); 76 } 77 78}
jsp(ResultOutput.jsp)
java
1<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3<%@ page import="javax.servlet.http.*,javax.servlet.*"%> 4<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 5<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%> 6<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7<html> 8<head> 9<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 10<title>Insert title here</title> 11</head> 12<body> 13<%= request.getAttribute("data") %> 14</body> 15</html>
エラーコード
HTTP Status 404 – Not Found Type Status Report Message /Search/Search Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. Apache Tomcat/9.0.8
回答1件
あなたの回答
tips
プレビュー