2012-08-07 99 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