How to open a JSP inside a JSP through a Servlet?

3

I'm using a Script, but I'm not using Servlet to do the desired action inside my index.jsp page, and do not use this Script below to open a JSP file inside my index.jsp home page.

  <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

    <script>
        $(document).ready(function() {
            var domContent = $("#content");

            $(".nav-link").click(function() {
                domContent.load($(this).attr("href"));
                return false;
            });
        });
    </script>

<li class="topmenu"><a class="nav-link" href="cad_conta.jsp" title="Cadastrar uma conta" style="height:19px;line-height:19px;">Cadastro</a></li>
  • I already have a class Servlet() with method doGet() and doPost() .
  • Within my Servlet() class, can I open a JSP file on my home page without having to use a Script JQuery using some method within the Servlet() class?
  • asked by anonymous 21.06.2015 / 16:54

    1 answer

    2

    The correct one in a web application is always to create a class servlet every jsp created, to deal with your requests and answers.

    In your case you already have a servlet , you should create a new servlet corresponding to the jsp you want to call, examples: jspCadastro , ServletCadastro .

    As for the method, you can create a doGet method that will be responsible for calling your jsp using RequestDispacher . Example:

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    req.getRequestDispatcher("/WEB-INF/paginas/jspCadastro.jsp").forward(req, resp);
    }
    

    In your main%% of%, you only add a button by passing the path of your jsp responsible for that servlet . Example:

    <button onclick="window.location=' ServletCadastro '">Cadastrar</button>
    

    calling its jsp by method servlet through doGet .

        
    21.11.2015 / 03:46