2016-12-15 48 views
0

首先,看看我的網頁InterFace of My WebSite IMAGEJSP的sendRedirect與回報不工作

我用文件的index.jsp與參數視圖觀點是我想讓它顯示JSP文件的路徑在<jsp:include>作爲一個IFRAME我宣佈所有視圖在一個枚舉,如:

public enum FView { 
    HomePage, LogIn, SignUp, Profile, UpdateProfile 
} 

在我的index.jsp

<% 
String viewParam = request.getParameter("view"); 
if (viewParam == null) 
    viewParam = "HomePage"; 
FView f = FView.valueOf(viewParam); 

String pageToView = ""; 
switch (f) 
{ 
    case HomePage: 
     pageToView = "page/viewArticles.jsp"; 
     break; 
    case LogIn: 
     pageToView = "login.jsp"; 
     break; 
    case Profile: 
     pageToView = "page/viewProfile.jsp"; 
     break; 
    default: 
     pageToView = "../login.jsp"; 
     break; 
}%> 
<jsp:include page="main.jsp"> 
    <jsp:param name="page" value="<%= pageToView %> 
</jsp:include> 

在我main.jsp中

<div id="header"></div> <!-- <jsp:include> header here, I don't mention all of it.--> 
<div id="content"> 
     <jsp:include page="<%= request.getParameter("page")%>"/> //RED RECTANGLE HERE 
</div> 
<div id="footer"></div> 

我有一個頁面,顯示用戶的個人資料的index.jsp?鑑於=檔案(文件的路徑是的WebContent /頁/ viewProfile.jsp)。我想,當我訪問該頁面無需登錄,它會重定向到我的登錄頁面的index.jsp?鑑於=登錄(文件的路徑是的WebContent/login.jsp的)。下面是代碼:

<% 
Object user_session = session.getAttribute("user_session");; 

String usernameFromSession = null; 
if (user_session != null) 
    usernameFromSession = user_session.toString(); 
else 
{ 
    response.sendRedirect("index.jsp?view=LogIn");//any url 
    return; 
}%> 
<p>This is the profile page</p> 

知道usernameFromSession爲空。當我重新加載頁面時,它顯示一個白頁,甚至沒有p標籤。當我刪除回報,它顯示的個人資料頁內容(上述p標籤)。 但是,如果使用原始路徑:的localhost:8088:......./page/viewProfile.jsp代替localhost:8088:....index.jsp?view=Profile,它的工作。那我該如何解決呢?

回答

0

請發表您的完整代碼。如何獲得會話並將user_session屬性傳遞給會話? 您還希望從您的代碼中獲得哪些行爲?

的sendRedirect應

response.sendRedirect("http://google.com"); 
+0

會議並不重要。現在它是空的,如果它是null(在profile.jsp)sendRedirect到login.jsp,我想要它。而我的sendRedirect是'response.sendRedirect(「index.jsp」)',我使用本地主機。謝謝。 – ThinhPhan

+0

response.sendRedirect(「index.jsp」)是否按預期工作? – fg78nc

+0

@ThinhPhan看到[這個SO答案](http://stackoverflow.com/a/20371272/17300)...因爲fg78nc說,如果你像你的外部重定向一樣,你應該使用協議'http://' google.com測試,但純' 「的index.jsp」'應該工作,因爲的sendRedirect是相對於_current URL_,除非它是一個絕對路徑(開始與'/'或'HTTP [S]://') –