John Kheng
John Kheng

Reputation: 49

Redirect back to previous jsp page with previous form value from servlet

So basically I using following code as a search parcel function, it has 2 button addnew and search where both will get the value target entered in the input field, and redirected depending on which button is chose

        <div>
            <i class="fas fa-search"></i>
            <form id="forms" method="post" action="admin_tracking.jsp">
                <input name="target" type="input" placeholder = "TrackingNo..." class = "text_search"/>
            </form>
        </div>
        <script>
            function submit(value)
            {
                console.log(value)
                if (value == "add") {
                    //change action of form
                    document.getElementById('forms').action = 'admin_tracking.jsp?status='
                    document.getElementById('forms').submit(); //submit
                } else {
                    document.getElementById('forms').action = 'admin_home_result.jsp'
                    document.getElementById('forms').submit();
                }
            }
        </script>
        <button class="button_search" value = "add" onclick="submit(this.value)">
            Add New
        </button>
        <button class="button_search" value = "srch" onclick="submit(this.value)">
            Search
        </button>

Once redirected to the search page, parcel with match trackingNo will be return and user can choose to update the status of the parcel or delete it. When user choose to update the parcel, AdminCollectServletwill be call as followed:

href="AdminCollectServlet?target=<%=rs.getString("trackingNo")%>&from=${pageContext.request.requestURI}&query=${pageContext.request.queryString}"

to perform database update. Once completed i used the line

response.sendRedirect(request.getParameter("from") + "?" + request.getParameter("query"));

to redirect back to where the servlet is called.

But because the target was submitted in form and not as a parameter at the url, redirected page won't be able to return the search result.

I was wondering is there a way to retrieve the full form content and passed it into the response.sendredirect() like how the parameter can be pass?

Upvotes: 2

Views: 2171

Answers (2)

John Kheng
John Kheng

Reputation: 49

After going through the answer Swati mentioned above, I was able to solve part of it. It solve the problem of getting value of target from my forms after redirected back from my AdminCollectServlet

But since i needed to use the the value of target, which is in javascript, in java to execute sql query, i modified some part.

        <script>
            function submit(value)
            {
                console.log(value)
                if (value == "add") {
                    //change action of form
                    document.getElementById('forms').action = 'admin_tracking.jsp?status='
                    document.getElementById('forms').submit(); //submit
                } else {
                    var save = document.getElementById('forms')[0].value;
                    localStorage.clear();
                    localStorage.setItem("save", save);
                    localStorage.setItem("counter", "0");
                    document.getElementById('forms').action = 'admin_home_result.jsp'
                    document.getElementById('forms').submit();
                }
            }
        </script>

in my submit(value) function, i use as guided by Swati, adding another variable counter. This variable allows me to implement the check_storage() function as Swati wrote above and put the targetvalue in the url parameter, so that i can use it in java sql query.

            function check_storage()
            {
                if(localStorage.getItem("save") != null)
                {
                    var value = localStorage.getItem("save");
                    if(localStorage.getItem("counter") != null && localStorage.getItem("counter")=="0")
                    {
                        localStorage.setItem("counter", "1");
                        window.location.replace("admin_home_result.jsp?target=" + value);
                    }
                }
            }

I used the line:

window.location.replace("admin_home_result.jsp?target=" + value);

To refresh the page with the parameter value set and the line:

if(localStorage.getItem("counter") != null && localStorage.getItem("counter")=="0")

To prevent a infinite refresh, by changing the counter variable before changing the url

This way allows me to store the target value from forms, redirect it to servlet, again redirect back to the original page and access javascript variable in java for my sql query

Upvotes: 0

Swati
Swati

Reputation: 28522

If you need to store value at client-side you can use localStorage to store the value of token before submitting the form and onload of page again you can get that value localStorage.getItem(yourvalue);.Some of the changes you need to make in your js and html code . In your html code add onload under body tag .i.e :

 //add onload event
 <body onload="check_storage()">
 <!--other html codes-->
 </body>

Your javascript code :

//onload of your page this will get called
function check_storage() {
  //check if there is any value in localStorage
  if (localStorage.getItem("save") != null) {
    //get that value
    var value = localStorage.getItem("save");
    alert(value);
    //put that value again in input box
  document.getElementsByName("target")[0].value = value;
  }
}

function submit(value) {
//getting input value
  var save =  document.getElementsByName("target")[0].value;
   console.log(save);
  localStorage.clear();//clear previous data
  localStorage.setItem("save", save);//add data to storage
  if (value == "add") {
    //change action of form
    document.getElementById('forms').action = 'admin_tracking.jsp?status='
    document.getElementById('forms').submit(); //submit
  } else {
    document.getElementById('forms').action = 'admin_home_result.jsp'
    document.getElementById('forms').submit();
  }
}

Or if you need to store value at server side you can use HttpSession and using setAttribute you store the value of token and get it in your jsp page simply writing ${sessionScope.yourvariablename} .

Upvotes: 1

Related Questions