• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Form Variables

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a JSP page (say a.jsp) with FORM variables.
FORM variables in "a.jsp" are submitted to a servlet (Servlet1).
Servlet1 has additional FORM variables.
After some processing is done in Servlet1, using response.sendRedirect I am redirecting the response to another 3rd party Servlet (Servlet2).
I would like to know how Servlet2 can recognize the form variables present in Servlet1.
Thanks.
A.JSP
-----
<SCRIPT LANGUAGE="JavaScript">
function Execute()
{
document.A.action = "/servlet/Servlet1";
document.A.submit();
}
</SCRIPT>
.......
.......
.......
<FORM NAME=A METHOD=POST ACTION="">
//FORM variables are present here
</FORM>
.......

Servlet1
--------
response.sendRedirect("servlet/Servlet2");
.......
.......
.......
<FORM NAME=Servlet1 METHOD=POST ACTION="">
//FORM variables are present here..These form variables could not be retrieved from Servlet2
</FORM>
.......
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can tell you what I do when writing servlets this way. Inside my servlet I make a private method. Now in my doPost or doGet, I take the request and response objects and forward them to wherever they need to go, based on parameters. For example, 'prepForPageTwo' would receive the request and response objects if the parameter page=Page2. Now the reason that I get the session is because sometimes I'll put some data into the session, so that the next page can see some of the things I come up with in the "//do some work here " section. Alternately, you can use req.setAttribute(obj, obj). And I have to use these, because unfortunately there is no such thing as request.setParameter() *you can't add parameters to the request that a servlet received and forward it to another resource.* But, you can add attributes which are just as good. In the code above, it would look like:
It's not quite as nice as being able to 'setParameter' on the request, but it does the job.

I hope that helped.
 
Subbu Viswanathan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mike,
Your suggestion was very useful for me.
I had earlier tried using RequestDispatcher, but the Inprise App Server 4.1 from Borland complains this interface as error. Please find below the error message displayed on the browser.
----------------------------------------------------------------
500 Internal Server Error
The servlet named invoker at the requested URL
http://rls/servlet/Realogic.ReportExecute
reported this exception: java.lang.NoSuchMethodError. Please report this to the administrator of the web server.
----------------------------------------------------------------
I have another query to ask at this time. I am aware of the fact that we can use setAttribute on the request object and use getAttribute on a different page to get the value set.
However, the Servlet2 code that I had mentioned in my earlier post is a 3rd party servlet. I just have the jar file for Servlet2 and I am not sure whether I can still use SetAttribute on my Servlet1.
Any suggestions please.
Thanks once again for your help.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a feeling that your second servlet would be 'black box'. You are right in suspecting that the 2nd servlet would require the form data to be in the QueryString, as opposed to in the Attributes.

If the 'additional' form elements don't depend on the values entered in the first form, then you can include them in the first form as hidden fields.

Otherwise, I can only think of a very kludgey approach to the problem. Somehow you can send the first form data to temp.jsp, where temp.jsp has code to populate the first form data into a second form on temp.jsp that includes the form data that the servlet needs. Then on this page, you can write a bit of javascript that on window load, will say form.submit(); I have not thought about this very hard, and i think there may be a problem with this approach. But I'm also not sure how to do this without client side javascript.
 
Subbu Viswanathan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The 3rd Party Servlet that I have requires parameters to be passed via QueryString/POST method.
I cannot use QueryString since there is a limitation on the size of data that can be sent over the URL. Instead what I am doing is have a FORM in my intermediate Servlet (Servlet1) that would have the required parameters as Hidden Fields. As suggested by you, I am writing Client-Side JavaScript on Servlet1 that would load Servlet2 on the OnLoad event in <BODY> tag. I knew this method is not the correct one.
The problem here is, if I hit back once while I am on Servlet2 page, Servlet2 is displayed everytime (since I have automatic code on Servlet1 that would load Servlet2).
So I have to use the little drop-down arrow on the browser to go back to the 1st page (JSP).
Its because of these problems I thought would it be possible to send the Response object from Servlet1 to Servlet2. I guess it would be okay for me to use Client-Side JavaScript, if so, how can I tackle this issue of NOT seeing the same Servlet2 page everytime I hit back button in the browser.
Thanks for your time Mike...
 
Police line, do not cross. Well, this tiny ad can go through:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic