Visualizzazione dei risultati da 1 a 10 su 12

Visualizzazione discussione

  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2015
    Messaggi
    5

    Problema invio dati di un form da Ajax alla servlet

    Buonasera, cercando e spulciando a lungo su internet non ho trovato nessuna risposta al mio problema, pertanto spero che voi possiate essermi di aiuto

    In pratica io ho dei dati di un form html da inviare con Ajax alla servlet, per maggiore chiarezza vi posto i pezzi di codice incriminato in modo che possiate indicarmi dove sto sbagliando:

    Questo è l'html del mio form:
    codice:
    <p id="myParp"> ciao </p>
    
                <!-- Creo il form -->
                <!-- action="mailto:juymiu@yahoo.it?subject=Yes" method="post" -->
                <form id="myForm" name="datiPerPosta" action="mailto:juymiu@yahoo.it?subject=Yes" method="post" enctype="text/plain" >
                
                    Username:
                    <input id="user" type="text" size="18" name="user" />
    
                    <br />
                    <br />
    
                    Inserire password:
                    <input id="pwd" type="password" size="18" name="password" />
                    
                    <br />
                    <br />
                    
                    Conferma password:
                    <input id="conferma" type="password" size="18" name="conferma" />
    
                    <br />
                    <br />
    
                    Città:
                    <input id="citta" type="text" name="citta" />
                    
                    <br />
                    <br />
    
                    Età:
                    <!-- onclick="verify();" -->
                    <input id="eta" type="text" />
                    <br />
                    <br />
    
                    <button id="send" onclick="verify();" type="submit">
                        invia
                    </button>
    
                </form>
    Questa è la mia chiamata Ajax:
    codice:
    console.log("Prima dell'ajax");
        
        $.ajax({
            type: 'POST',
            url: "http://localhost:8080/ServletCompito4/Main",
            data: "user=" + user + "&password=" + pwd + "&citta=" + citta + "&eta=" + eta,
            dataType: "html",
            success:function message(msg) {
                console.log("SUCCESS");
                $("myParp").html("SUCCESSO");
                
            },
            error:function message(msg) {
                console.log("ERROR");
                $("myParp").html("ERRORE");
                
            }
            
        });
        
        console.log("Dopo Ajax");
    Ho già controllato la correttezza dei parametri user, pwd, ecc

    Come server uso TomCat.
    Il codice della mia servlet è:
    codice:
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            BufferedReader buf = request.getReader();
            System.out.println(buf.readLine());
    
            str = "Sono dentro doGet ";
    
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println(str);
    
            System.out.println("CIAOOOOOOOOOOOONEEEEEE");
    
    
        }
    
        /**
         * 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 {
    
            BufferedReader buf = request.getReader();
            System.out.println(buf.readLine());
    
            str = "Sono dentro doPost";
    
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println(str);
    
            System.out.println("CIAOOOOOOOOOOOO");
    
            // processRequest(request, response);
        }
    Il codice del mio web.xml è:
    codice:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
        <servlet>
            <servlet-name>Main</servlet-name>
            <servlet-class>Main</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>Main</servlet-name>
            <url-pattern>/Main</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
    </web-app>
    Il mio index.jsp è:
    codice:
    <%-- 
        Document   : index
        Created on : 15-gen-2015, 18.04.57
        Author     : ale
    --%>
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <h1>Hello World!</h1>
        </body>
    </html>
    Ho i ldubbio che dipenda dal passaggio di data nella chiamata Ajax, ma non ho la più pallida idea di come farla.
    Grazie mille dell'attenzione
    Ultima modifica di axtml; 15-01-2015 a 21:25

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.