• Post Reply Bookmark Topic Watch Topic
  • New Topic

No output from servlet.

 
jim wallis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jsp file:
<%@ page buffer="5" autoFlush="false" %>
Jim1
<jsp:include page="/servletprogram" flush="true"/>
Jim2

servlet:
public class servletprogram {
public void doGet (HttpServletRequest req, HttpServletResponse res)
{
String Doc = "This is test";
req.setContentType("text/html"); //tell browser what is coming.
ServletOutputStream out = response.getOutputStream(); //obtain a writer.
out.println(Doc);
out.close();
}
}
All I get back on the browser is Jim1 and Jim2.

I have tried using Printwriter (same results), and changed the jsp:include to jsp:forward (bombs big time). Any thoughts on what I'm doing wrong?
Yes, I'm new jsp's.
Thank you in advance,
Jim
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your servlet in a package other than the default?

And it's rather odd to be invoking a servlet from a JSP rather than forwarding to the JSP from a servlet. Is this just an exercise?
 
Siyaa Hoffman
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Few things that I see wrong here -

1. I do not think you can define buffer as buffer="5". You have to specify buffer="5kb"

2. Your servlet class does not extend HttpServlet which is essential.

3. To write the String out in the servlet, you should take the PrintWriter using the API call response.getWriter()

4. You are closing the response stream in the servlet, so you would not be able to see "Jim2"


-Siyaa
 
jim wallis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank youfor the replies. To answer the questions:
1). My servlet is in a package.
2). No, this is not an exercise. I wish it were so I could walk away from this problem. I work for a large railroad and this this code was given to me to make a change. The original author has left the company.
3). I made the change from buffer="5" to buffer="5kb" as per your suggestion.
4). The servlet does extend the HTTPServlet class, I left it off of the example just to make the example easier to look at.
5). I changed the ServletOutPutStream to PrintWriter and the messge returned to the browser is:
Invalid at the top level of the document. Error processing resource.
I'm attempting to run that message down for more clues.
6). I also removed the out.close(); statement thinking that was causing a problem. I wasn't.

I'm still digging around so if you have any other comments/suggestions please send them on as any new perspective is always welcome.
Thanks again,
Jim
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, this is not an exercise. I wish it were so I could walk away from this problem. I work for a large railroad and this this code was given to me to make a change. The original author has left the company.


Been there. My commiserations.

The servlet does extend the HTTPServlet class, I left it off of the example just to make the example easier to look at.


Try not to do that; as you can see, it just causes confusion.

Things that occur to me:

- I never use the page buffer setting at all, the default is usually fine

- Can you hit the servlet directly and get the expected output?

- Are you sure that the servlet is being invoked at all? (System.out.println as a last resort)
 
jim wallis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about the confusion on the HTTP extention not being included. Tried to avoid confusion and just created some. Not what yo want to do when you need to drain the swamp. Yes, the servlet is being invoked as I do a System.out.println just before the doing the out.println to the browser. As for calling it directly, no I can't and I'm sure why. Monday I guess I will have to talk to the group that does Apache/Tomcat and find out why not. Don't really like talking to them as they always say they are there to help but aren't really any help at all most of the time. They seem to be always worried that you are looking to blame them for something. I just can't figure out where that output is going. I had a thought it may be going to the browser and then the was overwritten by the jsp but I don't know to verify that.
Thanks again,
Jim
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you removed the close as Siyaa suggested?
 
shriram kutty
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
When i run following code on was3.5 i get the output ::
abc This is test abc2
in the browser.

the jsp is ::
<%@page buffer="5kb" autoFlush ="false" %>
abc
<jsp:include page="ServletProgram" flush="true"/>
abc2

the servlet is ::
import java.io.IOException;
public class ServletProgram extends javax.servlet.http.HttpServlet {
public void doGet (javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
{
String Doc = "This is test";
res.setContentType("text/html"); //tell browser what is coming.
try{
java.io.PrintWriter out = res.getWriter(); //obtain a writer.
out.println(Doc);
//out.close();
}catch(IOException io)
{
System.out.println("IO Error");
}
}
}

May be the correction to your code is just removing the '/' before 'ServletProgram'
 
jim wallis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmmmm. Yes, I had removed the close. And tried removing the /. All to no avail. Since this works on another system, I don't think the problem is in the code. I'm thinking there is something in the Tomcat configuration. On to the guru's.
Thanks for the suggestions,
Jim
 
shriram kutty
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing you should keep mind that the servlet should be in the classpath.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suspect this un-needed line is causing a problem:
req.setContentType("text/html"); //tell browser what is coming.
The JSP already set the content type automatically and the output has been flushed - I think you get an exception if you try to change the header after the output has been flushed.
Bill
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good catch William! Even if it doesn't solve this problem, you should remove it as it could cause other problems in the future.

The basis of all this is because the response headers need to be sent to the client before any body content. So once any content has been sent, the headers are gone and can no longer be set. This is why there are so many restriction around "comitting" the request.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!