重定向主页中的所有页面 [英] Redirect All the Pages in Home Page
问题描述
我有一个包含少量jsp页面的Web应用程序。我的主页是welcome.jsp和应用程序网址就像www.test.com
I have a web application which includes few jsp pages. And my home page is welcome.jsp And the Application url is like www.test.com
因此,每当用户点击网址(www.test.com)时重定向到www.test.com/welcome.jsp
So, whenever a user hit the url (www.test.com) it redirects to www.test.com/welcome.jsp
现在我想要用户直接想要访问任何其他页面,例如www.test.com/*.jsp它应始终重定向到我的主页www.test.com/welcome.jsp
now i want if a user directly wants to access any other page like www.test.com/*.jsp it should always redirect to my home page that is www.test.com/welcome.jsp
请提供任何有关如何操作的建议。
Kindly give any suggestion on how to do it.
推荐答案
您可以将以下映射添加到您的web.xml:
You can add the following mapping to your web.xml:
<servlet>
<servlet-name>welcome</servlet-name>
<jsp-file>welcome.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
这会将.jsp文件的所有请求映射到welcome.jsp。
This will map all requests for a .jsp file to welcome.jsp.
编辑:
如果您只想重新定向用户(如果他们尚未访问过欢迎jsp),请不要使用web.xml文件中的上述代码。相反,在你的jsp中,在welcome.jsp中设置用户会话的标志:
If you want to only redirect the users if they haven't already been to the welcome jsp, don't use the code above in your web.xml file. Instead in your jsp set a flag on the user's session in welcome.jsp:
<c:set scope="session" var="sessionStarted" value="true"/>
然后添加create Filter来重定向它们就像这样一个 RedirectFilter.java
:
Then add create Filter to redirect them like this one RedirectFilter.java
:
@WebFilter("*.jsp")
public class RedirectFilter implements Filter {
public void destroy() {}
public void init(FilterConfig fConfig) throws ServletException {}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
Object sessionStarted = ((HttpServletRequest)request).getSession(true).getAttribute("sessionStarted");
if(sessionStarted==null){
request.getServletContext().getRequestDispatcher("welcome.jsp").forward(request, response);
}else{
chain.doFilter(request, response);
}
}
}
这篇关于重定向主页中的所有页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!