Can not cast from Object to JSP

1

I am developing an application using JSP and servelt in which I want to get the total amount of records from my tb_motorist table, I created a servelt in which I passed the method that makes the query and created a session but when I get the session created in the servlet and put in the pageMotorista2 list it appears the following error. An error occurred at line: 47 in the jsp file: /listaMotoristas2.jsp Cannot cast from Object to int

 public int totalRegistros(){

     try {
        con = Conecta.conexao();
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Conecta.class.getName()).log(Level.SEVERE, null, ex);
    }


            try {

       String sql="Select count(*) as contaRegistros from tb_motorista";
        Statement statement = con.createStatement();     

       ResultSet rs = statement.executeQuery(sql);    
        rs.next();
        int totalRegistros=Integer.parseInt(rs.getString("contaRegistros"));
        return totalRegistros;

    } catch (Exception e) {
        JOptionPane.showConfirmDialog(null,e);

}
            return 0;
}

 protected void processRequest(HttpServletRequest request, 
 HttpServletResponse response)
        throws ServletException, IOException {

     MotoristasDAO dao= new MotoristasDAO();

     try {

          int totalMotorista=dao.totalRegistros();

     request.setAttribute("totalMotoristas", totalMotorista);

     RequestDispatcher rd= 
   request.getRequestDispatcher("/listaMotoristas2.jsp");
    rd.forward(request, response);

    } catch (Exception e) {
    }

   }

            <%

    int totalRegistros= (int) request.getAttribute("totalMotorista");//Cannot cast from Object to int


          %> 
    
asked by anonymous 13.09.2017 / 00:12

1 answer

2
request.setAttribute("totalMotoristas", totalMotorista);

The parameters of HttpServletRequest.setAttribute () are String and Object. totalMotorist is int, which is not Object but a primitive type.

Change to Integer in points:

- public Integer totalRegistros(){
- Integer totalRegistros=Integer.parseInt(rs.getString("contaRegistros"));
- Integer totalMotorista=dao.totalRegistros();
    
13.09.2017 / 19:44