【问题标题】:form load but the process does not stop表单加载但过程不会停止
【发布时间】:2013-02-14 13:53:06
【问题描述】:

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

Categoria类:

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>

【问题讨论】:

  • 老兄,正确格式化你的问题!并删除所有不必要的代码,以便我们仅尝试了解与您的问题实际相关的部分
  • 你能告诉我你为什么要在页面加载时提交表单吗?
  • 我想要的是让页面在没有按钮的情况下自动加载
  • 使用引号按钮格式化引号,使用代码按钮格式化代码。不要同时使用引号和代码按钮来格式化代码。更重要的是,在您的整个问题中,您没有任何引号,因此您根本不应该在整个问题中使用引号按钮。
  • 我修复了疯狂的格式。我注意到您甚至删除了所有 &gt; 字符,使代码在语法上无效。你不应该那样做。只需选择代码并按代码按钮。就这样。真的。

标签: java javascript jsp servlets


【解决方案1】:

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

您需要进行以下更改:

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

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

  3. listar_idcat.jsp 文件移动到/WEB-INF 文件夹中(这样可以防止最终用户直接访问)。

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

  5. 从 JSP 中删除整个 &lt;script&gt;

  6. 从 JSP 中删除整个 &lt;form&gt;

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

另见:

【讨论】:

  • 非常感谢你的朋友最近我开始涉足 web java 的世界,如果你能帮助我成为你所知道的一些手册的朋友,谢谢。
  • 你好 BalusC 如果你能在组合框插入数据后给我一个关于 listarme 的想法
  • 如果您有新的独立问题,请按 按钮。您不应该在完全不同的问题上使用Add Comment 按钮。
猜你喜欢
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-29
  • 1970-01-01
  • 2021-08-07
相关资源
最近更新 更多