前提・実現したいこと
ServletからJSPにデータを渡したいのですかエラーが発生しました。
どのように改善すればよろしいでしょうか?
発生している問題・エラーメッセージ
org.apache.jasper.JasperException: [/Abs.jsp] の処理中に行番号 [31] で例外が発生しました。 28: </tr> 29: </thead> 30: <tbody> 31: <%for(String x:list) {%> 32: <tr> 33: <td><%= x %></td> 34: <td><button type="button" class="btn btn-outline-warning">変更</button></td> Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:617) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:514) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
ソースコード
Abslistservlet.java
package sample; import java.io.IOException; import java.util.ArrayList; 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; import javax.servlet.http.HttpSession; @WebServlet("/list") public class AbslistServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); HttpSession session = request.getSession(); ArrayList<String> list = new ArrayList<>(); list.add("a"); list.add("b"); session.setAttribute("lists", list); RequestDispatcher dispatcher = request.getRequestDispatcher("Abs.jsp"); dispatcher.forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); } }
Abs.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.ArrayList" %> <%@ page import="sample.AbslistServlet" %> <% request.setCharacterEncoding("UTF-8"); @SuppressWarnings("unchecked") ArrayList<String> list = (ArrayList<String>) session.getAttribute("lists");%> <!DOCTYPE html> <html lang="ja"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> <head> <meta charset="UTF-8"> <title>一覧</title> </head> <body> <%for(String x:list) {%> <tr> <td><%= x %></td> <%} %> </tbody> </table> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script> </body> </html>
回答2件
あなたの回答
tips
プレビュー