1

我有home.jsp页面和联系页面,我们关心的是home.jsp页面如下:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="javax.faces.context.FacesContext" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<f:view>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
    <title>home</title>
  </head>
  <body>

<%   
    response.sendRedirect("contact.jsp");
%>
  </body>

</html>
</f:view>

web.xml文件代码:

<?xml version = '1.0' encoding = 'windows-1252'?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/home.jsp</url-pattern>
  </servlet-mapping>
</web-app>

当我加载 home.jsp 它返回空白页而不是 contact.jsp

4

1 回答 1

2

首先,您真的不想在任何页面中包含此代码

<%   
    response.sendRedirect("something.jsp");
%>

如果您想在访问“home.jsp”时重定向到“contact.jsp”,您应该在过滤器中进行。

public class MyAppFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
}

    public void destroy() {
}

    public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain filterChain) throws IOException, ServletException {
        if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)) {
            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
            HttpServletResponse httpServletResponse = (HttpServletResponse) respo
            String requestPath = httpServletRequest.getRequestURI();
            //check if the URL contains <anything>/home.jsp
            if (requestPath.contains("home.jsp")) {
                 //if that's the case, redirect to another page
                 httpServletResponse.sendRedirect("/SportABAWeb/jsf/Esquema.jsf");
                 return;
            }
        }
        filterChain.doFilter(request, response);
    }
}

您应该在 Web 应用程序中配置过滤器。假设您使用 Java EE 6 Web 应用程序服务器(如 GlassFish 3.x)或 Java EE 6 servlet 容器(如 Tomcat 7),您可以将此标记添加到您的类以使其成为过滤器

@WebFilter(filterName = "MyAppFilter", urlPatterns = {"/*"})
public class MyAppFilter implements Filter {
    //filter implementation...
}

如果没有,那么你应该在你的 web.xml 中配置它

<filter>
    <filter-name>MyAppFilter</filter-name>
    <filter-class>my.package.MyAppFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>MyAppFilter</filter-name>
    <servlet-name>*.jsp</servlet-name>
</filter-mapping>
于 2012-10-14T22:46:26.073 回答