前提・実現したいこと
SELECT WHERE句を使い詳細ページを実装したく、SQL文で下記のようにサーブレットに記述しましたが、DBからうまくデータを引っ張ってこれません。長時間調べて試しましたがブラウザには値はNULLの状態です。
ご教授いただけたら幸いです。
発生している問題・エラーメッセージ
java.lang.NumberFormatException: null at java.base/java.lang.Integer.parseInt(Integer.java:614) at java.base/java.lang.Integer.parseInt(Integer.java:770) at servlet.ShowServlet.doGet(ShowServlet.java:49) at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at filter.PageEncoding.doFilter(PageEncoding.java:39) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:688) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Thread.java:834)
ShowServlet.java
package servlet; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.HashMap; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ShowServlet */ @WebServlet("/ShowServlet") public class ShowServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ShowServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); //DB connection String url = "jdbc:h2:tcp://localhost/~/posters"; String user ="sa"; String password = ""; try { Connection connection = DriverManager.getConnection(url, user, password); connection.setAutoCommit(false); String sql = "SELECT * FROM POSTS WHERE POST_ID = ?;"; PreparedStatement statement = connection.prepareStatement(sql); statement.setInt(1, Integer.parseInt(request.getParameter("POST_ID"))); ResultSet results = statement.executeQuery(); ArrayList<HashMap<String,String>> rows = new ArrayList<HashMap<String,String>>(); while(results.next()) { int post_id = results.getInt("POST_ID"); request.setAttribute("POST_ID", post_id); String message = results.getString("message"); request.setAttribute("message", message); String image = results.getString("image"); request.setAttribute("image", image); String video = results.getString("video"); request.setAttribute("video", video); } request.setAttribute("rows", rows); results.close(); statement.close(); } catch(Exception e) { e.printStackTrace(); } RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/show.jsp"); dispatcher.forward(request,response); } }
show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h2>MEMO DETAIL</h2> <p>This app is made by Java Servlet and JSP.</p> <br> <form action="delete?id=<%= request.getAttribute("id") %>" method="POST"> <p>ID: <%= request.getAttribute("post_id") %></p> <p>TITLE: <%= request.getAttribute("message") %></p> <p>CONTENT:</p> <p><%= request.getAttribute("image") %></p> <p><%= request.getAttribute("video") %></p> <br> </form> <br> <a href="/Posters/LoginServlet">トップへ</a> </body> </html>
top.jsp
top.jsp
1<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 5<!DOCTYPE html> 6<html> 7<head> 8<meta charset="UTF-8"> 9<title>Posters</title> 10</head> 11<body> 12<p>ようこそ<c:out value="${userId }" />さん</p> 13<a href="/Posters/NewServlet">投稿する</a> 14<a href="/Posters/IndexServlet">ログアウト</a> 15<c:forEach var="post" items="${postList }"> 16 <p><c:out value="${post.message}"></c:out>:<c:out value="${post.image }"></c:out><c:out value="${post.video }"></c:out> 17 <a href="/Posters/ShowServlet?post_id=?">詳細</a> 18 </p> 19</c:forEach> 20 21 22 23 24</body> 25</html>
補足情報
eclipse
tomcat
DB:H2
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー