使用 Servlet 控制器转发页面
servlet 就像:
String rootJspPath = "/WEB-INF/jsp"
String page = request.getParameter("pageName")!=null?request.getParameter("pageName").toString();
String forwardPage = rootJspPath + "/" + page ;
request.getRequestDispatcher(forwardPage).forward(request, response);
在 Web.xml 中
<servlet>
<servlet-name>ForwardServlet</servlet-name>
<display-name>ForwardServlet</display-name>
<servlet-class>common.ForwardServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ForwardServlet</servlet-name>
<url-pattern>/page</url-pattern>
</servlet-mapping>
现在使用 servlet 控制器调用转发,
<a href="/page/common/Welcome.jsp">click here</a>
请建议调用它的便捷方法。
servlet is like :
String rootJspPath = "/WEB-INF/jsp"
String page = request.getParameter("pageName")!=null?request.getParameter("pageName").toString();
String forwardPage = rootJspPath + "/" + page ;
request.getRequestDispatcher(forwardPage).forward(request, response);
in Web.xml
<servlet>
<servlet-name>ForwardServlet</servlet-name>
<display-name>ForwardServlet</display-name>
<servlet-class>common.ForwardServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ForwardServlet</servlet-name>
<url-pattern>/page</url-pattern>
</servlet-mapping>
now call forward using servlet controller
<a href="/page/common/Welcome.jsp">click here</a>
please suggest convenient way to call this..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议:
对于您的映射,
然后创建文件夹
common
并将Welcome.jsp
放入其中。问题不是最清楚:这符合你的要求吗?
编辑:此外,您可能应该对 req.getPathInfo() 进行一些路径验证,以避免人们尝试使用
..
字符进行目录跳跃,尽管据我所知,较新的 Java 应用程序服务器会捕获这种情况自动地变坏。I'd suggest:
for your mapping, and
and then create the folder
common
and putWelcome.jsp
in it.Question wasn't the clearest: does that do what you want?
EDIT: Also, you should probably do some path verification on req.getPathInfo() to avoid people trying to directory hop using
..
characters, though for all I know the newer Java app servers catch that sort of badness automatically.根据您的 servlet 正在执行的操作:
应该是
它将在
WEB-INF/jsp/common
文件夹中查找 Welcome.jsp。Based om what your servlet is doing:
should be
Which will look for Welcome.jsp in
WEB-INF/jsp/common
folder.