Java 表单加载,但进程不会停止

Java 表单加载,但进程不会停止,java,javascript,jsp,servlets,Java,Javascript,Jsp,Servlets,从JSP页面中的arraylist查看记录时遇到问题。每次我通过javascript onload事件自动加载JSP页面时,都会显示数据,但过程不会停止 Categoriaclass: package proyecto.modelo; public class Categoria { private int idcategoria; public int getIdcategoria() { return idcategoria; } pu

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

Categoria
class:

package proyecto.modelo;

public class Categoria {

    private int idcategoria;

    public int getIdcategoria() {
        return idcategoria;
    }

    public void setIdcategoria(int idcategoria) {       
        this.idcategoria = idcategoria;
    }
}
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);
        }
    }
}
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;
    }

}
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;
    }

}
BaseDAO
class:

package proyecto.modelo;

public class Categoria {

    private int idcategoria;

    public int getIdcategoria() {
        return idcategoria;
    }

    public void setIdcategoria(int idcategoria) {       
        this.idcategoria = idcategoria;
    }
}
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);
        }
    }
}
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;
    }

}
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;
    }

}
CategoriaDAO
class:

package proyecto.modelo;

public class Categoria {

    private int idcategoria;

    public int getIdcategoria() {
        return idcategoria;
    }

    public void setIdcategoria(int idcategoria) {       
        this.idcategoria = idcategoria;
    }
}
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);
        }
    }
}
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;
    }

}
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;
    }

}
Servlets
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);
}
protectedvoiddopost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
Category Anegocio negocio=新的Category Anegocio();
试一试{
集合lista=negocio.listarIdCat();
setAttribute(“IDCATEGORIA”,lista);
}渔获物(DAOExcepcion e){
System.out.println(e.getMessage());
}
RequestDispatcher rd=request.getRequestDispatcher(“listar_idcat.jsp”);
转发(请求、响应);
}
JSP:


window.onload=函数(){
document.formulario.submit()
};
document.close();
${c.idcategoria}

您似乎希望在GET请求中调用servlet。你走错路了。您不应该在加载页面时提交帖子表单。您应该在servlet的
doGet()
方法中执行作业,并直接调用它

您需要进行以下更改:

  • 将servlet的URL模式更改为
    /listar\u idcat

  • 将servlet的
    doPost
    方法重命名为
    doGet

  • listar\u idcat.jsp
    文件移动到
    /WEB-INF
    文件夹中(这样可以防止最终用户直接访问)

  • 更改
    getRequestDispatcher(“listar_idcat.jsp”)在servlet中调用
    getRequestDispatcher(“/WEB-INF/listar_idcat.jsp”)

  • 从JSP中删除整个

  • 从JSP中删除整个

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

    另见:
    • -你好,世界2号

    伙计,把你的问题格式化!并删除所有不必要的代码,以便我们只尝试理解与您的问题实际相关的部分。您能告诉我为什么尝试在页面加载时提交表单吗?我想要的是页面自动加载,而无需按钮使用“报价”按钮设置报价格式,并使用“代码”按钮设置代码格式。不要同时使用“报价”和“代码”按钮来格式化代码。更重要的是,在你的整个问题中,你没有任何引号,所以你根本不应该在整个问题中使用引号按钮。我注意到您甚至删除了所有
    字符,这使得代码在语法上无效。你不应该那样做。只需选择代码并按下代码按钮。这就是全部。真的。非常感谢你,朋友。最近我开始了网络java的世界,如果你能帮助我了解一些手册,朋友,谢谢。你好,巴卢斯,如果你能在组合框插入数据后给我一个listarme的想法。如果你有一个新的独立问题,你应该按
    问问题
    按钮。您不应在完全不同的问题上使用
    添加注释
    按钮。