Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

RequestDispatcher.forward() and POST

Posted on 2000-02-14
7
Medium Priority
?
621 Views
Last Modified: 2008-02-01

I need to POST various parameters from one servlet to the next.

I've been using sendRedirect (which works, but is very irritating under anything but IE). But forward() seems to be the correct way. However, it only seems to support GET.

So, how can I POST to the next servlet, without annoying "this page has moved" messages appearing.

We need to explicitly dictate whether or not parameters are passed using POST or GET - as our servlets may perform two distinct tasks.
0
Comment
Question by:dogma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 2519116
>> as our servlets may perform two distinct tasks.

why don't you use seperate servlets for each task ?

and, please post some example code.
0
 

Author Comment

by:dogma
ID: 2519244

We already have over 100 servlets. Basically, it's one servlet to one page.

Each servlet can have two tasks:
1) Render the page with content
2) Process changes to the content on that page

When a doGet() is received, we render by loading values and kicking off a JSP. When a doPost() is received, we examine the FORM variables for changes, perform any update, and then doFoward() to another page depending on what was changed, context etc.. We have a very large class topology, so it's meaningless to provide too many code extracts.

This is our doForward() function which is a member of the TonicServlet class (which has been derived from HttpServlet).
 
public void doForward(String url) throws TonicForwardException{
 try{
  String encoded_url = getResponse().encodeURL(url);
  getResponse().sendRedirect(encoded_url);
  throw new TonicForwardException();
 }
 catch(IOException exception){
  exception.printStackTrace();
 }
}

As you can see: it's actually a temporary redirect (which some browsers report annoying messages about). But it works for GET.

We pass the URL (e.g. http://server/MyServlet) which then applies any encoding (to add the SessionID etc.). The TonicForwardException serves to allow the current servlet to close without doing anything else (as control of output is handled by the forward servlet).

This function always sends the various paramters by GET - ie. the next servlet will render. However, there are circumstances where we want to be able to send parameters directly to another servlet for processing (performing the POST function).

So, we need a function to be of the form:

doForward(String url,boolean usePost);

So how is it done?

0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2519370
in fact you are returning 'Resource moved' response to the client browser, which send another request to the new resource location. you can't 'redirect' POST request this way (because 'resource moved' returns only new URL as parameter) .

I don't see why you need this additional traffic (response + another request) - when you can implement all your logic in one pass. fo example you can use

getServletContext().getRequestDispatcher(servletURL).forward(request, response);
(Servlet API 2.1)

0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

Author Comment

by:dogma
ID: 2522108
Exactly.

forward() (as stated earlier) always sends by GET. How do I send by POST?
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2522268
1.
.....
public void doForward(String url) throws TonicForwardException{
 try{
  String encoded_url = getResponse().encodeURL(url);
  getResponse().sendRedirect(encoded_url);

....

I can see only sendRedirect in your code (no RequestDispatcher at all).

2.
>> forward() (as stated earlier) always sends by GET.

what's your environment ? (web server / servlet runner etc.)

3. you'd better put all your 'application logic code' outside of servlets. use servlets to parse the extract parameters from the HttpRequset, and handle this request in some other Object (this way you won't need servlet forwarding at all)
0
 

Author Comment

by:dogma
ID: 2522513
OK, let's boil it down... I have used forward(), but couldn't get it to work as I needed, so I reverted to the sendRedirect() which did - but Netscape whinges.

So, now I want to know how to get the forward() method to work as required.

My further investigations have shown that:

1) If a doGet() is received by a servlet, and a forward() is made to another... the doGet() of the second servlet is called.

2) If a doPost() is received by a servlet, and a forward() is made to another... the doPost() of the second servlet is called.

Therefore, the question now becomes...

How can I do the following:

3) If a doGet() is received by a servlet, and a forward() is made to another... the doPost() of the second servlet is called.

and

4) If a doPost() is received by a servlet, and a forward() is made to another... the doGet() of the second servlet is called.

I'm sorry if my question was unclear. You'll have to forgive me as I've only been doing Java for three weeks! Let alone Servlets!

tia.
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 300 total points
ID: 2522674
it seems to me that you can't use forward() in this situation.
(you can try to find second servlet by name and call it's doPost method directly)

I would recomend you to change your design - put all your logic in seprate objects.
your servlets will parse the http parameters and call the appropriate method of the business object
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

721 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