File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Why Servlet return a blank page? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why Servlet return a blank page?" Watch "Why Servlet return a blank page?" New topic
Author

Why Servlet return a blank page?

tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
Hi,

I created a form and then processed it.

Before I process, I debug both the form and the servlet. No errors.

But, when I filled up the form which has a post to a servlet.

The servlet gives me a blank page...as in the tomcat server gives me a blank page in my browser.

By right, it is supposed to give me a statement that says "records successfully added" and my ms access database will be updated with the new records. None of these happen.

Hope to hear why.

Thanks.
manoj r patil
Ranch Hand

Joined: Jun 06, 2002
Posts: 181
How did you verify that its calling the correct servlet?

When you said "I debug both the form and the servlet", I expect you did it at runtime uaing some IDE like eclipse. So was the code executing your business logic?


love your job and not your company;
...because you never know when your company will stop loving you!
tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
I only have one servlet inside. ...it's just a simple program....also I'm not using eclipse...just using IDE...that's all.

Any idea where I had gone wrong?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Are you perhaps printing to System.out? You shouldn't, servlets have their own output stream which you can get using the servlet response's getPrintWriter (for text) or getOutputStream (for binary data) methods. Just don't use both methods in the same servlet.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
I am using out.println.....as per my partial code below. Anything wrong with using that?



ps.setString(9, strFrequency);
for (int i=0; i < strTutors.length; i++){
ps.setString(10, strTutors[i]);
}
ps.setString(11, strBudget);
ps.setString(12, strRemarks);
ps.executeUpdate();
response.setContentType("text/html");
out.println("Member account has been created"); [/code]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You're giving us almost no information to work with. How about using DEBUG-level log statements and checking the logs???
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
in your form are you using POST or GET, and which method did you implement in your servlet, doPost or doGet?


Do you know why this cup is useful? Because it is empty.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

tangara goh wrote:
Are you aware that you are overwriting the 10th parameter each time? You might as well write "ps.setString(10, strTutors[strTutors.length - 1]);"
tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
Hongli Li wrote:in your form are you using POST or GET, and which method did you implement in your servlet, doPost or doGet?


In my form, I'm using Post.

In my servlet, I have used the overriding method over do Get and do Post, because I was told that this way works. Is it wrong?


As shown here,


tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
Rob Prime wrote:
tangara goh wrote:
Are you aware that you are overwriting the 10th parameter each time? You might as well write "ps.setString(10, strTutors[strTutors.length - 1]);"


I'm not aware of this.


In my form, I have checked boxes and user may tick as many checked boxes so the parameters will read whatever ticked boxes and parsed it into the database.





Is the codes wrong?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

tangara goh wrote:Is the codes wrong?

Yes.
tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
OK. It must be the servlet that is wrong right?

So, I'll have to follow Rob Prime's suggestion then.

O my......I'm like hopeless when comes to something that I'm not familiar with.....Can somebody enlighten me how you guys learn programming?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You're missing Rob's point--you're trying to stuff several values into a single column: it doesn't work like that. If a user can have multiple subjects then in general you'd need another table that either has a user id/subject, or a mapping table of user id/subject id.

How to learn how to program? Program a lot. Read a lot about programming. Read other people's code, and try to improve it. Practice, practice, practice.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

tangara goh wrote:In my servlet, I have used the overriding method over do Get and do Post, because I was told that this way works. Is it wrong?

Wait, what's processRequest? I don't see it in HttpServlet.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

That's because it isn't. In fact, all of JEE's API knows no such method. There is the service method that can be overridden, but HttpServlet subclasses should override doGet or doPost instead.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
One of the best ways to learn servlets and jsp is to study the examples that come with Tomcat.

Try modifying them to see what happens so that you are starting with working code. Modifying working code is much better than trying to write a whole new servlet or jsp.

As I recall, one way to get a blank page response is to fail to flush and close the output stream.

Bill
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
tangara goh wrote:
In my servlet, I have used the overriding method over do Get and do Post, because I was told that this way works. Is it wrong?


There are fundamental differences in POST and GET, GET is idempotent, which means no more you call it once or many times, the side effects to the server will be the same. and POST is not. When you are retrieving data from server, GET is the one you should use, When you are inserting data you POST. PUT is for updating and DELETE for removal.
tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
First, thank you guys for pointing out so many of my mistakes....looks like I have to pore over the books for the fundamental again.

There's this book that I just read - Better, Faster, Lighter Java by Bruce A.Tate & Justin Gehtland,2004. Not sure if the book is outdated but one of the chapters recommend using Hibernate.


There are only 5 basic steps involved. I find it something that looks easy...correct me if I'm wrong.

However, the book only illustrates a simple example. And I'm not sure how to start.

I hope to know what are the books or websites I can study for beginner like me.

Also, is it foolish to jump to Hibernate when I haven't even really grasp the basic JDBC using Tomcat?

Really hope you guys can share with me your views. Many thanks.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Just to repeat myself, study the examples that Tomcat comes with.

I heartily recommend using the FireFox browser with the plugins such as FireBug and LiveHTTPHeaders - they will show you what is really going on if you get a "blank page" or any other mystery on the client side.

Bill
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

tangara goh wrote:Also, is it foolish to jump to Hibernate when I haven't even really grasp the basic JDBC using Tomcat?

Yes. How will you debug Hibernate issues if you don't understand the underlying technology?
tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
William Brogden wrote:Just to repeat myself, study the examples that Tomcat comes with.

I heartily recommend using the FireFox browser with the plugins such as FireBug and LiveHTTPHeaders - they will show you what is really going on if you get a "blank page" or any other mystery on the client side.

Bill


OK. I searched for the examples inside my IDE but can't find the examples.

I doubt you are talking about the contents under the Help tab inside the IDE....could you be more explicit whereabout I can find the examples? Many thanks.
manoj r patil
Ranch Hand

Joined: Jun 06, 2002
Posts: 181
tangara goh wrote:I searched for the examples inside my IDE but can't find the examples.


Why are you checking in IDE? Its been clearly told that the examples are distributed along with Tomcat. Run tomcat as a standalone server and visit its home page, you should be able to see those examples unless you have custom installation.
tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
manoj r patil wrote:
tangara goh wrote:I searched for the examples inside my IDE but can't find the examples.


Why are you checking in IDE? Its been clearly told that the examples are distributed along with Tomcat. Run tomcat as a standalone server and visit its home page, you should be able to see those examples unless you have custom installation.


Please bear with my ignorance for I always have this thinking that Tomcat server is inside every Netbean IDE and so I looked inside IDE.


Is this the URL you are referrring to ?
http://tomcat.apache.org/

but I can't find any examples you mentioned. When I clicked on the Java server pages, it brought me to Oracle page.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

In the Tomcat download.

That aside, there are hundreds, if not thousands, of Java web application tutorials and examples on the web, including Sun's (Oracle's) own tutorials.

http://java.sun.com/j2ee/1.4/docs/tutorial/doc/WebApp.html

It'll be really, really, *really* important to learn how to learn about these things, how to find examples, how to read forums to find solutions, etc. It's also very important to have strong fundamentals: without them, little else will make sense, and when things go cow's legs up, not knowing how to examine the various layers involved will make it literally impossible to figure out what's gone wrong.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
And THAT is why I do not recommend that beginners start learning servlets with an IDE.

Bill (the old curmudgeon)
tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
David Newton wrote:In the Tomcat download.

That aside, there are hundreds, if not thousands, of Java web application tutorials and examples on the web, including Sun's (Oracle's) own tutorials.

http://java.sun.com/j2ee/1.4/docs/tutorial/doc/WebApp.html

It'll be really, really, *really* important to learn how to learn about these things, how to find examples, how to read forums to find solutions, etc. It's also very important to have strong fundamentals: without them, little else will make sense, and when things go cow's legs up, not knowing how to examine the various layers involved will make it literally impossible to figure out what's gone wrong.


Thank you David. The link you provided is got lots of information. Thank you very much.
tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
William Brogden wrote:And THAT is why I do not recommend that beginners start learning servlets with an IDE.

Bill (the old curmudgeon)


But, is there any other way to execute the servlet without IDE? I don't think notepad ++ can allow servlet to run right?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

tangara goh wrote:But, is there any other way to execute the servlet without IDE? I don't think notepad ++ can allow servlet to run right?

And this is why William recommends against IDEs.

You run Tomcat (or other container) as a real server rather than relying on an IDE to do it for you.
Prabhat Shankar
Greenhorn

Joined: Oct 08, 2009
Posts: 27
Dear Tangara,


First of all read all concept of servlet and after that try to run existing example.
Then you will not get this type of problems.










Prabhat Shankar Consumer Court,Consumer Court India,Consumer Complaints,Complaint India, Web Value
srinivas kotagiri
Greenhorn

Joined: May 14, 2010
Posts: 1
[Hijack removed. Please ask your own questions in your own new topic.]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Servlet return a blank page?