Solved

Sending a user back a page in history from a form submission

Posted on 2003-11-04
6
259 Views
Last Modified: 2010-04-01
I have a jsp page that submits a form back to itself. When the page is submitted it checks a value for duplicates in the database. If a record is found It alerts the user and this is where the problem occurs.
I want it to take the user back to the page in the state it was when they entered the data but it submits the form data and moves onto the next process.
I have tried history.back() and window.go()
as well as
parent.frames['header'].history.back();parent.frames['main'].history.back();
(the site unfortunately uses framesets at the moment)

below is a snippet of the surrounding code. If anyone could advise me what I'm doing wrong<!--as my javascript knowledge is extremely noobish :| --> I'd be extremely grateful

cheers in advance


//get email submitted and test to make sure it is unique record
<%
  try {
     email = request.getParameter("email");
  }
  catch(Exception e) {
     throw new Exception("email not passed in queyystring");
  }  
try {
     query = "begin ? := \"CHANGEDB_REP\".\"CHECKIFEMAILEXISTS\"(?); end;";

     try{
       connection.setAutoCommit(false);
         try {
            stmt =(OracleCallableStatement)connection.prepareCall(query);
            str = new  oracle.sql.CHAR(email ,myCharSet);
            stmt.setCHAR(2, str);
            stmt.registerOutParameter(1, OracleTypes.NUMBER);
            stmt.execute();
            connection.commit();

            try {
               output = stmt.getNUMBER(1);

               if (output.intValue() > 0) {
%>
                  <script language="Javascript">
                      alert('Sorry the system already has a user registered with the
                              email address <%=email%>');
                      window.history.go(-2);
                  </script>
<%
               }
            }
            catch (Exception e) {
               e.printStackTrace();
            }
            stmt.close();
         }
         catch (Exception e) {
             System.err.println("error creating statement for checkifemailexists");
             e.printStackTrace();
         }
     }
     catch(Exception e) {
        e.printStackTrace();
     }
 }
%>
0
Comment
Question by:RapidDrinker
  • 3
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
applekanna earned 250 total points
ID: 9683978
We can do this witithout JS

add another parameter in the JSP page whcih lets you know if it is a first request or not say

<input type = hidden name = "error" value =""> for the first time when the page is viewed itself whcih means the user enters data and

suppose this is the signup page

<%
  try {
     email = request.getParameter("email");
     error = request.getParameter("error");

  }
  catch(Exception e) {
     throw new Exception("email not passed in queyystring");

if(error.equals(true)) //duplicate login
{
 //add code to display that you have an error
}
else if (error.equals(false))
{
  //add code to submit the form or use a bean to submit data to db
}
else if(error == null) //first time it is being submitted so get to the db and chec it
{
....

   try {
               output = stmt.getNUMBER(1);

               if (output.intValue() > 0) {
                //error so do
               response.sendRedirect(yourPage?error=true&email=jhjh@hkj.cm);
               }
              else
               response.sendRedirect(yourPage?error=false&email=jhjh@hkj.cm)




}
 

Hope this helps.
If code is still not clear and you want to continue mail again I will explain
  }  
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9684347
shouldn't it be history.go(-1)?
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9684467
expert kennethxu

RFC on my approach :)
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 14

Expert Comment

by:kennethxu
ID: 9689616
>> RFC on my approach :)
I think the question is about duplicate registration instead of duplicate login. Asker want to push user back to the registration page and correct the email address, then submit again.

There are usually 2 approaches. one is what asker did, use javascript the send user back. another is to redisplay the login page with all the data pre-polulated with what user has already submitted.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9689622
sorry, I meant to redisplay the "registration" page with ...
0
 

Author Comment

by:RapidDrinker
ID: 9689964
Hi thanks all,

I kinda got it sorted myself before all your kind help. I simply added a flag in the after the test for duplication which determined whether or not the serverside code was executed or not. lol I still got to get my head around the whole client side scripting. I've accepted apples reply as it was along the same idea as what I used.

thanx for ya help
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet init param and contect param 3 67
TreeSet comparator example 7 97
jdbc error in jsp application 20 64
designing in object programming 12 94
If you are looking at this article, you have most likely been hit by some version of ransomware and are trying to find out if there is anything you can do, or what way you should react - READ ON!
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question