2013-03-13 39 views
1

我有一個servlet,如下所示,它在我的Eclipse Tomcat上完美運行。當我將它上傳到webapps文件夾並在我的Ubuntu服務器上重新啓動我的tomcat時,它的URL(/ ServerAPP/Login)將不會加載,儘管tomcat根頁面沒有問題。如果有人知道爲什麼會發生這種情況,我將不勝感激。我對Ubuntu和Tomcat的內部工作相當陌生,所以我可能錯過了任何東西。 我可以根據要求提供更多信息,我只是不確定需要多少,或者有什麼簡單而愚蠢的東西我沒有考慮。在Eclipse中運行在Tomcat上但在Ubuntu服務器上不顯示的Servlet Tomcat

package Actions; 
import java.io.*; 
import java.sql.*; 
import java.util.logging.Logger; 


import javax.servlet.*; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.*; 
import javax.sql.DataSource; 

@WebServlet(urlPatterns={"/Login"}) 
public class Login extends HttpServlet implements DataSource { 

private String User = null; 
Connection connection = null; 
private String password = null; 


public void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException { 
    if(request.getParameter("User") != null){ 
     this.setUser((String) request.getParameter("User").toString()); 
    } 
    if(request.getParameter("password") != null){ 
     this.setPassword((String) request.getParameter("password").toString()); 
    } 



    try { 
     System.out.println("Loading driver..."); 
     Class.forName("com.mysql.jdbc.Driver"); 
     System.out.println("Driver loaded!"); 
    } catch (ClassNotFoundException e) { 
     throw new RuntimeException("Cannot find the driver in the classpath!", e); 
    } 

    Login ds = new Login(); 
    try { 
     connection = ds.getConnection(); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 


    PrintWriter out = response.getWriter(); 
    if(connection != null){ 

     //out.println(User + " " + password); 

     //Check if user exists in database 
     if(User!= null){ 

      Statement stmt; 
      ResultSet rs; 
      try { 
       stmt = connection.createStatement(); 
       rs = stmt.executeQuery("SELECT * FROM tblUsers WHERE Username = '" + User + "';"); 


       if(!rs.next()){ 
        out.println("Username: " + User + " was not found in Users table."); 
       } 
       else{ 
        //User was found now check if password is correct 
        if(rs.getString(3).equals(password)){ 
         out.println("User: " + User + " login successful!"); 
        } 
        else if(rs.getString(3).equals(password) == false){ 
         //password was incorrect 
         out.println("Password incorrect!"); 
        } 

        /* 
        while(rs.next()){ 
         out.println("User ID: " + rs.getInt(1) + " Username: " + rs.getString(2)); 
        } 
        */ 

       } 

       rs.close(); 
       stmt.close(); 
       connection.close(); 

      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 

     /* 
     Statement stmt; 
     ResultSet rs; 
     try { 
      stmt = connection.createStatement(); 
      rs = stmt.executeQuery("Select * from tblUsers;"); 
      while(rs.next()){ 
       out.println("User ID: " + rs.getInt(1) + " Username: " + rs.getString(2)); 
      } 
      rs.close(); 
      stmt.close(); 
      connection.close(); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     */ 

    } 



} 



@Override 
public PrintWriter getLogWriter() throws SQLException { 
    // TODO Auto-generated method stub 
    return null; 
} 


@Override 
public void setLogWriter(PrintWriter out) throws SQLException { 
    // TODO Auto-generated method stub 

} 


@Override 
public void setLoginTimeout(int seconds) throws SQLException { 
    // TODO Auto-generated method stub 

} 


@Override 
public int getLoginTimeout() throws SQLException { 
    // TODO Auto-generated method stub 
    return 0; 
} 


@Override 
public Logger getParentLogger() throws SQLFeatureNotSupportedException { 
    // TODO Auto-generated method stub 
    return null; 
} 


@Override 
public <T> T unwrap(Class<T> iface) throws SQLException { 
    // TODO Auto-generated method stub 
    return null; 
} 


@Override 
public boolean isWrapperFor(Class<?> iface) throws SQLException { 
    // TODO Auto-generated method stub 
    return false; 
} 


@Override 
public Connection getConnection() throws SQLException { 
    if (connection != null) { 
     System.out.println("Cant craete a Connection"); 
} else { 
     connection = DriverManager.getConnection(
         "<redacted>", "AWSCards", "Cards9876"); 
} 
return connection; 
} 


@Override 
public Connection getConnection(String username, String password) 
     throws SQLException { 
    // TODO Auto-generated method stub 
    if (connection != null) { 
      System.out.println("Cant craete a Connection"); 
    } else { 
      connection = DriverManager.getConnection(
          "<redacted>", username, password); 
    } 
    return connection; 
} 


public String getUser() { 
    return User; 
} 


public void setUser(String user) { 
    User = user; 
} 



public String getPassword() { 
    return password; 
} 



public void setPassword(String password) { 
    this.password = password; 
} 

}

+1

在'catch'節中添加'response.sendError()'異常,並指定關於堆棧跟蹤的所有信息。似乎數據庫有問題(連接等)。 – 2013-03-13 08:24:36

+0

那麼數據庫會從Eclipse中正常返回.... – dhockey 2013-03-14 00:46:41

+0

確保數據庫也可以從Ubuntu獲得。 – 2013-03-14 02:17:10

回答

0

所以我最終想出來的,我想這可能幫助別人知道。 原因: 很顯然,我在Ubuntu的Tomcat只有Java 6中,而我的日食是針對Java 7中

修復: 我去項目屬性,改變了目標(這使蝕狂),但隨後重新編譯它並把它放在Ubuntu上的Tomcat上,它工作正常。

感謝那些試圖幫助我的人。

相關問題