简体   繁体   English

如何在WEB-INF文件夹中重定向到JSP

[英]How to redirect to JSP inside WEB-INF folder

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的jsp,它还包含和带有以下元素的UL,如下面的代码所示,

<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. 我想要做的是在点击LI时将此页面重定向到相应的jsp页面。 Now since all the pages are inside WEB-INF Folder, I cant figure out how to do so. 现在由于所有页面都在WEB-INF文件夹中,我无法弄清楚如何这样做。 I dont want to create a jsp out side WEB-INF and then put servlet redirection code in it. 我不想创建一个jsp out side WEB-INF,然后将servlet重定向代码放入其中。 Thanks in advance. 提前致谢。

What can I use here ? 我可以在这里使用什么?

PS: Started web development few months before. PS:几个月前开始进行Web开发。 Thanks in advance. 提前致谢。

You need to invoke a servlet through href on the LI. 您需要通过LI上的href调用servlet。

In the servlet, you need to use requestdispatcher to redirect to your jsp 在servlet中,您需要使用requestdispatcher重定向到您的jsp

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

=================EDIT : Sample Code ========================================= =================编辑:示例代码============================= ============

Index.html 的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 Servlet代码

@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 : Jsp位置:

WEB-INF/sample.jsp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM