簡體   English   中英

重定向首頁中的所有頁面

[英]Redirect All the Pages in Home Page

我有一個Web應用程序,其中包含幾個jsp頁面。 我的主頁是welcome.jsp,應用程序的URL類似於www.test.com

因此,只要用戶點擊該網址(www.test.com),它就會重定向到www.test.com/welcome.jsp

現在我想如果用戶直接想要訪問任何其他頁面,例如www.test.com/*.jsp,它應該始終重定向到我的主頁,即www.test.com/welcome.jsp

請給任何建議如何做。

您可以將以下映射添加到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。

編輯:

如果您只想在尚未訪問過歡迎的jsp的用戶上進行重定向,請不要在web.xml文件中使用上面的代碼。 而是在您的jsp中,在welcome.jsp中的用戶會話上設置一個標志:

<c:set scope="session" var="sessionStarted" value="true"/>

然后添加create Filter來像這樣重定向它們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);
    }
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM