Solved

sendRedirect and querystring

Posted on 2001-06-28
14
694 Views
Last Modified: 2012-08-13
currently i'm doing the following:
res.sendRedirect(ServletUtilities.urlServlet + "AdminTopicsRequired?updatedTopic=" + whichTopic);

but when it redirects it obviously has the querystring in the location textbox of the browser.  I am calling a servlet to update my database and the servlet sends us back to the submit page.  the querystring verifies that the transaction took place and the javascript in the page alerts that it in fact did.  The problem is that if the user refreshes, that alert will show up even though it didn't do anything.  

my question is:
how do i tell the submit page that the transaction was ok without using querystring.

thanks in advance
0
Comment
Question by:bobbit31
  • 7
  • 6
14 Comments
 
LVL 2

Expert Comment

by:bagi112599
ID: 6235780
In order to submit page that the transaction was ok without using querystring:
Basically you use request.setAttribute(strBeanName, objBean) method.

So, in your servlet:
 
 public void doPost(HttpServletRequest request, HttpServletResponse response){
 ....
   // create a Bean:
  MyBean myBean =  new MyBean();
  // set properties to be read on JSP page:
  myBean.setName("Bob");
...
  // attach bean object to request:
  request.setAttribute("aBean", myBean);

  ..
  // redirect from servlet to jsp page called 'myJsp':
  this.getServletContext().getRequestDispatcher("myJsp.jsp").forward(request, response);
}

And, in you jsp:
...
<jsp:useBean id="aBean" class="MyBean" scope="request" />

...
<td>Client Name: </td>
<td><jsp:getProperty name="aBean" property="name"/>
</td>...

or, altenatively on JSP:
...
<% MyBean myBean  = (MyBean)request.getAttribute("aBean"); %>
...
<td>Client Name: </td>
<td><%=myBean.getName()%>
</td>
...


Code for MyBean class:

class MyBean {
private String name;
public String getName(){
     return name;
}
public void setName(String str){
     name= str;
}
}

TIP: If you use

public String getName(){
     return (name==null)?"":name;
}
method, then it won't print 'null' on browser, when name attribute is null.



0
 
LVL 18

Author Comment

by:bobbit31
ID: 6235920
ok, but i still have the same problem... now i get this in the location text box:

http://localhost/servlet/UpdateTopicsRequired?selTopic=3&selCampus=1&selDept=3&selEmployee=3&cmdAdd=Add+Requirement%28s%29

0
 
LVL 18

Author Comment

by:bobbit31
ID: 6235989
0
 
LVL 9

Expert Comment

by:nimaig
ID: 6236831
This is just an idea. Try using cookies. Set the cookie when the User submitsa the page.
When you redirect it back to the SubmitPage check for the cookie. If the cookie is present
display the alert. After doing this remove the cookie. After thet if user refreshes the page also
your code won't find the cookie and it won't disply the alert.
0
 
LVL 2

Expert Comment

by:bagi112599
ID: 6238264
put all your vars in
selTopic, selCampus, selDept, selEmployee, cmdAdd etc
in a form if you haven't done so and change form method to POST.
It looks like you are using Method=GET
or Method not specified(thenbrowser defaults it to GET)
Then it wan't show query string in Address field.
0
 
LVL 2

Expert Comment

by:bagi112599
ID: 6238270
And remember to handle client requests in your servlet's
doPost method.
0
 
LVL 18

Author Comment

by:bobbit31
ID: 6238593
nimiag: sorry, can't be sure everyone will have cookies enabled.

bagi: getting closer!

now i get http://localhost/servlet/UpdateTopicsRequired
which is the servlet that handles the database update

but i want http://localhost/servlet/AdminTopicsRequired 
which is the servlet that generates the submit page

in the location box
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 18

Author Comment

by:bobbit31
ID: 6238605
increasing points since i might as well use the ee-kpro points before i get refilled
0
 
LVL 2

Accepted Solution

by:
bagi112599 earned 200 total points
ID: 6238913
now,when instead of
res.sendRedirect(ServletUtilities.urlServlet + "AdminTopicsRequired?updatedTopic=" + whichTopic);

use :
attach value of "updatedTopic" to request as described in my first comment and use:

res.sendRedirect(ServletUtilities.urlServlet + "AdminTopicsRequired");

More specifically:
public void doPost(HttpServletRequest request, HttpServletResponse res){
....
 String whihTopic =  "This Topic";
 // attach topic info to request:
 request.setAttribute("updatedTopic", whihTopic);

 ..
 // redirect from servlet to AdminTopicsRequired:
res.sendRedirect(ServletUtilities.urlServlet + "AdminTopicsRequired");


And, in your AdminTopicsRequired:
...
...
 String whichTopic  = (String)request.getAttribute("updatedTopic");
...
...
0
 
LVL 18

Author Comment

by:bobbit31
ID: 6239005
i have it exactly as you state but in AdminTopicsRequired:

String whichTopic is always null...
0
 
LVL 2

Expert Comment

by:bagi112599
ID: 6239083
try to use  
this.getServletContext().getRequestDispatcher("AdminTopicsRequired").forward(request, response);

instead of
res.sendRedirect(ServletUtilities.urlServlet + "AdminTopicsRequired");

Or alternativley,
public void doPost(HttpServletRequest request, HttpServletResponse res){
....
String whihTopic =  "This Topic";
// attach topic info to request:
res.setAttribute("updatedTopic", whihTopic);

..
// redirect from servlet to AdminTopicsRequired:
res.sendRedirect(ServletUtilities.urlServlet + "AdminTopicsRequired");


And, in your AdminTopicsRequired:
...
...
String whichTopic  = (String)res.getAttribute("updatedTopic");

NOTE:
I attached "updatedTopic" to response this time.

When we use
res.sendRedirect(ServletUtilities.urlServlet + "AdminTopicsRequired");
request object is not forwarded to AdminTopicsRequired.

cheers,
0
 
LVL 18

Author Comment

by:bobbit31
ID: 6239176
HttpServletResponse does not have the setAttribute method.

i'm gonna go about this another way using session variables.

you get the points anyway, thanks for your help!
0
 
LVL 2

Expert Comment

by:bagi112599
ID: 6239555
thanks for points but,
did you try my first suggestion:
this.getServletContext().getRequestDispatcher("AdminTopicsRequired").forward(request, response);
?
0
 
LVL 18

Author Comment

by:bobbit31
ID: 6239568
yes...

basically the goal i wanted was:

if they do an update, do it, send them back to the submit page and display a message.  If they refresh the submit page DO NOT show the message again.

even if i were to do it your way, the refresh would still show the message since the attribute would still be set.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 24
create a gui in perl 3 72
Unable to start eclipse ? 17 134
numbers ascending pyramid 100 141
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now