File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

More simple doubts....errr

 
faiza haris
Ranch Hand
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
I want to forward from my Testforward servlet to TestpostInclude servlet. It can include and forward to a .jsp file perfectly but wht is the prob with servlets...do i point tothe class file or the .java for the servlet ...urghhhh
Please point all the errors that i'm making...i admit i have to learn alot

TestpostInclude.java

web.xml

Satya and ersin....can you provide with some forward/redirect/include examples??
thanx
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chapter SRV 8 of the Servlet Spec 2.3 has some good discussion re the RequestDispatcher <eye-opener>.
You should observe (I failed) that the getRequestDispatcher(...) method called in the two servlets is different. One is on the ServletContext and the other is on the ServletRequest.
I will try to compile the example and get back.
- satya
 
Axel Janssen
Ranch Hand
Posts: 2166
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Faiza,
think you missed some little point with method:

Api - Doc states:

The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. This method returns null if the servlet container cannot return a RequestDispatcher.

So its:

Because current context root is http://127.0.0.1:8080/test/
(I named the Webapp test).
There are other points which are not completly clear to me:
The

is never printed out.
Seems that forward clears the buffer.
If u try

the forward does not happen.
btw. happy birthday to wife of ersin.
Hope it helps.
Correct me if I am wrong.

Axel
confused by taglibs.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Seems that forward clears the buffer.
If u try
out.println("its not good to forget b-days");
out.flush();
the forward does not happen.

Yup, this should throw an IllegalStateException.
Acc to the SRV.8.4 Spec:

SRV.8.4 The Forward Method
The forward method of the RequestDispatcher interface may be called by the
calling servlet only when no output has been committed to the client. If output data
exists in the response buffer that has not been committed, the content must be
cleared before the target servlet’s service method is called.
If the response has been
committed, an IllegalStateException must be thrown.


What I am not clear is "Who has to clear the response buffer?"
Should I do it or will the container do it for me?
Thanks.
- satya
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Axel:
Thanks.
So finally:
CASE I:
Testforward.java

Both the out.println lines are ignored
and the servlet is forwarded.
CASE II:
The above RequestDispatcher is obtained from the request. So even the foll code works (since it resolves relative to the path of the current request).


For both the cases mentioned above, the foll. should be the code for TestpostInclude.java
TestpostInclude.java

Note, that this RequestDispatcher is obtained from the ServletContext hence the path must be relative to the ServletContext and begin with a /. Acc to the SRV.8.1.
- satya
Ps:
Axel, lets talk Taglibs, when you get a chance. In another thread ofcourse.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

out.println("its not good to forget b-days");
out.flush();
the forward does not happen.
Yup, this should throw an IllegalStateException.

For the record......
I tried this, but din't get an IllegalStateException.
I saw the first line on the browser and the forward din't happen.
Tomcat 4.0.1 on NT.
- satya
 
Axel Janssen
Ranch Hand
Posts: 2166
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Madhav Lakkapragada:

I tried this, but din't get an IllegalStateException.
I saw the first line on the browser and the forward din't happen.
Tomcat 4.0.1 on NT.
- satya

Satya:
it does. Its in the log-file but does not propagate to browser display:
log Tomcat4.0.1 on WindowsXP *

---------
Axel
* (will install Windows2000 as soon as possible )
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ummm....I verified the log files, before I posted. I did that again now after seeing your post, but I don't find anything in the logs.
I am looking in the folder %CATALINA_HOME%\logs
Assuming that I din't miss anything.
Let me know otherwise.
Thanks.
- satya
 
Axel Janssen
Ranch Hand
Posts: 2166
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Satya:
Tested three times:
The

does not work.
whereas ("/servlet/TestpostInclude") does work.
(So your Case 2 above, doesn't work on my computer .
For me this seems consistent to whats stated in the Servlet-Api:
In RequestRequest.getRequestDispatcher(java.lang.String) you can pass a relative path. The question is relative to what. The ServletApi states that its relative to the current context root. And context root is not

but:

/TestpostInclude
will be transformed to

(and this does not work)
Axel
[ February 02, 2002: Message edited by: Axel Janssen ]
 
Axel Janssen
Ranch Hand
Posts: 2166
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I copied the code from my %tomcat4-root%\logs\localhost_log.2002-02-02.txt file.
Its clearly there.
Axel
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

RequestDispatcher rd = request.getRequestDispatcher("/TestpostInclude");
does not work.

Ofcourse, it doesn't.
Thats where we started. Look at faiza's code.
whereas ("/servlet/TestpostInclude") does work.
That was your fix. I said, Thanks.
(So your Case 2 above, doesn't work on my computer
Now that is wrong. Since in my Case 2 I used "TestpostInclude" not "/TestpostInclude". (My case 2 code doesn't start with a slash "/" )
See the code carefully again.
regds.
- satya
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

For me this seems consistent to whats stated in the Servlet-Api:
In RequestRequest.getRequestDispatcher(java.lang.String) you can pass a relative path. The question is relative to what. The ServletApi states that its relative to the current context root. And context root is not

IMO, you got this worng. Check the API.. As I understand from the API, "If it starts with a '/' then it will be resolved from the root of the webapp/servletContext".
Also please read SRV.8. It is very clear in the specification.
regds.
- satya
 
Axel Janssen
Ranch Hand
Posts: 2166
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satya,
thanks for clearifying concepts.
Of course you are right.
I just have a problem with calling the starting-with-"/" syntax an absolute path.
For me an absolute path starts allways with http://.
Will check the specs chapter u mentioned.
Axel
[ February 03, 2002: Message edited by: Axel Janssen ]
 
faiza haris
Ranch Hand
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Axel and Satya for this great discussion!!!
i'll go thru all that and get back ...i hafta go thru the srv 8 in much greater detail and i still need to practice examples...to follow the specs like for example it says
srv 8.1.1
parameters in the query string used to create the RequestDispatcher take precedence over other parameters of the same name passed to the included servlet

Let me go thru the stuff ...yeah its a real i-opener
Thanx for the help
faiza
 
faiza haris
Ranch Hand
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the StackOverflowError when giving the relative path "SessionTimer" without "/" in the following code

faiza
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

RequestDispatcher rd = request.getRequestDispatcher("SessionTimer");
// /servlet/sessiontimer works fine with request

If /servlet/sessiontimer works fine then you should use
RequestDispatcher rd = request.getRequestDispatcher("sessiontimer");
NOT "SessionTimer"
Tell me that was a typo..........
- satya
 
faiza haris
Ranch Hand
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heyyyy..... the comments are just for my later reference and understanding, so i wrote in a hurry all small caps...there is no typo!!
faiza
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StackOverflowError
The only thing I would do in that case is to reboot the system and try again. Since we are dealing with a Session obj, may be it got too big...guessing.
But I don't think it is due to the request.get...(...); method.
$0.02
- satya
 
R K Singh
Ranch Hand
Posts: 5382
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Madhav Lakkapragada:

What I am not clear is "Who has to clear the response buffer?"
Should I do it or will the container do it for me?

Resurrecting the thread ... really good one.
and I read somewhere that servlet container is responsible for clearing the buffer.
CMIW
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic