将 href 更改为转发方法
如何更改正常链接 列出所有条目
在 JSP 中
转发方法 forward("listNotes.jsp", request, response);
protected void forward(String JSPFileName, HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher(JSPFileName).forward(request, response);
}
?
使用表格的某种方式?
How can I change normal link <a href="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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需让链接 URL 与 servlet 的 URL 模式匹配
即可
使用映射到 URL 模式
/forward/*
的 servlet,并在doGet()
方法中执行以下工作,假设您想隐藏/WEB-INF
文件夹中的 JSP 文件以防止直接访问:Just let the link URL match the URL pattern of the servlet
E.g.
with a servlet which is mapped on an URL pattern of
/forward/*
and does the following job in thedoGet()
method, assuming that you'd like to hide the JSP file in/WEB-INF
folder to prevent direct access:您可以定义一个 FORM 并将 href 标签放入其中,并将该标签连接到表单提交。在您的表单操作中,将其指向 servlet。 (实际上,您甚至可以将 href 直接指向您的 servlet) 在 servlet doPost 或 doGet 方法中,您可以调用forward 方法。
所以代码看起来像
FORM action="/MyForwardServlet"
a href="#" action="/MyForwardServlet"
并且在你的 MyFOrwardServlet.doPost
doPost(HttpServletRequest request ,HttpServletResponse 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.
}