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

Asked

Viewed 888 times

3

I am using a Script but I am not using Servlet to do the desired action within my page index.jsp, the goal is not to use this Script below to open a JSP file inside my home page index.jsp.

  <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>
  1. I already own a class Servlet() methodically doGet() and doPost().
  2. Inside of my class Servlet(), I can open a JSP file on my homepage without having to use a Script JQuery using some method within the class Servlet()?.

1 answer

2

The correct thing in a web application is to always create a class servlet each jsp created to handle your requests and responses.

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

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

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

Already in his jsp main is just you add a button passing the path of your servlet responsible for doing so jsp. Example:

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

calling his servlet by the method doGet through the RequestDispatcher.

  • <button onclick="window.Location=' Servletregister '">Register</button>

Browser other questions tagged

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