gpt4 book ai didi

java - Servlet 未应用更改

转载 作者:行者123 更新时间:2023-11-28 22:02:20 24 4
gpt4 key购买 nike

在第一次编译我的 Servlet 之后,代码中的每一次修改对服务器都是不可见的。无论我在类里面写什么,我都会得到完全相同的输出。我试图在 Eclipse 中重新配置 Tomcat,但没有成功。 Servlet 包含连接到 SQL 的连接器,他的工作是从表中获取数据并通过 getRequestRispatcher 将其发送到 .jsp 文件,因为我给方法提供了错误的 jsp 路径,所以它给出了找不到文件的异常。但是,即使我更改路径以更正 Servlet,也好像什么都没有改变,它仍然给出相同的异常。

代码:

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class Load_table
*/
@WebServlet("/Load_table")
public class Load_table extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Load_table() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

List<Towar> magazyn = accessData();

request.setAttribute("magazyn", magazyn);

request.getRequestDispatcher("/WebContent/Main.jsp").include(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}


public List<Towar> accessData() {

List<Towar> output = new ArrayList<Towar>();

final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
final String DB_URL = "jdbc:oracle:thin:@localhost:1521:xe";


final String USER = "log";
final String PASS = "pass";


Connection conn = null;
Statement stmt = null;
try{

Class.forName(JDBC_DRIVER);


System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);



System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = String.format("SELECT * from Towary");
PreparedStatement prepStat = conn.prepareStatement(sql);

ResultSet rs = prepStat.executeQuery();


while(rs.next()){



output.add(new Towar(rs.getString("Nazwa"), rs.getString("Cena"),
rs.getString("Stan")));

}


rs.close();
stmt.close();
conn.close();
}catch(SQLException se){

se.printStackTrace();
}catch(Exception e){

e.printStackTrace();
}finally{

try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}

return output;
}

}

最佳答案

尝试从 tomcat 工作目录中删除已部署的应用程序,然后重新启动 tomcat 并再次部署。希望您会得到最新的更改。

关于java - Servlet 未应用更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29881651/

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