Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2018
    Messaggi
    19

    Problema con la Serverlet

    Salve a tutti. Da poco ho iniziato a studiare le serverlet ma ho un problema. L'applicazione in questione dovrebbe ricevere una richiesta get e post da un form html. Se la richiesta e get fa il redirect su hello2. Ma quando fa il redirect, mi dice che hello2 non esiste. Dove sbaglio?
    codice:
    package itis.volta.classe5b;
    
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    /**
     *
     * @author 310-15-70IX
     */
    
    
    @WebServlet(urlPatterns = "/hello")
    public class PrimaServerlet extends HttpServlet 
    {
    
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
        {
            resp.getWriter().write("Query string: " + req.getQueryString() + "\n");
            resp.getWriter().write("Parameter: " + req.getParameter("username") + "\n");
        }
    
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
        {
            resp.sendRedirect("hello2");
    //        req.getRequestDispatcher("hello2").forward(req, resp);
            resp.getWriter().write("Query string: " + req.getQueryString() + "\n");
            resp.getWriter().write("Parameter: " + req.getParameter("username") + "\n");
        }
    }
    Mentre l'HTML è il seguente:
    codice:
    <!DOCTYPE html>
    <html>
        <head>
            <title>Start Page</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        </head>
        <body>
            <form action="hello" method="post">
                <h1> Metodo post </h1>
                <input type="text" name="username"/>
                <input type="submit" name="invio" value="Invia"/>
            </form>
            
            <form action="hello" method="get">
                <h1> Metodo get </h1>
                <input type="text" name="username"/>
                <input type="submit" name="invio" value="Invia"/>
            </form>
        </body>
    </html>

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da enzo2605 Visualizza il messaggio
    Ma quando fa il redirect, mi dice che hello2 non esiste. Dove sbaglio?
    La documentazione di sendRedirect dice chiaramente: If the location is relative without a leading '/' the container interprets it as relative to the current request URI.

    Quindi quel sendRedirect("hello2") è relativo al URI della request corrente.

    Se quella prima servlet risponde a http://nomehost/nomecontesto/hello allora per quel redirect ci dovrà essere qualcosa "mappato" su http://nomehost/nomecontesto/hello2
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2018
    Messaggi
    19
    Per qualcosa mappato intendete un urlPattern??

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da enzo2605 Visualizza il messaggio
    Per qualcosa mappato intendete un urlPattern??
    Sì, in uno dei qualunque modi possibili, tramite web.xml o annotation.
    Ma lo intendevo comunque in generale: qualunque cosa lato server che "risponde" a quel url.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.