首页 > 解决方案 > getRequestDispatcher dispatching jsp but not html

问题描述

I am running google app engine and using JAVAEE.

I have a two filters but only one is configured with restrictions.

WEB.XML

<?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. I first login so I can go trough the first filter.
  2. I change my url to localhost:8080/client

my connexion filter is as so:

    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

    }

    }

my servlet is as so:

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);;
    }
}

when my file is setted to test.jsp I go to the correct page, but when I rename it to test.html and of course change the path in getRequestDispatcher I get redirected to my login page (/ route).

So I have to files /restrict/client/test.jsp i get the correct response and /restrict/client/test.html I go back to the signin page.

I've tried putting a simple hello in test.html also tried with valid html page with the same result. Is there any settings I'm missing in order to allow html?

Where could it come from? Any help is greatly appreciated.

标签: htmljspgoogle-app-enginejakarta-ee

解决方案


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

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

为了更好地理解,您可以检查

您可以将没有 .jsp 扩展名的文件呈现为 JSP 吗?

将 .jsp 作为 .html 文件运行


推荐阅读