0

I'm making a webapp using JSP and MYSQL.

I have set up a connection pool and used it in a Servlet, but I cannot access it.

I had to create manually the "lib" folder inside WEB-INF to put the database connector and the META-INF folder to put the context.xml file, because those folders were not there at the begining.

I'm following a tutorial from 2016 so I don't know how different it is now.

Thanks in advance for your help.

Src folder:

folder structure

Main files:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
         version="5.0">
</web-app>

context.xml

<Context>
    <Resource name="jdbc/wishes" auth="Container" type="javax.sql.DataSource" maxActive="15" maxIdle="3" maxWait="5000" username="root" password="" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/jee">
    </Resource>
</Context>

Servlet

package com.gabit.dev.makeawish.controllers;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

@WebServlet(name = "ServletDatabase", value = "/ServletDatabase")
public class ServletDatabase extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Resource(name = "jdbc/wishes")
    private DataSource myPool;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter output = response.getWriter();
        response.setContentType("text/plain");
        Connection myConnection = null;
        Statement myStatement = null;
        ResultSet myResult = null;
        try {
            myConnection = myPool.getConnection();
            String query = "SELECT * FROM wishes";
            myStatement = myConnection.createStatement();
            myResult = myStatement.executeQuery(query);

            while (myResult.next()) {
                String title = myResult.getString(2);
                output.println(title);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Error

java.lang.NullPointerException: Cannot invoke "javax.sql.DataSource.getConnection()" because "this.myPool" is null
    at com.gabit.dev.makeawish.controllers.ServletDatabase.doGet(ServletDatabase.java:31)
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:683)
    ...
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:833)
gabit
  • 23
  • 5
  • Is this a homework? Because no one in their right mind should be writing servlet code for enterprise software. – Abhijit Sarkar Oct 02 '22 at 00:15
  • Yes, it is a test project. I'm a beginner about Java EE. I think I could connect and then disconnect from the database to handle each request for now. – gabit Oct 03 '22 at 13:34

0 Answers0