getRequestDispatcher调度jsp但不调用html

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了getRequestDispatcher调度jsp但不调用html相关的知识,希望对你有一定的参考价值。

我正在运行谷歌应用引擎并使用JAVAEE。

我有两个过滤器,但只有一个配置了限制。

VEB.HML

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <filter>
        <filter-name>connexionFilter</filter-name>
        <filter-class>AHSFilters.connexionFilter</filter-class>
    </filter>
    <filter>
        <filter-name>restrictFilter</filter-name>
        <filter-class>AHSFilters.restrictFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>connexionFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>restrictFilter</filter-name>
        <url-pattern>/client</url-pattern>
        <url-pattern>/admin</url-pattern>
    </filter-mapping>
</web-app>
  1. 我首先登录,所以我可以通过第一个过滤器。
  2. 我将我的网址更改为localhost:8080/client

我的连接过滤器是这样的:

    package AHSFilters;

    import java.io.IOException;

    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;

    public class connexionFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        HttpSession session = req.getSession();

        if(session.getAttribute("sessionUser") != null)
            chain.doFilter(req, resp);
        if (req.getHeader("X-Requested-With") != null)
            chain.doFilter(req, resp);
        else
            req.getServletContext().getRequestDispatcher("/").forward(request, response);
        // TODO Auto-generated method stub

    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    }

我的servlet是这样的:

package AHSServlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(
        name = "ClientServ",
        urlPatterns = {"/client"}
    )
public class ClientServ extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.getWriter().flush();
        this.getServletContext().getRequestDispatcher("/restrict/client/test.jsp").forward(request, response);;
    }
}

当我的文件被设置为test.jsp时,我转到正确的页面,但是当我将其重命名为test.html并且当然更改getRequestDispatcher中的路径时,我被重定向到我的登录页面(/路由)。

所以我必须文件/restrict/client/test.jsp我得到正确的答复和/restrict/client/test.html我回到登录页面。

我已经尝试在hello中放置一个简单的test.html,也尝试使用有效的html页面,结果相同。为了允许html,我有什么设置吗?

哪里可以来的?任何帮助是极大的赞赏。

答案

我认为为了做到这一点,你需要很少的配置。在web.xml中编写配置代码

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.html</url-pattern>
    </jsp-property-group>
</jsp-config>

为了更好地理解你可以检查

Can you render a file without a .jsp extension as a JSP?

Running .jsp as .html file

以上是关于getRequestDispatcher调度jsp但不调用html的主要内容,如果未能解决你的问题,请参考以下文章

JSP的getRequestDispatcher()与sendRedirect()的区别

request.getRequestDispatcher("**.jsp").forward(request,response);乱码的解决

response.sendRedirect()与request.getRequestDispatcher("/index.jsp").forward(request, respon

关于项目在网页中运行部分jsp出现乱码(由request.getRequestDispatcher("XXX.jsp").forward(request, response)造成)

servlet中request.getrequestDispatcher()的路径问题

JSP标签