I am trying to pass the data from servlet to JSP without forms. I am setting up a data in Servlet and through Dispatcher I am passing it to JSP file. Now when I am running JSP file I am getting error message I even have tried with JSTL as well, my problem is not with JSTL or JSP, problem is I am not able to get the data passed by servlet directly when JSP is ran, so question is can I even directly run JSP file and get the result on UI without forms. intention to this is I want to show Database contents on UI for which I will prepare a connection in servlet and then will pass result-set in JSP which will get displayed on UI without forms I don't want to submit any form just want to show the data of database on UI but through servlet, help me out.
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 File. 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>
I am getting below error message, when I am just running - ToTestPage.jsp file:
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);
Below is my Web Config File:
<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>
Complete Stack Trace:
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)