How to redirect to JSP inside WEB-INF folder

23,581

You need to invoke a servlet through href on the LI.

In the servlet, you need to use requestdispatcher to redirect to your jsp

RequestDispatcher dispatcher=getServletContext().getRequestDispatcher( "/WEB-INF/sample.jsp" );
dispatcher.forward( request, response );

=================EDIT : Sample Code =========================================

Index.html

<nav>
     <ul>
        <li class="current"><a href="/DynamicTest/MyServlet">Home</a></li>
        <li><a>Access Control</a></li>
        <li><a>Site Administration</a></li>
        <li><a>Dashboard</a></li>
        <li><a>Visitor Management</a></li>
      </ul>                
</nav>

Servlet code

@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher dispatcher = getServletContext()
                .getRequestDispatcher("/WEB-INF/sample.jsp");
        dispatcher.forward(request, response);
    }

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

}

Jsp location :

WEB-INF/sample.jsp
Share:
23,581
Haris Mehmood
Author by

Haris Mehmood

Love to code, Love to learn, Love to work hard, Love to explore new technologies. So much in love with Java web/desktop development. Love to work on Tomcat, MariaDB, phpMyadmin, apache2, Ubuntu. Want to learn everything related to IT.

Updated on July 09, 2022

Comments

  • Haris Mehmood
    Haris Mehmood almost 2 years

    I have a jsp with a NAV in it, which further contains and UL with the following elements as shown in the code below,

    <nav>
         <ul>
            <li class="current"><a>Home</a></li>
            <li><a>Access Control</a></li>
            <li><a>Site Administration</a></li>
            <li><a>Dashboard</a></li>
            <li><a>Visitor Management</a></li>
          </ul>                
    </nav>
    

    What I want to do is to redirect this page to the corresponding jsp page whenever an LI is clicked. Now since all the pages are inside WEB-INF Folder, I cant figure out how to do so. I dont want to create a jsp out side WEB-INF and then put servlet redirection code in it. Thanks in advance.

    What can I use here ?

    P.S: Started web development few months before. Thanks in advance.

  • Haris Mehmood
    Haris Mehmood about 10 years
    now to redirect to corresponding page we have to pass arguments in the Servlet URL right ? can you explain that a bit too
  • Haris Mehmood
    Haris Mehmood about 10 years
    Thank you I have figured it out, thank you very much.Please upvote the post so that newbees can get help. Thanks again :)