go to jsp page by hitting back button

我的未来我决定 提交于 2020-12-30 06:40:11

问题


I have 2 jsp pages and one Servlet. I am fetching data from database by servlet and sending result to result.jsp page where i am displaying result. But i want to add a Back button in result.jsp , by clicking back button i want to go to index.jsp, but problem is when i am clicking back button everytime a message is coming Confirm form submission and it is irritating me. How can i avoid this Confirm form submission? perhaps it is coming as processing is done in servlet.

index.jsp

<form method="post" action="Student">

<input type="text" name="studentname"/>
<input type="submit" value="search"/>

</form>

Student servlet

 String student_name=request.getParameter("studentname");

  -------fetching data from database------------
  -------------------sending to result.jsp------

  String nextJSP = "/result.jsp";
      RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
      dispatcher.forward(request,response);

result.jsp

 //displaying data here

<form action="index.jsp">

 <input type="submit" value="back"/>// a back button is here which is going to index.jsp   
</form>

EDIT

From result.jsp i am going to another page result1.jsp like below

In result.jsp i have written the following:

<a  href="result1.jsp.jsp?a code=<%out.println(student_name);%>"><%out.println(student_name);%></a>

By clicking the above hyperlink i went to result1.jsp

I want to add a back button here(in result1.jsp) and after clicking i want to do to result.jsp, but when clicking back button i am getting Confirm form submission every time. I have written the following in result1.jsp

<input type="button" value="Back" onclick="javascript:history.go(-1)">

Still i am getting that message Confirm form submission. How can i avoid this? I want to go to result.jsp directly with out this message. How is it possible?


回答1:


you can also use this to go one page back

<button type="button" name="back" onclick="history.back()">back</button>



回答2:


You can write the below code that let's you to go index.jsp on your result.jsp page

<a href="index.jsp">Back</a>



回答3:


Try this

<button type="button" 
    name="back" 
    onclick='window.location='<your_path>/index.jsp'>back</button>



回答4:


If you want a back button to go index.jsp, why not just make a normal link?

<a href="index.jsp">Back</a>

There is only two way to get rid of the message, normal link or window.location. If the previous page is always the same, you don't need to use complicated function client side. If the page could be different, just post the link to result.jsp and use it to actually print a back link!

EDIT :

previous.jsp

<form method="post" action="Student">
<input type="hidden" name="back" value="previous.jsp" />
<input type="text" name="studentname"/>
<input type="submit" value="search"/>
</form>

result.jsp

out.println("<a href=\"" + request.getParameter("back") + "\">Back</a>");



回答5:


This is the easiest way to create a goBack button using the method goBack(). This is the same as clicking the "Back button" used on the top of your browser.

 <!DOCTYPE html>
    <html>
    <head>
    <script>
    /* The back() method loads the previous URL in the history list. 
       This is the same as clicking the "Back button" in your browser.
    */
    function goBack() {
        window.history.back()
    }
    </script>
    </head>
    <body>

    <button onclick="goBack()">Go Back</button>

    <p>Notice that clicking on the Back button here will not result in any action, because there is no previous URL in the history list.</p>

    </body>
    </html>



回答6:


You can use a common button.

<input type="button" value="Back" onclick="javascript:history.go(-1)">

With history.go(-1), your browser will simply display previous page by reading from cache, it will not resubmit your data to the server, thus, no Confirm form submission will happen.

EDIT

I was wrong!

All you need is a client side redirect, you may write something like this in your result.jsp after it handles the form submitted from index.jsp:

response.sendRedirect("result.jsp");

So you will need a parameter to help you in result.jsp to tell whether it is a simple display request or a form submission, something like this:

String action = request.getParameter(action);
if("submit".equals(action)) {
    // handle form data
    response.sendRedirect("result.jsp");
} else {
    // other code to display content of result.
}

In your index.jsp, it would be something like this:

....
<form action="result.jsp?action=submit" ...>



回答7:


I experienced that form submit confirmation with Safari (not with IE/Chrome/FF).

The work around is submitting your form with "get" method. Of course this is only valid for "small" forms (max 2048 K).




回答8:


Try this

<a href="${pageContext.request.contextPath}/index.jsp" class="btn btn-success">
    Back
</a> 

with bootstrap.css from here



来源:https://stackoverflow.com/questions/10513745/go-to-jsp-page-by-hitting-back-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!