0

I have just started using connection pooling and can't seem to get anything to work, such as getting all modules that are in a database.

Here's my testconnectionservlet which must be the problem:

 package Servlet;


 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;

 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;

 import DTO.Module;
 import DAO.*;
 import Service.ModuleService;



 /**
  * Servlet implementation class checkLoginServlet
  */
 @WebServlet(urlPatterns={"/TestConnectionServlet"})
 public class TestConnectionServlet extends HttpServlet 
 {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public TestConnectionServlet() 
{
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
     processRequest(request, response);
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
    processRequest(request, response);
}

private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{

        String forwardToJsp = "";
        HttpSession session = request.getSession();
        String clientSessionId = session.getId();

    //Check the 'action' parameter to see what the user wants...
    if ( request.getParameter("action") != null)
    {
        // Create a LoginCommand and execute it




        ModuleService mService = new ModuleService();
        if (request.getParameter("action") == "allModules") {
            List allModules = mService.getAllModules();
            session.setAttribute("theModulesNoOrder", allModules);
            forwardToJsp = "all-Modules.jsp";
        } else if (request.getParameter("action") == "allModulesAsc") {
            List allModulesAsc = mService.getAllModulesCodeAsc();
            session.setAttribute("theModulesAsc", allModulesAsc);
            forwardToJsp = "whatever.jsp";
        }
    }

    //Get the request dispatcher object and forward the request to the appropriate JSP page...
    //RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(forwardToJsp);
    //dispatcher.forward(request, response);

    //to stop UserActionServelet coming up in URL when logging in.
    //lines above were in it before
    response.sendRedirect(forwardToJsp);
}
 }

Anyone notice anything out of the ordinary?

When I do something such as this in a JSP I get a java.lang.NullPointerException, but shouldn't this be there from the servlet?:

    <%
        List<Module> modules;
        modules = (List)(request.getSession().getAttribute("theModulesAsc"));
        out.print(modules.size());
      %>

Stacktrace:

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
jmail
  • 5,944
  • 3
  • 21
  • 35
  • can you please post the stacktrace? – rogue-one Mar 05 '14 at 20:05
  • 1
    `request.getParameter("action") == "allModules"` doesn't look good. Take a look at: [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Mar 05 '14 at 20:05

2 Answers2

2

change

if (request.getParameter("action") == "allModules") {

to use equals()

jmj
  • 237,923
  • 42
  • 401
  • 438
0

The attributes are not available in your JSP page because in the servlet you are redirecting to the JSP instead of forwarding to it. Redirecting causes a complete roundtrip to the browser which then calls your JSP page in a separate request, therefore losing any attribute you have set in the original request to the servlet.

It looks like you commented out the code doing a forward in your servlet (dispatcher.forward()) to replace it with response.sendRedirect(). Try reverting back to the forward.

David Levesque
  • 22,181
  • 8
  • 67
  • 82