2014-03-05 180 views
0

我剛開始使用連接池,似乎無法獲得任何工作,例如獲取數據庫中的所有模塊。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) 
+0

可以請您發佈stacktrace嗎? –

+1

'request.getParameter(「action」)==「allModules」'看起來不太好。看看:[我如何比較Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo

+0

@ mig-25foxbat更新了問題。 – user3385250

回答

2

變化

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

使用equals()

+0

以前只是使用==,它的工作雖然,但沒有連接池。將嘗試它!編輯 - 仍然無法正常工作。 – user3385250

+0

可能在你的字符串被緩存在池中之前 –

+0

不明白你的意思是說實話。 – user3385250

0

這些屬性在JSP頁面中不可用,因爲在servlet中,您將重定向到JSP而不是轉發給它。重定向會導致完整的往返瀏覽器,然後瀏覽器會在單獨請求中調用您的JSP頁面,因此失去了原始請求中設置的任何屬性。

它看起來像你註釋掉了代碼在你的servlet(dispatcher.forward())中執行前進以代替response.sendRedirect()。嘗試恢復到前進。