2012-08-07 90 views
1

我正在Java EE中創建一個簡單的網站,我不使用bean,因爲我想保持簡單。Jsp,servlet後退按鈕問題

網站的結構就像我有不同的Jsp頁面,每個jsp頁面都有一個相應的servlet,然後將jsp頁面連接到數據庫來存儲和檢索數據。

問題是我有這個頁面index1,我從下拉菜單中選擇一些選項。我的下一個jsp頁面是index2.jsp,它應該根據從index1.jsp中選擇的選項從其數據庫中加載數據。

index2有一個下一個和上一個按鈕。我將存儲從index1中選擇的選項的值以顯示在每個jsp頁面上。然而,當我點擊這個index2.jsp上的下一個時,它使我得到index3具有相同的選項值,但是當我按下index3上的上一個按鈕時,它將我帶到index2的servlet,在這種情況下,Servlet2的選項值顯示爲空值。當我按下「上一個」按鈕時,它正在破壞會話的價值。之後所有其他頁面顯示option的值也爲null。

另一個問題是爲什麼當使用servlet從數據庫將數據加載到indexa中時,servlet的地址而不是indexa的地址。

+0

從你的描述使用豆可能會簡化你的應用程序。你可以發佈一些代碼嗎? – gebuh 2012-08-07 02:35:15

+1

猜猜正確的代碼會做。發佈它我可以修改並告訴你答案。 – Prateek 2012-08-07 04:02:19

+0

剛剛更新了下面的答案由Farhan艾哈邁德..請檢查它..等待快速回復.. – 2012-08-07 18:14:04

回答

0

好吧,首先,沒有任何代碼,我有點「黑暗中的回答」

[...]每一個JSP頁面有相應的小服務程序,然後JSP頁面連接到數據庫來存儲和檢索數據[...]

那麼,一個JSP是一個servlet,所以你所有的servlet代碼都可以在JSP中運行,但我假設你選擇了一個MVC體系結構。

我認爲你的主要問題如下。

從index1.jsp中,使用名爲param(僅舉例)的參數向index2.jsp發出請求。 然後在index2.jsp您再次要求與所謂PARAM相同的參數index3.jsp 但是當你打以前你不及格稱爲PARAM的參數index2.jsp

要麼你傳遞參數返回(最安全),或者您使用瀏覽器的歷史記錄(不安全,但會執行您從index1.jsp發送到index2.jsp的SAME請求)

恐怕我不能幫助您了,而沒有任何代碼

編輯

我覺得這是你的錯誤 request.setAttribute("corrtoepost", corrtoepost); 你是不是存儲「corrtoepost」在會議,但該請求。要存儲參數會話,你必須做到以下幾點 request.getSession().setAttribute("corrtoepost", corrtoepost);

新的編輯 好吧,這裏是我的全功能的例子。我測試過它,它工作。

indexA。JSP

<html> 
    <body> 
     <form action="servletA" method="POST"> 
      <select name="corrtoe"> 
       <%if("1".equals(request.getSession().getAttribute("corrtoe"))){ %> 
        <option value="1" selected>1</option> 
       <%}else{ %> 
        <option value="1">1</option> 
       <%} %> 
       <%if("2".equals(request.getSession().getAttribute("corrtoe"))){ %> 
        <option value="2" selected>2</option> 
       <%}else{ %> 
        <option value="2">2</option> 
       <%} %> 
       <%if("3".equals(request.getSession().getAttribute("corrtoe"))){ %> 
        <option value="3" selected>3</option> 
       <%}else{ %> 
        <option value="3">3</option> 
       <%} %> 
      </select> 
      <input type="submit"> 
     </form> 
    </body> 
</html> 

ServletA.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     String corrtoepost = request.getParameter("corrtoe"); 
     //Search in DB or whatever 
     request.getSession(true).setAttribute("corrtoe", corrtoepost); 
     getServletContext().getRequestDispatcher("/jsp/indexB.jsp").forward(request,response); 
    } 

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     doGet(request, response); 
    } 

indexB.jsp

<html> 
    <body> 
     <h1><%=request.getSession().getAttribute("corrtoe")%></h1> 
     <form action="jsp/indexC.jsp" method="GET"> 
      <input type="submit"> 
     </form> 
    </body> 
</html> 

indexC.jsp

<html> 
    <body> 
     <h1><%=request.getSession().getAttribute("corrtoe")%></h1> 
    </body> 
</html> 

,這是我的web.xml

<servlet> 
    <display-name>servletA</display-name> 
    <servlet-name>servletA</servlet-name> 
    <servlet-class>org.test.servlets.ServletA</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>servletA</servlet-name> 
    <url-pattern>/servletA</url-pattern> 
</servlet-mapping> 

<welcome-file-list> 
    <welcome-file>jsp/indexA.jsp</welcome-file> 
</welcome-file-list> 

我希望它能幫助

+0

嘿javiercbk檢查我的代碼解釋下面...等待迴應.. – 2012-08-07 18:13:17

+0

請檢查我的編輯...我認爲你無法將參數設置爲會話。 – javiercbk 2012-08-08 19:15:50

+0

嘿javiercbk ..我只是檢查一下,然後回覆你.. – 2012-08-09 12:18:29

0

好吧,我會在這裏發佈的所有代碼非常久遠的描述......

好這裏是被稱爲第一指標.. indexnpro.jsp其在獲取值轉發這些代碼到Servletnpro這樣.. <form class="form-class" name="myform" action="Servletnpro" method="POST">注意:記住,因爲這裏的架構是MVC,所以每個jsp都有自己的servlet,所以值不會直接指向另一個jsp,而是一個servlet,然後將它們傳遞給下一個jsp ..... Now在Servletnpro的dopost方法中...

//這是不同的值,我從indexnpro.jsp獲得,有的則是下拉框,但我不應該真正的問題..

String exproject=request.getParameter("country"); 
String corrcust=request.getParameter("state"); 
String excust=request.getParameter("country1"); 
String corrproject=request.getParameter("state1"); 

String corrtoepost= request.getParameter("corrtoe"); 

< ----------- ----這個數值我一直在失去按下後退按鈕時..當它再次談到這個servlet它變爲空..

System.out.println(corrtoepost); 

System.out.println("doPost is running"); 
System.out.println("Session id on dopost: " + session.getId()); 

request.setAttribute("corrtoepost", corrtoepost); < ----------- ----在會話中存儲價值,但我仍然失去它..

///////////從現在開始,數據庫連接的簡單代碼將連接到數據庫,根據用戶選擇獲取數據,即我剛剛存儲在會話中的「corrtoepost」,然後將顯示其重新打開indexa.jsp ..

try{ 
int rs1; 

// Load the database driver 
Class.forName("com.mysql.jdbc.Driver"); 
// Get a Connection to the database 
connection = DriverManager.getConnection(connectionURL); 

//Add the data into the database 

Statement stmt = connection.createStatement(); 
Statement stmt1 = connection.createStatement(); 
Statement stmt2 = connection.createStatement(); 
Statement stmt3 = connection.createStatement(); 
Statement stmt4 = connection.createStatement(); 
Statement stmt5 = connection.createStatement(); 
Statement stmt6 = connection.createStatement(); 





ResultSet rs = stmt.executeQuery("Select * from toe_description where toe_id= '" + corrtoepost + "' ") ; 




ResultSet rs2 = stmt1.executeQuery("Select * from toe_text where type_id=1 AND toe_id= '" + corrtoepost + "' ") ; 
ResultSet rs3 = stmt2.executeQuery("Select * from toe_text where type_id=2 AND toe_id= '" + corrtoepost + "' ") ; 
ResultSet rs4 = stmt3.executeQuery("Select * from toe_text where type_id=3 AND toe_id= '" + corrtoepost + "' ") ; 
ResultSet rs5 = stmt4.executeQuery("Select * from toe_text where type_id=4 AND toe_id= '" + corrtoepost + "' ") ; 
ResultSet rs6 = stmt5.executeQuery("Select * from toe_text where type_id=5 AND toe_id= '" + corrtoepost + "' ") ; 
ResultSet rs7 = stmt6.executeQuery("Select * from toe_text where type_id=6 AND toe_id= '" + corrtoepost + "' ") ; 











while(rs.next()){ 
String toeid= rs.getString(1); 
String toename= rs.getString(2); 
String Intname= rs.getString(4); 
String Assetname= rs.getString(5); 
String Objname= rs.getString(6); 
String ProjectID= rs.getString(7); 



request.setAttribute("toeid", toeid); 
request.setAttribute("toename", toename); 
request.setAttribute("Intname", Intname); 
request.setAttribute("Assetname", Assetname); 
request.setAttribute("Objname", Objname); 
request.setAttribute("ProjectID", ProjectID); 





while(rs2.next()){ 
String purpose= rs2.getString(4); 

request.setAttribute("purpose", purpose); 








while(rs3.next()){ 
String scope= rs3.getString(4); 

request.setAttribute("scope", scope); 
System.out.println(scope); 





while(rs4.next()){ 
String toe_desc= rs4.getString(4); 

request.setAttribute("toe_desc", toe_desc); 


System.out.println(toe_desc); 




while(rs7.next()){ 
String toe_ass= rs7.getString(4); 

request.setAttribute("toe_ass", toe_ass); 








while(rs6.next()){ 
String enviroment= rs6.getString(4); 

request.setAttribute("enviroment", enviroment); 





while(rs5.next()){ 
String ass_env= rs5.getString(4); 

request.setAttribute("ass_env", ass_env); 



} 


} 



} 



} 



} 



} 

} 


request.getRequestDispatcher("/WEB-INF/indexa.jsp").forward(request, response); 

//這裏我送取從數據庫到所需的JSP然後將dislpay數據..即所有數據「indexa.jsp」

System.out.println("Connected to the database"); 
connection.close(); 
System.out.println("Disconnected from database"); 

} 

catch (Exception e) { 
e.printStackTrace(); 
} 



} 



} 

////////現在代碼的Indexa.jsp

此頁面將顯示從前面討論的Servletnpro中獲取的數據..

在頁面的body標籤下,我檢索了先前在會話中保存在Servletnpro中的「corrtoepost」的值..我可以看到此值..

String corrtoepost1=request.getParameter("corrtoepost"); 
String corrtoepost=(String) session.getAttribute("corrtoepost"); 
session.setAttribute("corrtoepost",corrtoepost); 

,然後jsp頁面上顯示它..這正顯示出正確的值作爲現在..在這個頁面中的下一個按鈕

現在代碼 記住下一個按鈕是假設取我到了下一頁,其中使用了servlet中的correcttoepost值。

下一頁

現在讓我們進入下一個頁面是indexb.jsp

在這裏,我再次從indexa.jsp得到 「corrtoe」 的值,這樣的..

String corrtoe=(String) session.getAttribute("corrtoe"); 
session.setAttribute("corrtoe",corrtoe); 

Uptill現在,我還是得到了此頁面上的正確的價值......一旦我按後退按鈕這個頁面上出現問題....

現在對於此頁面上的後退按鈕代碼..

上一頁

現在,當我按下這個按鈕,以前我失去indexa.jsp頁腳趾值,並將其顯示在界面爲空corrtoe值.....在此之後我嘗試使用Servlet的獲取方法得到的值可以工作一次,但在按下nexta.jsp後,它會在indexb.jsp和剩餘的頁面上顯示空值......問題是,爲什麼當我將它存儲在會話中時我失去了這個值。 !請給我一些想法....

另一個問題是,當我按indexnpro頁上提交爲什麼它顯示Servletnpro在地址欄,而不是indexa.jsp ...但它確實顯示頁面indexa.jsp從indexa.jsp的值填充...我認爲這就是爲什麼我繼續失去jsp頁面之間的「corrtoe」值的原因..

+0

這應該被添加到問題中,而不是作爲回答。 – gebuh 2012-08-14 13:45:59

0

看看這個問題,它是關於數據存儲跨請求源於索引1頁面值選擇。有多種方法,其中一種是在會話中保留該值並在整個頁面中使用它。另一種是在每個屏幕上都有隱藏的字段並將其傳遞。這可能是粗暴的方式。你可以有單個的servlet和不同的頁面方法,或者servlet到頁面的映射。很高興看到代碼示例JSP examples