我试图将数据从servlet传递给没有表单的JSP。我在Servlet中设置数据,并通过Dispatcher将其传递给JSP文件。现在,当我运行JSP文件时,我收到错误消息我甚至尝试过使用JSTL,我的问题不在于JSTL或JSP,问题是当JSP运行时我无法直接获取servlet传递的数据,所以问题是我是否可以直接运行JSP文件并在没有表单的UI上获得结果。我想在UI上显示数据库内容,我将在servlet中准备连接,然后在JSP中传递结果集,这将在没有表单的UI上显示我不想提交任何表单想要在UI上显示数据库的数据,但是通过servlet,帮助我。
Below is the code:
Servlet:
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NewClass extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArrayList<String> myList;
myList = new ArrayList<>();
myList.add("one");
myList.add("Two");
myList.add("Three");
request.setAttribute("MyList", myList);
request.getRequestDispatcher("/ToTestPage.jsp").forward(request, response);
//request.getRequestDispatcher("/ToTestPage.jsp").include(request, response);
}
}
JSP文件。 ToTestJSP:
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<body>
<%
ArrayList<String> list = (ArrayList<String>) request.getAttribute("MyList");
Iterator itr = list.iterator();
while(itr.hasNext()) {
String value = (String) itr.next();
out.println(value);
}
%>
</body>
</body>
</html>
当我刚刚运行时,我收到以下错误消息 - ToTestPage.jsp文件:
org.apache.jasper.JasperException: An exception occurred processing JSP page /ToTestPage.jsp at line 22
19: <%
20:
21: ArrayList<String> list = (ArrayList<String>) request.getAttribute("MyList");
22: Iterator itr = list.iterator();
23: while(itr.hasNext()) {
24: String value = (String) itr.next();
25: out.println(value);
以下是我的Web配置文件:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>NewClass</servlet-name>
<servlet-class>NewClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewClass</servlet-name>
<url-pattern>/NewClass</url-pattern>
</servlet-mapping>
</web-app>
完成堆栈跟踪:
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:580)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NullPointerException
org.apache.jsp.ToTestPage_jsp._jspService(ToTestPage_jsp.java:103)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)