2012-05-09 36 views
1

我在JSP中相當新,我需要一些幫助。在index.jsp加載之前調用控制器

我有一個index.jsp文件,顯然如果你輸入「localhost //」,它會默認自動調用index.jsp。我想知道這是如何工作的,因爲我打算在index.jsp加載之前先調用控制器。

雖然我試着解決它。在我的index.jsp,我把那裏是這樣的:

if(request.getParameter("submit") == null && 
    request.getAttribute("submit") == null){ 
    response.sendRedirect("getInformation"); 
} 

在這裏,我強迫的index.jsp直接調用我的控制器/ servlet的。 (我用@ WebServlet(「/ getInformation」在我想調用的控制器上)

我想知道是否有更好的方法來做到這一點,因爲我希望我的控制器/ servlet上的所有邏輯代碼以及所有html代碼儘可能多。

+0

你要什麼在控制呢?如果你分享這個,我想你會得到更好的方法。 – Apurv

回答

1

IMO,這是最好的辦法。你可以在web.xml中定義你的歡迎文件。創建一個'stupid'index.html設置一個META標籤重定向到你的控制器,SomeController在下面(代表某種計算從服務器到客戶端)的示例:

的web.xml

<welcome-file-list> 
    <welcome-file>/WEB-INF/jsp/index.html</welcome-file> 
</welcome-file-list> 

的index.html

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<meta http-equiv="refresh" content="0; URL=./SomeController" /> 
<title>Some title</title> 
</head> 
<body> 
If you are not automatically redirected please click <a href="./SomeController">here</a>. 
</body> 
</html> 
+0

太棒了!謝謝!將盡力做到這一點。 – user979023

+0

它的工作。謝謝一堆! – user979023

相關問題