2014-06-19 71 views
0

我試圖轉換Java類以顯示在網頁上。以最佳答案here爲準則。如果我用System.out打印出所有內容,Java就會完成它應有的功能。當試圖轉發到一個jsp頁面時,它會循環(重新實例化?),並且不會停止(必須手動終止該進程)。Java Servlet循環時轉發到jsp

Connector.java

public class Connector extends HttpServlet { 
    private static URL url = https://my.server.com/WEB-INF/ws/service.php?wsdl");; 
    private static final String USERNAME = "JoeBoB"; 
    private static final String PASSWORD = "1337pass"; 

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 


     //login to my.server.com 
     try { 
      authToken = serverAPI.login(USERNAME, PASSWORD); 
      System.out.println("Logged into server with Token: " + authToken); 
      //this shows up in console over and over again, until manually killed 
     } 
     catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

     request.setAttribute("message","bacon"); 

     request.getRequestDispatcher("/WEB-INF/draw.jsp").forward(request, response); 
     //line above appears to be the one that re-inits the class. 
     //commenting this out stops the looping 
     //but also prevents the data from showing on the webpage 
     serverAPI.logout(authToken); 

WEB-INF/draw.jsp

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>This is my drawn page</title> 
    </head> 
    <body> 
     ${message} 
    </body> 
</html> 

WEB-INF/web.xml中

<web-app> 
    <display-name>Connector</display-name> 

    <servlet> 
     <servlet-name>Connector</servlet-name> 
     <servlet-class>com.company.package.Connector</servlet-class> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Connector</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

我有一種感覺它有點簡單(我忘了配置或錯誤配置的東西),但對於我的生活無法弄清楚什麼。

回答

2

通過將/ *映射到您的servlet,您正在重寫JSP請求的默認處理程序。您需要使用更具體的模式,使用文件擴展名或子目錄。

+0

我剛剛想到了我自己,現在又回來讓別人了。我所做的就是將web.xml中的'url-pattern'改爲指向index.jsp--它引發了java,然後指向draw.jsp – Andenthal