我剛開始使用連接池,似乎無法獲得任何工作,例如獲取數據庫中的所有模塊。Servlet沒有設置屬性?
這裏是我的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)
可以請您發佈stacktrace嗎? –
'request.getParameter(「action」)==「allModules」'看起來不太好。看看:[我如何比較Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo
@ mig-25foxbat更新了問題。 – user3385250