开发者

changing a href to forward method

How can I change normal link <a 开发者_StackOverflowhref="listNotes.jsp">List all entries</a> in JSP

to to forwarding method forward("listNotes.jsp", request, response);

protected void forward(String JSPFileName, HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.getRequestDispatcher(JSPFileName).forward(request, response);
}

?

some way using forms?


Just let the link URL match the URL pattern of the servlet

E.g.

<a href="forward/listnotes.jsp">

with a servlet which is mapped on an URL pattern of /forward/* and does the following job in the doGet() method, assuming that you'd like to hide the JSP file in /WEB-INF folder to prevent direct access:

request.getRequestDispatcher("/WEB-INF" + request.getPathInfo()).forward(request, response);


You can define a FORM and put your href tags inside that and connect the tag to a form submit. And in your form action, point it to a servlet. (Actually, you can even point your href directly to your servlet) IN the servlet doPost or doGet method, you can call the forward method.

So code will look something like

FORM action="/MyForwardServlet"

a href="#" action="/MyForwardServlet"

And in your MyFOrwardServlet.doPost

doPost(HttpServletRequest request ,HttpServletResponse response) {

// call your forward method or put that code inline here. }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜