我刚刚开始使用连接池,似乎无法正常工作,例如获取数据库中的所有模块。
这是我的testconnectionservlet,一定是问题所在:
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);
}
}
有人注意到一些与众不同的事情吗?
当我在JSP中执行此类操作时,我得到了一个java.lang.NullPointerException,但是不应该从servlet那里得到它?:
<%
List<Module> modules;
modules = (List)(request.getSession().getAttribute("theModulesAsc"));
out.print(modules.size());
%>
堆栈跟踪:
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)
答案 0 :(得分:2)
更改
if (request.getParameter("action") == "allModules") {
使用equals()
答案 1 :(得分:0)
JSP页面中没有这些属性,因为在servlet中,您将重定向到JSP而不是转发给它。重定向导致浏览器的完整往返,然后在单独的请求中调用您的JSP页面,因此丢失您在原始请求中设置的任何属性到servlet。
看起来您在servlet(dispatcher.forward()
)中注释了代码,以将其替换为response.sendRedirect()
。尝试恢复前进。