my dog learned polymorphism*
The moose likes Servlets and the fly likes Form Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Form Variables" Watch "Form Variables" New topic
Author

Form Variables

Subbu Viswanathan
Greenhorn

Joined: May 15, 2001
Posts: 8
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>
.......
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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

Joined: May 15, 2001
Posts: 8
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

Joined: Feb 20, 2001
Posts: 3695

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

Joined: May 15, 2001
Posts: 8
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Form Variables