好吧,我會在這裏發佈的所有代碼非常久遠的描述......
好這裏是被稱爲第一指標.. 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」值的原因..
從你的描述使用豆可能會簡化你的應用程序。你可以發佈一些代碼嗎? – gebuh 2012-08-07 02:35:15
猜猜正確的代碼會做。發佈它我可以修改並告訴你答案。 – Prateek 2012-08-07 04:02:19
剛剛更新了下面的答案由Farhan艾哈邁德..請檢查它..等待快速回復.. – 2012-08-07 18:14:04