This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes PrintWriter out variable used in servlet and want to use in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "PrintWriter out variable used in servlet and want to use in JSP" Watch "PrintWriter out variable used in servlet and want to use in JSP" New topic
Author

PrintWriter out variable used in servlet and want to use in JSP

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

Hello
I have declared a variable as follows and used in another class used for model

and in the doPost method of the servlet
I write

this gives me CoyoteWriter in Apache when I debugged in eclipse
now when I write

I am not getting the desired output
the servlet forwards the request directly to JSP
but on JSP I am not getting the output of displayQuestion method which is as follows


what is the problem with out variable?



SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

I'm sorry Prasad, I didn't understand your question properly the first time.

What is the method displayQuestion() you invoke? I cannot find such in the CoyoteWriter class - did you add it?

What is the code of your JSP which is not giving you what you need?

Cheers!

For what I remember, the RequestDispatcher.forward(-) will leave the code below this line and do nothing with it - it just forwards the request to the JSP for processing.

I think you should do everything you really need to do in servlet before doing a forward. Or use include and leave the out-related statements as they are.

Cheers!


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

well what you had striked down was the answer for me
now I understand that everything after forwarding the request to JSP nothing will be executed afterwords

but what I want is that
I have to call the displayQuestion() method from the servlet and display it on the JSP which has some additional design

a solution I am thinking about setting the attribute that displayQuestion returns and then get that attribute in JSP and access it
but is it a good practice to do that?
what is your opinion?
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Oh, than it's good I didn't erase it firmly ;-)

IMHO that is definitely a good solution to create a bunch of data (attributes) you need to use (access) in your presentation layer.
Thanks to this you are creating loosely coupled design in which the presentation layer (JSP) and business logic layer / controller (Servlet) are less dependent on each other.

By calling a servlet method from the JSP you are creating an additional dependency. I guess you could call this method using scripting (which is ultimately bad) or move the servlet calling code into the EL function which could do the work if you really, really need this.

So, the bottom line is that in your case I would certainly go for the attribute setting in Servlet class and JSP which accesses it. :-)

Cheers!
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

nice
that helped
I would definitely follow your advice

this is the first web application I am building and hence facing too much silly problems
but we fall and then we learn to stand up isn't it

learning is good
I will keep still asking silly questions
thanks
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Glad I could help :-)

It's just normal that when you read the book it seems so obvious and simple as you read it. You can even pass the certificate just basing on those information.

But when you face the real-life problems, start to actually develop an application, it seems like it's the first time you see some things, you begin to ask the questions, become confused and so on.

And besides, sometimes when you ask the question you might even realize that you already know the answer :-)

So please ask as many relevant questions as you want - it also stimulates the answerer to refresh his memory :-)
 
GeeCON Prague 2014
 
subject: PrintWriter out variable used in servlet and want to use in JSP