我有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
答案 0 :(得分: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>