request.getRequestDispatcher is not working and shows no errors

Asked

Viewed 154 times

0

I have a problem in request.getRequestDispatcher("Valida.jsp"). forward(request, Response); from my non-forward Folder to the Valida.js page. I need to go from an HTML to a javascript (by Ajax) that passes the Infos to a Servlet and this Servlet saves the data in a Javabeans and foward to a jsp to show the data. No console or build error. It simply sits on the main html page.

My Servlet is like this:

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

    Beans beans = new Beans();
    String email = request.getParameter("email");
    String senha = request.getParameter("senha"); 
    String Pnome= request.getParameter("Pnome");
    String Mnome= request.getParameter("Mnome");
    String Snome= request.getParameter("Snome");

    beans.setEmail(email);
    beans.setSenha(senha);
    beans.setPnome(Pnome);
    beans.setMnome(Mnome);
    beans.setSnome(Snome);

    request.setAttribute("beans", beans);
    request.getRequestDispatcher("Valida.jsp").forward(request, response);
    System.out.println("passou do forward");
}

My JAVASCRIPT

count = 0;
onload = inicia;

function inicia() {

  document.getElementById("idCadastra").addEventListener("click", conecta, false);
}

function conecta() {

  var email = document.getElementById("idEmail").value;
  var senha = document.getElementById("idSenha").value;
  var Pnome = document.getElementById("idPrimeiroNome").value;
  var Mnome = document.getElementById("idNomeDoMeio").value;
  var Snome = document.getElementById("idSobreNome").value;

  if (email != "" && senha != "" && Pnome != "" && Snome != "") {
    console.log("entrou na conexão")
    httpRequest = createRequest();
    httpRequest.open("GET", "Servlet?email=" + email + "&senha=" + senha + "&Pnome=" + Pnome + "&Mnome=" + Mnome + "&Snome=" + Snome, true);
    httpRequest.onreadystatechange = function() {
      if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {

        }
      }
    }
    httpRequest.send(null);
  }

}
-------MEU HTML-------

<td class="td" align="right" width="300">
  Ainda não é usuário? Crie o seu Login:

  <form method="get">

    e-mail:
    <input id="idEmail" type="text" name="e-mail" required="required">*
    <br>Senha:
    <input id="idSenha" type="text" name="senha" required="required">*
    <br>Repita Senha:
    <input id="idRepitaSenha" type="text" name="repitasenha" required="required">*
    <br>Primeiro Nome:
    <input id="idPrimeiroNome" type="text" name="primeironome" required="required">*
    <br>Nome do Meio:
    <input id="idNomeDoMeio" type="text" name="nomedomeio">&nbsp&nbsp
    <br>Sobrenome:
    <input id="idSobreNome" type="text" name="sobrenome" required="required">*
    <br>

  </form>
  <button class="inputCriarLogin" id="idCadastra" name="criarlogin">Criar Login</button>

  • Vinicius, apparently everything is ok. You need to check the server log to see if you are making an exception. Some questions: what function createRequest(); does exactly? O Valida.jsp has to be at the same level of Servlet to work the way this way, always put /caminho-jsp/arquivo.jsp, in your case: /Valida.jsp. Place a println before giving the forward and check if it is at least getting inside the Servlet. console.log in the first line of the function onreadystatechange and check the exit.

  • Andrew, thank you for the reply. I withdrew the console log.() and println() of the code to make it easier to understand. I get no exception anywhere. I checked through println() and my Serrvlet is receiving all values from the fields in my html form. I even put a println before and after request.getRequestDispatcher("Valida.jsp")... to see if he was going through it and it’s all right. createRequest(); is part of the Ajax routine to ask to open a connection with Ajax. Something like.

  • If you are not making an exception at the time of giving the Dispatch and you are sure that you are getting inside your Servlet, it means that Servlet is finding the file and sending it correctly. Your httpRequest may not be receiving the server response correctly. Could you run a test with xmlhttprequest object? Check that you are receiving Response correctly.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.