繁体   English   中英

jsp,servlet后退按钮问题

[英]Jsp, servlet back button issue

我正在Java EE中创建一个简单的网站,因为要保持简单性,所以我没有使用bean。

网站的结构就像我有不同的Jsp页面,每个jsp页面都有一个对应的servlet,该servlet然后将jsp页面连接到数据库以存储和检索数据。

问题是我有此一页索引1,在其中我从下拉列表中选择了一些选项。 我的下一个jsp页面index2.jsp应该根据从index1.jsp中选择的选项从数据库的字段中加载数据。

index2具有上一个和下一个按钮。 我正在存储从index1选择的选项的值,以立即显示在每个jsp页面上。 但是,当我在此index2.jsp上单击下一步时,它将带我到具有相同选项值的index3,但是当我在index3上按下前一个按钮时,它将带我到index2的servlet,在这种情况下为Servlet2,其中option的值显示为空值。 当我按下“上一个”按钮时,它正在破坏会话中的值。 之后,所有其他页面也将option的值显示为null。

另一个问题是,当使用servlet将数据从db加载到indexa时,为什么使用servlet地址而不是indexa。

好的,首先没有任何代码,我有点“在黑暗​​中回答”

每个jsp页面都有一个相应的servlet,然后将jsp页面连接到数据库以存储和检索数据[...]

好吧,JSP是servlet,因此您所有的servlet代码都可以在JSP内运行,但是我假设您选择了MVC架构。

我认为您的主要问题如下。

在index1.jsp中,您使用名为param的参数向index2.jsp发出请求(仅作为示例)。 然后在index2.jsp中,使用上一个相同的参数param BUT向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>

希望对您有所帮助

好吧,我将在此处发布所有代码以及说明...

好这里是称为第一索引.. indexnpro.jsp其在得到的值转发它们Servletnpro这样的代码.. <form class="form-class" name="myform" action="Servletnpro" method="POST">注意:请记住,因为这里的体系结构是MVC,所以每个jsp都将有其自己的servlet,因此值不会直接传递给另一个jsp,而是一个servlet,然后将其传递给下一个jsp .....现在使用Servletnpro的dopost方法...

String connectionURL = "jdbc:mysql://localhost/secnok?"+ "user=root&password=";
Connection connection=null;
response.setContentType("text/html");
PrintWriter out = response.getWriter();

HttpSession session = request.getSession(true);

//这些是我从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时,它将变为null。

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);

//在这里,我将从db获取的所有数据发送到所需的jsp,然后将这些数据分发出去。即“ 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相同的正确toepost值的下一个页面。

下一个

现在让我们转到下一页indexb.jsp

在这里,我再次像这样从indexa.jsp获取“ corrtoe”的值。

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

Uptill现在即使在此页面上我也能获得正确的价值...一旦按此页面上的后退按钮,问题就会出现。

现在,在此页面上的后退按钮代码。

以前

现在,当我按下上一个按钮时,我失去了indexa.jsp页面中的脚趾值,并且它在界面上将corrtoe值显示为null .....在此之后,我尝试使用Servlets Get方法来获取该值,但该值只能工作一次,但之后在indexa.jsp上按next,它将在indexb.jsp和其余页面上显示null ...问题是为什么在将其存储在会话中时为什么也会丢失此值。 请给我一些想法。

另一个问题是,当我在indexnpro页面上按Submit时,为什么它在地址栏中显示Servletnpro而不是indexa.jsp ...但是它的确显示页面indexa.jsp填充了indexa.jsp中的值...我认为这是我一直在jsp页面之间丢失“ corrtoe”值的原因。

在看这个问题时,它涉及跨源于index1页面值选择的请求中的数据存储。 有多种方法,一种是在会话中保留此值并在页面之间使用它。 另一个是在每个屏幕上都有隐藏字段,并将其传递出去。 这可能是粗略的方法。 您可以使用单个servlet和不同的页面方法,或servlet到页面的映射。 高兴查看代码示例的JSP示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM