長文失礼します。
Java(Eclipse)でタスク管理アプリ(Index,Create,Show,Edit)を作成をしています。
コード上はエラーはないのですが、ブラウザ上で動作確認すると500エラー(null)になってしまいます。
今現在、IndexとNewは動く状態です。
エラーの内容です。
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Integer.java:542)
java.lang.Integer.parseInt(Integer.java:615)
controllers.editServlet.doGet(editServlet.java:37)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
filters.EncodingFilter.doFilter(EncodingFilter.java:38)
初心者のため、この部分がこうなってるからこう書き換えるという感じで教えていただくと嬉しいです。
よろしくお願いします。
Macbook Air使用。Java8とTomcat8使用です。
1枚目(edit.jsp)
2枚目(editServlet)
3枚目(show.jsp)
4枚目(showServlet)
5枚目(index.jsp)
6枚目(indexServlet)
7枚目(new.jsp)
8枚目(newServlet)
edit.jsp
java
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<c:import url="../layout/app.jsp"> 5 <c:param name="content"> 6 <h2>id : ${task.id}の編集ページ</h2> 7 <form method="POST" action="${pageContext.request.contextPath}/update"> 8 <c:import url="_form.jsp" /> 9 </form> 10 <p> 11 <a href="${pageContext.request.contextPath}/index">一覧に戻る</a> 12 </p> 13 <p> 14 <a href="#" onclick="confirmDestroy();">削除</a> 15 </p> 16 <form method="POST" 17 action="${pageContext.request.contextPath}/destroy"> 18 <input type="hidden" name="_token" value="${_token}" /> 19 </form> 20 <script> 21 function confirmDestroy() { 22 if (confirm("本当に削除しますか?")) { 23 document.forms[1].submit(); 24 } 25 } 26 </script> 27 28 </c:param> 29</c:import> 30
editServlet
java
1package controllers; 2 3import java.io.IOException; 4 5import javax.persistence.EntityManager; 6import javax.servlet.RequestDispatcher; 7import javax.servlet.ServletException; 8import javax.servlet.annotation.WebServlet; 9import javax.servlet.http.HttpServlet; 10import javax.servlet.http.HttpServletRequest; 11import javax.servlet.http.HttpServletResponse; 12 13import models.TaskData; 14import utils.DBUtil; 15 16/** 17 * Servlet implementation class EditServlet 18 */ 19@WebServlet("/edit") 20public class editServlet extends HttpServlet { 21 private static final long serialVersionUID = 1L; 22 23 /** 24 * @see HttpServlet#HttpServlet() 25 */ 26 public editServlet() { 27 super(); 28 } 29 30 /** 31 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 32 * response) 33 */ 34 protected void doGet(HttpServletRequest request, HttpServletResponse response) 35 throws ServletException, IOException { 36 EntityManager em = DBUtil.createEntityManager(); 37 TaskData t = em.find(TaskData.class, Integer.parseInt(request.getParameter("id"))); 38 em.close(); 39 request.setAttribute("task", t); 40 request.setAttribute("_token", request.getSession().getId()); 41 request.getSession().setAttribute("task_id", t.getId()); 42 RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/tasks/edit.jsp"); 43 rd.forward(request, response); 44 } 45} 46```show.jsp 47```java 48<%@ page language="java" contentType="text/html; charset=UTF-8" 49 pageEncoding="UTF-8"%> 50<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 51<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 52<c:import url="../layout/app.jsp"> 53 <c:param name="content"> 54 <h2>id : ${task.id} 詳細</h2> 55 <p> 56 <c:out value="${task.content}" /> 57 </p> 58 <p> 59 作成日時 60 <fmt:formatDate value="${task.created_at}" 61 pattern="yyyy-MM-dd HH:mm:ss" /> 62 </p> 63 <p> 64 更新日時 65 <fmt:formatDate value="${task.updated_at}" 66 pattern="yyyy-MM-dd HH:mm:ss" /> 67 </p> 68 <p> 69 <a href="${pageContext.request.contextPath}/index">一覧にもどる</a> 70 </p> 71 <p><a href="${pageContext.request.contextPath}/edit?id=${message.id}">編集</a></p> 72 </c:param> 73</c:import>
showServlet
java
1package controllers; 2 3import java.io.IOException; 4 5import javax.persistence.EntityManager; 6import javax.servlet.RequestDispatcher; 7import javax.servlet.ServletException; 8import javax.servlet.annotation.WebServlet; 9import javax.servlet.http.HttpServlet; 10import javax.servlet.http.HttpServletRequest; 11import javax.servlet.http.HttpServletResponse; 12 13import models.TaskData; 14import utils.DBUtil; 15 16/** 17 * Servlet implementation class ShowServlet 18 */ 19@WebServlet("/show") 20public class ShowServlet extends HttpServlet { 21 private static final long serialVersionUID = 1L; 22 23 /** 24 * @see HttpServlet#HttpServlet() 25 */ 26 public ShowServlet() { 27 super(); 28 // TODO Auto-generated constructor stub 29 } 30 31 /** 32 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 33 * response) 34 */ 35 protected void doGet(HttpServletRequest request, HttpServletResponse response) 36 throws ServletException, IOException { 37 EntityManager em = DBUtil.createEntityManager(); 38 TaskData t = em.find(TaskData.class, Integer.parseInt(request.getParameter("id"))); 39 em.close(); 40 request.setAttribute("task", t); 41 RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/tasks/show.jsp"); 42 rd.forward(request, response); 43 } 44} 45```index.jsp 46```java 47<%@ page language="java" contentType="text/html; charset=UTF-8" 48 pageEncoding="UTF-8"%> 49<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 50<c:import url="../layout/app.jsp"> 51 <c:param name="content"> 52 <h2>タスク一覧</h2> 53 <ul> 54 <c:forEach var="task" items="${tasks}"> 55 <li> 56 <a href="${pageContext.request.contextPath}/show?id=${task.id}"> 57 <c:out value="${task.id}" /> 58 </a> <c:out value="${task.content}" /> 59 </li> 60 </c:forEach> 61 </ul> 62 <p> 63 <a href="${pageContext.request.contextPath}/new">新規タスク</a> 64 </p> 65 </c:param> 66</c:import> 67 68```indexServlet 69```java 70package controllers; 71 72import java.io.IOException; 73import java.util.List; 74 75import javax.persistence.EntityManager; 76import javax.servlet.RequestDispatcher; 77import javax.servlet.ServletException; 78import javax.servlet.annotation.WebServlet; 79import javax.servlet.http.HttpServlet; 80import javax.servlet.http.HttpServletRequest; 81import javax.servlet.http.HttpServletResponse; 82 83import models.TaskData; 84import utils.DBUtil; 85 86/** 87 * Servlet implementation class IndexServlet 88 */ 89@WebServlet("/index") 90public class indexServlet extends HttpServlet { 91 private static final long serialVersionUID = 1L; 92 93 /** 94 * @see HttpServlet#HttpServlet() 95 */ 96 public indexServlet() { 97 super(); 98 // TODO Auto-generated constructor stub 99 } 100 101 /** 102 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 103 * response) 104 */ 105 protected void doGet(HttpServletRequest request, HttpServletResponse response) 106 throws ServletException, IOException { 107 EntityManager em = DBUtil.createEntityManager(); 108 List<TaskData> tasks = em.createNamedQuery("getAllTasks", TaskData.class).getResultList(); 109 response.getWriter().append(Integer.valueOf(tasks.size()).toString()); 110 em.close(); 111 request.setAttribute("tasks", tasks); 112 113 RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/tasks/index.jsp"); 114 rd.forward(request, response); 115 116 } 117 118} 119```new.jsp 120```java 121<%@ page language="java" contentType="text/html; charset=UTF-8" 122 pageEncoding="UTF-8"%> 123<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 124<c:import url="../layout/app.jsp"> 125 <c:param name="content"> 126 <h2>新規作成</h2> 127 <form method="POST" action="${pageContext.request.contextPath}/create"> 128 <c:import url="_form.jsp" /> 129 </form> 130 <p> 131 <a href="${pageContext.request.contextPath}/index">一覧に戻る</a> 132 </p> 133 </c:param> 134</c:import>
newServlet
java
1package controllers; 2 3import java.io.IOException; 4 5import javax.servlet.RequestDispatcher; 6import javax.servlet.ServletException; 7import javax.servlet.annotation.WebServlet; 8import javax.servlet.http.HttpServlet; 9import javax.servlet.http.HttpServletRequest; 10import javax.servlet.http.HttpServletResponse; 11 12import models.TaskData; 13 14/** 15 * Servlet implementation class NewServlet 16 */ 17@WebServlet("/new") 18public class NewServlet extends HttpServlet { 19 private static final long serialVersionUID = 1L; 20 21 /** 22 * @see HttpServlet#HttpServlet() 23 */ 24 public NewServlet() { 25 super(); 26 // TODO Auto-generated constructor stub 27 } 28 29 /** 30 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 31 * response) 32 */ 33 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 34 // CSRF�� 35 request.setAttribute("_token", request.getSession().getId()); 36 // ���� 37 request.setAttribute("task", new TaskData()); 38 RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/tasks/new.jsp"); 39 rd.forward(request, response); 40 41 } 42 43} 44
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/11/25 01:16
2020/11/25 09:44
2020/11/26 04:08
2020/11/26 04:46
2020/11/26 05:19