0

i'm tryng to head out this problem but i can't figure it out. So i have my Servlet and my jsp page, but when i run the server and i click on the link that should trigger the logic for my jsp, just nothing happen. Here is my code:

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template
 */
package it.unica.fSociety.Servlet;
import it.unica.fSociety.Model.Videogioco;
import it.unica.fSociety.Model.VideogiocoFactory;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author stefa
 */
@WebServlet(name = "VideogiochiServlet", urlPatterns = {"/listinoVideogiochi"})
public class VideogiochiServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        List<Videogioco> videogiochi = VideogiocoFactory.getInstance().getAllVideogiochi();
        request.setAttribute("listinoVideogiochi", videogiochi);
        request.getRequestDispatcher("listinoVideogiochi.jsp").forward(request,response);


        
        
    
     
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

The Servlet

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package it.unica.fSociety.Model;

import it.unica.fSociety.Database.DatabaseManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author stefa
 */
public class VideogiocoFactory {

    private static VideogiocoFactory instance;
    
    private VideogiocoFactory(){}
    
    public static VideogiocoFactory getInstance(){
         if(instance== null)
            instance= new VideogiocoFactory();
        return instance;
    }

    public List<Videogioco> getAllVideogiochi() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet set = null;
        List<Videogioco> videogiochi = new ArrayList<>();

        try {
            conn = DatabaseManager.getInstance().gedDbConnection();
            String query = "select * from videogioco";
            stmt = conn.prepareStatement(query);
            set = stmt.executeQuery();
            while (set.next()) {
                Videogioco videogioco = new Videogioco();
                videogiochi.add(videogioco);
       
            }
            return videogiochi;
        } catch (SQLException e) {
            Logger.getLogger(VideogiocoFactory.class.getName()).log(Level.SEVERE, null, e);
        } finally {
            try {set.close();   } catch (Exception e) {}
            try {stmt.close();   } catch (Exception e) {}
            try {conn.close();   } catch (Exception e) {}
        }
        return null;
    }
}
<!DOCTYPE html>

<html>
    
    <head>
        
        <title> GameSociety </title>
        <meta charset="UTF-8">
        <meta name ="author" content ="fSociety">
        <meta name =" description " content =" Negozio online di videogiochi">
        <meta name =" keywords" content ="videogiochi,pc,games">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="preconnect" href="https://fonts.googleapis.com/" >
        <link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Mukta:wght@200&display=swap" rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="style.css" media="screen"/>
    </head>
    
    <c:set var="page" value="index" scope="request"/>
     
    <c:if test="${empty utente.getUsername()}">
        <c:redirect url="login.jsp"/>
    </c:if>
    <c:if test="${not empty utente.getUsername()}">
        <c:set var="page" value="listinoVideogiochi" scope="request"/>
    </c:if>
    
     
    
    
    
    <body>
        <%@ include file="header.jsp" %>
        
        <nav>
            <div>
                <ul>
                    <li><a href="index.jsp" > Home </a></li>
                    <li><a href ="nuovoProdotto.jsp"> Nuovo Videogame </a></li>
                    <li><a href ="listinoVideogiochi.jsp" class="active"> Listino Videogiochi </a></li>
                    <li><a href ="chiSiamo.jsp"> Chi siamo  </a></li>
                    <li><a href ="login.jsp"> Login </a></li>
                </ul>
            </div>
        </nav>
        <br>
        <div class="col-3 col-s-3">

            <%@ include file="pubblicità1.jsp" %>
            <div id="bookmark1" class="presentazioneListino">
                Acquista i tuoi giochi preferiti, o trovane di nuovi,comodamente
                da casa e scaricali subito!
                Qui troverai tutti i videogiochi in vendita nel nostro sito. <br>
                Scegli tra quelli più ricercati dagli utenti. Solo il 
                meglio del mondo games scelto per voi dallo staff di GameSociety. 
                <br> Inizia a fare acquisti!
            </div>
            <br>
            <h2 id="titoloh2">Tutti i titoli</h2>
             
            <article>
                <div class="boxListino">
                    <c:forEach items="${listinoVideogiochi}" var="videogiochi">
                    <h3 class="titoloListino"> "${videogiochi.getTitolo()}"</h3>
                 

                    <img title ="BlackMesa" alt="Foto del gioco di Black Mesa" src="${videogiochi.getUrl_Foto(

                    
                    <div class="infoGioco">
                        <b>Genere:</b> <i> "${videogiochi.getGenere()}"</i>
                        <br>
                        <b>Trama:</b> Sei Gordon Freeman, un fisico teorico del Black Mesa 
                        Research Facility. Quando un esperimento di routine va 
                        terribilmente storto, ti ritrovi a combattere un'invasione 
                        aliena interdimenzionale. Riuscirai a salvare la tua squadra 
                        scientifica e il mondo??
                        <br>
                        <b>Prezzo:</b> 14,99€ 
                        <br>
                        <b>Produttore:</b> Crowbar Collective.
                        <br>
                        <b>Data di Rilascio:</b> 6 Marzo 2020.
                    </div>


                    <h4 class="titoloh4">Requisiti di Sistema:</h4>
                    <div class="tabella">
                        <table>
                            <tr>             
                                <th></th>
                                <th>Minimi:</th>      
                                <th>Consigliati:</th>
                            </tr>
                            <tr>
                                <td><b>Sistema operativo:</b></td>
                                <td><i>Windows 7(32/64-bit)/Vista or Greater.</i></td>  
                                <td><i>Windows 7(32/64-bit) or Greater.</i></td>
                            </tr>
                            <tr>
                                <td><b>Processore: </b></td>
                                <td><i>2.6 Dual Core Processor or Greater.</i></td>
                                <td><i>3.2 Quad Core Processor or Greater.</i></td>
                            </tr>
                            <tr>
                                <td><b>Memoria: </b></td>
                                <td><i>6 GB di RAM.</i></td>
                                <td><i>8 GB di RAM.</i></td>
                            </tr>
                            <tr>
                                <td><b>Scheda Video: </b></td>
                                <td><i>2 GB Dedicated Video Card or Greater.</i></td>
                                <td><i>4 GB Dedicated Video Card or Greater.</i></td>
                            </tr>
                        </table>
                    </div>


                    <div class="votoGioco">
                        <i>Ti è piaciuto questo gioco?</i> 
                        <button type ="submit">
                            <img alt="sì" src="img/si.png" width="14" height="14" /> Sì! 
                        </button>
                        <button type ="submit">
                            <img alt="no" src="img/no.png" width="14" height="14" /> No! 
                        </button>
                    </div>
                </div>
                    </c:forEach>
            </article>

         

            </article>
            <a href ="#bookmark1"> <b>Torna su!</b></a>
        </div>
        <%@ include file="pubblicità2.jsp" %>
        <%@ include file="footer.jsp" %>
    </body>
</html>


the jsp

Thanks everyone for the support

akarnokd
  • 69,132
  • 14
  • 157
  • 192
erica
  • 1
  • 1
  • What's the name of your servlet? Did you put the right URL into your browser's search bar? What do you see with Page > Source in the browser? Is there an error log on the server's console or log file? – akarnokd Jun 16 '22 at 17:49
  • You reference `utente.getUsername()` in the JSP but I don't see JSP code that would obtain the object `utente` from somewhere. – akarnokd Jun 16 '22 at 17:51
  • @akarnokd yeah, i put the right url, no errors, just it doesn't works – erica Jun 16 '22 at 18:00
  • Okay, what does the View Source get you in the browser? A partial html page or completely empty response? – akarnokd Jun 16 '22 at 18:01
  • @akarnokd also, utente.getUsername is a session variable, and it works, my problem is with listinoVideogiochi, there are the snippets of the code – erica Jun 16 '22 at 18:01
  • If you address `listinoVideogiochi.jsp` in the browser directly, do you get something? – akarnokd Jun 16 '22 at 18:03
  • @akarnokd it seems like an empty list, i don't see anything but my navbar and the title "TITOLI VIDEOGIOCHI" , i have data in the db, and other records is being retrieved – erica Jun 16 '22 at 18:03
  • No just nothing – erica Jun 16 '22 at 18:04
  • Okay. Are you using a local servlet container? If so, restart it in debug mode, put a breakpoint into `processRequest` and one into `listinoVideogiochi.jsp` and step through both to see what happens. – akarnokd Jun 16 '22 at 18:07
  • @akarnokd i have already tried but my logic is never triggered – erica Jun 16 '22 at 18:14
  • Which logic? processRequest ? – akarnokd Jun 16 '22 at 18:15
  • When i put the breakpoint in request.setAttribute yes is never triggered – erica Jun 16 '22 at 18:18
  • Under what URL did you try to access `/listinoVideogiochi`? – akarnokd Jun 16 '22 at 18:19
  • I tried to access to localhost/ServletModule/listinoVideogiochi – erica Jun 16 '22 at 18:22
  • Try putting a breakpoint into `doGet` and `doPost`. If those don't trigger, let's start from square one. Create some basic `helloworld.jsp` in the webroot and try accessing it via localhost. – akarnokd Jun 16 '22 at 18:24
  • I have access to other jsps, they work correctly and it’s like the VideogiochiServlet isn’t working. – erica Jun 16 '22 at 18:27
  • Did you try the breakpoints in `doGet` and `doPost`? – akarnokd Jun 16 '22 at 18:29
  • Yes, i have tried and seams like the logic is never triggered – erica Jun 16 '22 at 18:32
  • I don't know then. There isn't much I can do through comments without seeing or debugging your entire project myself. If there are no errors in the log, including from the very start of the server, nor do you get 404 browser error, it could be anything. – akarnokd Jun 16 '22 at 18:36
  • Yeah, no errors, just nothing, it’s like my Servlet isnt starting when i go in the url – erica Jun 16 '22 at 18:43
  • If `jsp` files work, why don't you put the servlet's videogiochi retrieval directly into the `listinoVideogiochi.jsp` as a `<% %>` code block? Then whatever called the servlet, point it to the jsp. – akarnokd Jun 16 '22 at 18:46
  • What do you mean? I didn’t understand sorry – erica Jun 16 '22 at 18:47
  • You know you can put java code into `jsp`s via `<% %>` blocks? https://www.baeldung.com/jsp#syntax – akarnokd Jun 16 '22 at 18:51
  • Yes, good idea, srry now i try and i keep you updated – erica Jun 16 '22 at 18:52
  • @akarnokd i've tried, and with this method it works, i have my data. But it's not the way i want to do it, so have you any idea of how to do it with a servlet? – erica Jun 16 '22 at 20:41
  • I've never done servlets via annotation, only the traditional web.xml way. Maybe this is your problem https://stackoverflow.com/a/26247021/61158 ? – akarnokd Jun 16 '22 at 20:43
  • Sadly i'm not using Tomcat but Glassfish, if i used Spring and Tomcat it would be surely easier ahaha, but thank you the same – erica Jun 16 '22 at 20:50
  • Can't you check/make sure you are using Servlet 3.0 or newer? Btw, are any of your other `@WebServlet`s working? – akarnokd Jun 16 '22 at 20:51
  • https://stackoverflow.com/a/30778598/61158 – akarnokd Jun 16 '22 at 20:55
  • Yeah i have other WebServlets and they works, but they're triggered by buttons, this one is triggered by a link in my navbar, and yes i have Servlet 3.0 – erica Jun 16 '22 at 20:56
  • How do your other `@WebServlet` annotations look like? – akarnokd Jun 16 '22 at 20:59
  • Like this "@WebServlet(name = "NuovoProdotto", urlPatterns = {"/inserimentoProdotto"}) ", where inserimentoProdotto is the name of the form – erica Jun 16 '22 at 21:04
  • Do you have any servlet filters? I'd try to create a fresh servlet from scratch, name it "A" with urlpattern "/A" and see if its doXXX methods get called or not. If it works, start adjusting naming and see where it breaks. – akarnokd Jun 16 '22 at 21:07
  • How can i check if i have servlet filters? – erica Jun 16 '22 at 21:10
  • It's different now, even with the name the jsp is reached, but when i try to have access to the attribute that i set, it is null – erica Jun 16 '22 at 21:11
  • Something extends `javax.servlet.Filter` in your project or via some 3rd party library. Sometimes access right checks are enforced via filters. – akarnokd Jun 16 '22 at 21:13
  • I checked but nothing, it's like setAttribute and forward() isnt working – erica Jun 16 '22 at 21:18
  • How do you read the attribute from the request? https://stackoverflow.com/a/57370840/61158 – akarnokd Jun 16 '22 at 21:22
  • You can read it in the Jsp im trying to read it in the jsp using ${listinoVideogiochi}, with this syntax – erica Jun 16 '22 at 21:28
  • Maybe your attribute name `listinoVideogiochi` is conflicting with the servlet name or some scope variable? Try naming it something else. – akarnokd Jun 16 '22 at 21:34
  • Nothing. I've tried but nothing, i'm going crazy with this ahaha – erica Jun 16 '22 at 21:40
  • I don't know. At this point, I'd create a minimal project with just code and steps of the original problem to see if it works or not in isolation. – akarnokd Jun 16 '22 at 21:52
  • Somehow i solved, i'm so stupid because i've seen just now that i was accessing to listinoVIdeogiochi.jsp and not /listinoVideogiochi as i have to, so now it works, thank you a lot for the support, i don't know how to vote but if you tell me how i'll give you 10/10 – erica Jun 16 '22 at 21:59

0 Answers0