Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9297749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:09:01+00:00 2026-06-18T22:09:01+00:00

I’m having a problem viewing my records from an arraylist in JSP page. Every

  • 0

I’m having a problem viewing my records from an arraylist in JSP page. Every time I load my JSP page automatically by javascript onload event, data is displayed but the process does not stop.

Categoria class:

package proyecto.modelo;

public class Categoria {

    private int idcategoria;

    public int getIdcategoria() {
        return idcategoria;
    }

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

BaseDAO class:

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 class:

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 class:

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() method:

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:09:02+00:00Added an answer on June 18, 2026 at 10:09 pm

    You seem to want to invoke a servlet on a GET request. You’re approaching this the wrong way. You should not be submitting a POST form on load of the page. You should be performing the job in the doGet() method of the servlet and be invoking it directly.

    You need to make the following changes:

    1. Change URL pattern of servlet to /listar_idcat.

    2. Rename doPost method of servlet to doGet.

    3. Move listar_idcat.jsp file into /WEB-INF folder (this prevents direct access by endusers).

    4. Change getRequestDispatcher("listar_idcat.jsp"); call in servlet to getRequestDispatcher("/WEB-INF/listar_idcat.jsp");.

    5. Remove the whole <script> from JSP.

    6. Remove the whole <form> from JSP.

    Now, open the JSP by http://localhost:8080/context/listar_idcat instead (yes, without the .jsp extension! it’ll invoke the servlet’s doGet() directly).

    See also:

    • Our Servlets wiki page – Hello World #2.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I've tracked down a weird MySQL problem to the two different ways I was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.