How to redirect to JSP inside WEB-INF folder

前端 未结 1 599
太阳男子
太阳男子 2021-01-06 11:06

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

1条回答
  •  忘掉有多难
    2021-01-06 11:53

    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

    
    

    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
    

    0 讨论(0)
提交回复
热议问题