0

I am trying to retrieve a BasicDataSource from a bean. Whenever I try to access the bean, I get a NullPointerException

My code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@ page import="java.sql.*" %>
<%@ page import="org.apache.commons.dbcp2.BasicDataSource" %>
<%@ page import="org.postgresql.*" %>
<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Thank you for suscribing</title>
</head>
<body>
    <%  
        try {
            BasicDataSource source = (BasicDataSource)getServletContext().getAttribute("dataSource");
            out.write(source.toString());
        } catch(Exception e) {
            out.write(e.toString());
        }
    %>
    <p>${suscriber.name}<p/>
    <p>${suscriber.email}<p/>
</body>
</html>

My servlet context:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.edutainer4u.site" />



    <beans:bean id="dbURL" class="java.net.URI">
        <beans:constructor-arg
            value="'#{systemEnvironment['DATABASE_URL']}'">
        </beans:constructor-arg>
    </beans:bean>

    <beans:bean id="dataSource" 
        class="org.apache.commons.dbcp2.BasicDataSource">
        <beans:property name="url"
            value="jdbc:postgresql://localhost:10000/postgres">
        </beans:property>
        <beans:property value="blabla"
            name="username">
        </beans:property>
        <beans:property value="blabla"
            name="password">
        </beans:property>
    </beans:bean>
</beans:beans>

No matter what, I keep getting a NullPointerException. What am I doing wrong?

2 Answers2

1

There is nothing in your question to indicate that you have put an attribute named dataSource in the ServletContext used here

BasicDataSource source = (BasicDataSource)getServletContext().getAttribute("dataSource");

You seem to be confused by the naming of the Servlet API type ServletContext and the Spring WebApplicationContext loaded by the DispatcherServlet, also commonly referred to as the (dispatcher) servlet context. Those are two completely different components.

Here's some related content:

You're probably better off using model attributes (request attributes). To access a Spring bean from a JSP, you'll need to access the root WebApplicationContext or the servlet WebApplicationContext from the ServletContext using the appropriate attribute name, which may change from one version of Spring to the next, and access the bean on that.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

In the view resolver configuration you can add the list of beans that you need to be available in JSP:

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="exposedContextBeanNames">
      <list>
          <value>dataSource</value>
      </list>
  </property>
</beans:bean>
Mithun
  • 7,747
  • 6
  • 52
  • 68