gpt4 book ai didi

java - 表单加载但进程不会停止

转载 作者:行者123 更新时间:2023-12-01 14:50:49 24 4
gpt4 key购买 nike

我在 JSP 页面中的数组列表中查看记录时遇到问题。每次我通过 javascript onload 事件自动加载 JSP 页面时,都会显示数据,但该过程不会停止。

类别类:

package proyecto.modelo;

public class Categoria {

private int idcategoria;

public int getIdcategoria() {
return idcategoria;
}

public void setIdcategoria(int idcategoria) {
this.idcategoria = idcategoria;
}
}

BaseDAO 类:

package proyecto.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDAO {

protected void cerrarConexion(Connection con) throws RuntimeException {
try {
if (con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarConexion: " + se);
}
}

protected void cerrarResultSet(ResultSet rs) throws RuntimeException {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarResultSet: " + se);
}
}

protected void cerrarStatement(PreparedStatement stmt)
throws RuntimeException {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarStatement: " + se);
}
}

protected void cerrarCallable(CallableStatement callstmt)
throws RuntimeException {
try {
if (callstmt != null) {
callstmt.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarCallable: " + se);
}
}
}

CategoriaDAO 类:

package proyecto.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import proyecto.excepcion.DAOExcepcion;
import proyecto.modelo.Categoria;
import proyecto.util.ConexionBD;

public class CategoriaDAO extends BaseDAO {

public Collection<Categoria> listarIdCat() throws DAOExcepcion{

Collection<Categoria> = new ArrayList<Categoria>();
String query = "SELECT id_categoria from categoria ";
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;

try {
con=ConexionBD.obtenerConexionDirecta();
stmt=con.prepareStatement(query);
rs=stmt.executeQuery();

while(rs.next()) {
Categoria vo=new Categoria();
vo.setIdcategoria(rs.getInt("id_categoria"));
c.add(vo);
}

} catch (SQLException e) {
System.err.println(e.getMessage());
throw new DAOExcepcion(e.getMessage());
} finally {
this.cerrarStatement(stmt);
this.cerrarResultSet(rs);
this.cerrarConexion(con);
}

return c;
}

}

CategoriaNegocio 类:

package proyecto.negocio;

import java.util.Collection;
import java.util.List;

import proyecto.dao.CategoriaDAO;
import proyecto.excepcion.DAOExcepcion;
import proyecto.modelo.Categoria;

public class CategoriaNegocio {

public Collection<Categoria> listarIdCat() throws DAOExcepcion {
CategoriaDAO dao = new CategoriaDAO();
Collection<Categoria> lista = dao.listarIdCat();
return lista;
}

}

Servlet doPost() 方法:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
CategoriaNegocio negocio = new CategoriaNegocio();

try {
Collection<Categoria> lista = negocio.listarIdCat();
request.setAttribute("IDCATEGORIA", lista);
} catch (DAOExcepcion e) {
System.out.println(e.getMessage());
}

RequestDispatcher rd = request.getRequestDispatcher("listar_idcat.jsp");
rd.forward(request, response);
}

JSP:

<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"   %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<script type="text/javascript">
window.onload = function() {
document.formulario.submit()
};
document.close();
</script>
</head>
<body>
<form action="ListarIdCatServlet" method="post" name="formulario"></form>
<table>
<c:forEach items="${IDCATEGORIA}" var="c">
<tr>
<td>${c.idcategoria}</td>
</tr>
</c:forEach>
</table>
</body>
</html>

最佳答案

您似乎想要在 GET 请求上调用 servlet。你正在以错误的方式处理这个问题。您不应在加载页面时提交 POST 表单。您应该在 doGet() 中执行该工作servlet 的方法并直接调用它。

您需要进行以下更改:

  1. 将 servlet 的 URL 模式更改为 /listar_idcat .

  2. 重命名 doPost servlet 的方法为doGet .

  3. 移动listar_idcat.jsp文件写入/WEB-INF文件夹(这会阻止最终用户直接访问)。

  4. 更改 getRequestDispatcher("listar_idcat.jsp");在 servlet 中调用 getRequestDispatcher("/WEB-INF/listar_idcat.jsp"); .

  5. 删除整个 <script>来自 JSP。

  6. 删除整个 <form>来自 JSP。

现在,通过http://localhost:8080/context/listar_idcat打开JSP相反(是的,没有 .jsp 扩展!它将直接调用 servlet 的 doGet())。

另请参阅:

关于java - 表单加载但进程不会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14876352/

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