gpt4 book ai didi

eclipse - Servlet 在 Eclipse 中的 Tomcat 上运行但不显示在 Ubuntu Server Tomcat 上

转载 作者:行者123 更新时间:2023-11-28 22:31:36 26 4
gpt4 key购买 nike

我有一个如下所示的 servlet,它在我的 Eclipse tomcat 上完美运行。当我将它上传到 webapps 文件夹并在我的 Ubuntu 服务器上重新启动我的 tomcat 时,它的 URL(/ServerAPP/Login) 不会加载,尽管 tomcat 根页面没有问题。如果有人知道为什么会这样,我将不胜感激。我对 Ubuntu 和 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;
}

最佳答案

所以我最终弄明白了,我认为这可能会帮助其他人知道。原因:显然,我在 ubuntu 上的 tomcat 只有 java 6,而我的 eclipse 是针对 java 7。

修复:我去了项目属性并更改了目标(这让 eclipse 发疯了),但随后重新编译它并将它放在 ubuntu 上的 tomcat 上,它工作正常。

感谢那些试图帮助我的人。

关于eclipse - Servlet 在 Eclipse 中的 Tomcat 上运行但不显示在 Ubuntu Server Tomcat 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15376068/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com