使用 Servlet 控制器转发页面

发布于 2024-11-03 13:08:50 字数 863 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫九 2024-11-10 13:08:50

我建议:

<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>

对于您的映射,

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(req.getPathInfo());

    dispatcher.forward(req,resp);
}

然后创建文件夹 common 并将 Welcome.jsp 放入其中。

问题不是最清楚:这符合你的要求吗?

编辑:此外,您可能应该对 req.getPathInfo() 进行一些路径验证,以避免人们尝试使用 .. 字符进行目录跳跃,尽管据我所知,较新的 Java 应用程序服务器会捕获这种情况自动地变坏。

I'd suggest:

<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>

for your mapping, and

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(req.getPathInfo());

    dispatcher.forward(req,resp);
}

and then create the folder common and put Welcome.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.

淤浪 2024-11-10 13:08:50

根据您的 servlet 正在执行的操作:

<a href="/page/common/Welcome.jsp">click here</a>

应该是

<a href="/page?pageName=common/Welcome.jsp">click here</a>

它将在 WEB-INF/jsp/common 文件夹中查找 Welcome.jsp。

Based om what your servlet is doing:

<a href="/page/common/Welcome.jsp">click here</a>

should be

<a href="/page?pageName=common/Welcome.jsp">click here</a>

Which will look for Welcome.jsp in WEB-INF/jsp/common folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文