Redirect Servlet to JSP by passing data

Asked

Viewed 249 times

1

I’m doing a library project, I register Book, Publisher and Parents and then I display them on JSP. When I register the data appear normally in JSP but if I refresh the page they are re-registered. I have never used Servlet and JSP before.

Servlet:

package br.com.wesley.libna.controller;

import br.com.wesley.libna.database.CadastrarDataBase;
import br.com.wesley.libna.database.Conexao;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "Carregar", urlPatterns = {"/Carregar"})
public class Carregar extends HttpServlet {

private final CadastrarDataBase cad;

public Carregar() {
    this.cad = new CadastrarDataBase();
}

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Conexao con = new Conexao();

    con.conectar();
    RequestDispatcher rd = null;

    if (con.estaConectado()) {
        ArrayList<String> paises = this.listaPaises(con);
        ArrayList<String> editoras = this.listaEditoras(con);

        request.setAttribute("paises", paises);
        request.setAttribute("editoras", editoras);
        request.setAttribute("resultado", false);
        rd = request.getRequestDispatcher("cadastrar.jsp");
    }

    rd.forward(request, response);
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

public ArrayList<String> listaPaises(Conexao con) {
    cad.processarListaDePaises(con.getStm());
    return cad.getPais();
}

public ArrayList<String> listaEditoras(Conexao con) {
    cad.processarListaDeEditoras(con.getStm());
    return cad.getEditora();
}

}

JSP:

<%@page import="br.com.wesley.libna.database.Conexao"%>
<%@page import="br.com.wesley.libna.database.CadastrarDataBase"%>
<%@page import="java.util.ArrayList"%>
<%
    ArrayList<String> paises = (ArrayList) request.getAttribute("paises");
    ArrayList<String> editoras = (ArrayList) request.getAttribute("editoras");
%>
<html>
    <head>
        <title>Cadastrar | Libna</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script type="text/javascript" src="js\jquery-3.3.1.min.js"></script>
        <script type="text/javascript" src="js\bootstrap.min.js"></script>
        <link rel="stylesheet" type="text/css" href="css\style.css" />
        <link rel="stylesheet" type="text/css" href="css\bootstrap.min.css" />
    </head>
    <body onload="exibir_ocultar(this)">
        <nav class="navbar navbar-expand-md bg-dark navbar-dark">
            <a class="navbar-brand" href="#">Libna</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse"
                    data-target="#collapsibleNavbar">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="collapsibleNavbar">
                <ul class="navbar-nav">
                    <li class="nav-item"><a class="nav-link" href="cadastrar.jsp">Cadastrar</a></li>
                    <li class="nav-item"><a class="nav-link" href="#">Consultar</a></li>
                    <li class="nav-item"><a class="nav-link" href="sobre.jsp">Sobre</a></li>
                </ul>
            </div>
        </nav>
        <div class="container">
            <div class="row coluna">
                <%
                    boolean resultado = (Boolean) request.getAttribute("resultado");
                    if (resultado) {
                        boolean inseriu = (Boolean) request.getAttribute("inseriu");
                        if (inseriu) {
                            out.println("<div class='alert alert-success alert-dismissible col-md-12'>");
                            out.println("<button type='button' class='close' data-dismiss='alert'>&times;</button>");
                            out.println("<strong>" + request.getAttribute("frase") + "</strong>");
                            out.println("</div>");
                        } else {
                            out.println("<div class='alert alert-danger alert-dismissible col-md-12'>");
                            out.println("<button type='button' class='close' data-dismiss='alert'>&times;</button>");
                            out.println("<strong>" + request.getAttribute("frase") + "</strong>");
                            out.println("</div>");
                        }
                    }
                %>
            </div>
            <div class="row coluna">
                <div class="col-md-12">
                    <h2 class="text-success">O que deseja cadastrar?</h2>
                </div>
            </div>
            <div class="row coluna">
                <div class="checkbox col-md-12">
                    <select class="form-control" id="tipoCadastro" name="tipoCadastro" onchange="exibir_ocultar(this)">
                        <option>Livro</option>
                        <option>Pais</option>
                        <option>Autor</option>
                        <option>Editora</option>
                    </select>
                </div>
            </div>
            <div class="row view-pais coluna" id="view-pais">
                <div class="col-md-12">
                    <form action="Pais" method="POST">
                        <div class="form-group">
                            <label for="nome">Nome:</label>
                            <input type="text" class="form-control" id="nome" placeholder="Digite o nome do pais" name="nome">
                        </div>
                        <button type="submit" class="btn btn-primary">CADASTRAR</button>
                    </form>            
                </div>
            </div>
            <div class="row view-editora coluna" id="view-editora">
                <div class="col-md-12">
                    <form action="Editora" method="POST">
                        <div class="form-group">
                            <label for="email">Nome:</label>
                            <input type="text" class="form-control" id="nome" placeholder="Digite o nome da editora" name="nome">
                        </div>
                        <div class="form-group">
                            <label for="nomePais">Pais:</label>
                            <select class="form-control" id="nomePais" name="nomePais">
                                <%
                                    for (String p : paises) {
                                        out.print("<option>" + p + "</option>");
                                    }
                                %>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-primary">CADASTRAR</button>
                    </form>
                </div>
            </div>
            <div class="row view-editora coluna" id="view-livro">
                <div class="col-md-12">
                    <form action="Editora" method="POST">
                        <div class="form-group">
                            <label for="email">Nome:</label>
                            <input type="text" class="form-control" id="nome" placeholder="Digite o nome do livro" name="nome">
                        </div>
                        <div class="form-group">
                            <label for="nomePais">Pais:</label>
                            <select class="form-control" id="nomePais" name="nomePais">
                                <%
                                    for (String p : paises) {
                                        out.print("<option>" + p + "</option>");
                                    }
                                %>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="nomePais">Editora:</label>
                            <select class="form-control" id="nomeEditora" name="nomeEditora">
                                <%
                                    for (String e : editoras) {
                                        out.print("<option>" + e + "</option>");
                                    }
                                %>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-primary">CADASTRAR</button>
                    </form>
                </div>
            </div>
        </div>
    </body>
    <script type="text/javascript">
        function exibir_ocultar() {
            var valor = $("#tipoCadastro").val();

            $('#view-editora').hide();
            $('#view-pais').hide();
            $('#view-livro').hide();

            if (valor == "Pais") {
                $('#view-pais').show();
            } else if (valor == 'Editora') {
                $('#view-editora').show();
            } else if (valor == 'Livro') {
                $('#view-livro').show();
            }
        }
    </script>
</html>
  • Maybe the problem is rd.forward: https://www.codebyamir.com/blog/java-servlet-essentials-sendredirect-vs-forward

  • using Response.sendRedirect() the data will pass ?

No answers

Browser other questions tagged

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