wood burning stoves 2.0*
The moose likes Servlets and the fly likes An objective question on servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "An objective question on servlets" Watch "An objective question on servlets" New topic
Author

An objective question on servlets

Sumit Aggarwal
Greenhorn

Joined: Jul 31, 2011
Posts: 11
I recently came across a question,

If in a web container we have two applications A & B deployed and we need to send information from a servlet of application A to a servlet of application B, so that the information is not exposed to other servlets of application A or B. Which thing we should use to achive this objective.

1. HttpServletRequest
2. HttpServletResponse
3. ServletContext
4. ServletConfig
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
ServletContext is one for whole of the application. So it can be accessed by all the servlets and Jsp.

ServletConfig is one for each servlet.

HttpServletRequest can help you if you disptach the request. (by setting the request attribute)

Let me know if i am wrong somewhere.

Thanks


kunal
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 998
    
    2
these threads might help..

this

this

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

What is the source of this question?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sumit Aggarwal
Greenhorn

Joined: Jul 31, 2011
Posts: 11
@Bear : it is an interview question<objective screening interview>
@Kunal : One among them is correct, even i don't know the correct answer.
@Punit : Thanks for the related threads
Swetha Bhagavathula
Ranch Hand

Joined: Jan 04, 2011
Posts: 112

Sumit Aggarwal wrote:I recently came across a question,

If in a web container we have two applications A & B deployed and we need to send information from a servlet of application A to a servlet of application B, so that the information is not exposed to other servlets of application A or B. Which thing we should use to achive this objective.

1. HttpServletRequest
2. HttpServletResponse
3. ServletContext
4. ServletConfig


the above question seems to be like under the topic of servlet-chaining where one servlets communicates with other..
there are two possibilities .. where:
A) both servlets /JSPs(web resource programs) residing same web application of same web server.
for this we can use either HttpServletRequest or HttpServletResponse..or ServletContext object.. (better to use ServletContext)
request object comes under sending the request nothing forwarding the request response object carries the result..

b)one servlet component residing in one web applicatopn and another servlet/JSP residing in another web application of same server..
for this we can use only ServletContext object because ServletContext object(object of the class which is implementin javax.servlet.ServletContext interface) is visible to the web applications residing in same web server.

where as the request and response object are visible only to that particular web application.. not out of that web application.. So according to your question i think it is ServletContext.

ServletConfig is use to gather the details of web.xml in your servlet program ,so the option 4 can never be the answer.. and option 1 and 2 cannot send any request or response if the servlet component residing out of the web application.. hence option 3 may be correct..

if I am wrong I request to correct my statements.. thanku

SCJP5
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 513

Swetha Bhagavathula wrote:we can use only ServletContext object because ServletContext object(object of the class which is implementin javax.servlet.ServletContext interface) is visible to the web applications residing in same web server.

Sorry, I beg to differ on this..
ServletConfig is one per application and its not visible across applications on the same web server.


Regards, Prasad
SCJP 5 (93%)
Swetha Bhagavathula
Ranch Hand

Joined: Jan 04, 2011
Posts: 112

Prasad Krishnegowda wrote:
Swetha Bhagavathula wrote:we can use only ServletContext object because ServletContext object(object of the class which is implementin javax.servlet.ServletContext interface) is visible to the web applications residing in same web server.

Sorry, I beg to differ on this..
ServletConfig is one per application .

hi..ServletConfig is one per servlet program.not one per web application
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Swetha Bhagavathula wrote:
Prasad Krishnegowda wrote:
Swetha Bhagavathula wrote:we can use only ServletContext object because ServletContext object(object of the class which is implementin javax.servlet.ServletContext interface) is visible to the web applications residing in same web server.

Sorry, I beg to differ on this..
ServletConfig is one per application .

hi..ServletConfig is one per servlet program.not one per web application

There's no such thing as a "servlet program".

ServletConfig is specific to servlets that are created from a single servlet declaration in the deployment descriptor.

ServletContext is specific to each web app.
Swetha Bhagavathula
Ranch Hand

Joined: Jan 04, 2011
Posts: 112

so should i use servlet program,servlets ,servlet component?can anyone please tell what is the difference between each of them?Is saying Servlet program quite wrong? I request to reply and want to correct myself.. Thank you
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

It's just semantics. The term "program" is usually taken to mean something that starts and stops. It's not accurate and could be confusing to use it to describe a servlet which stays in memory awaiting requests to process.
Aman Tandon
Greenhorn

Joined: May 19, 2012
Posts: 10
Actually it is very simple my friend...
consider an app 1 from which you want to access app2 demo3 page okay...
we can't get out our context but that app2 context can be come in our app1...so now the point is how we do it..
so what you have to do...just simply firstly get the context of your app1
ServletContext ctx= getServletContext();
and then ServletContext ctx1= ctx.getContext("http://localhost:7001/app2");// to get the context of another app using servlet context method getContext()and give the absolute path in this.
then use the request dispatcher object to get to that app2's page demo3
RequestDispatcher rd = ctx1.getRequestDispatcher("/demo3");
rd.forward(req,res);

reply if you don't find it suitable...

Aman Tandon
Prasoon Kumar Mishra
Greenhorn

Joined: Mar 20, 2012
Posts: 6

you have to use servletConfig ,if not then in such kind of scenario which is appropriate to use can you please clarify?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Prasoon Kumar Mishra wrote:you have to use servletConfig

Not correct.
Swetha Bhagavathula
Ranch Hand

Joined: Jan 04, 2011
Posts: 112

but we should also remember one key point that using rd.forward(req,res)... all the statements placed before and after rd.forward(req,res) in the source servlet component/program will be executed but the html output will be discarded and only the output of the destination program is sent to browser window ..

so, rd.forward(req,res) is useful to configure Error Servlet(the servlet that executes only when exception is raised in other servlet component/program of web application having the capability of generating non-technical messages representing the exceptions) in java web application.

use rd.forward(req,res) only such above situations otherwise write logic in your source servlet program/component such that when it is executed by the container it itself should have the ability to send the response to the browser window.

also recommended to call rd.forward(req,res) in source servlet program by verifying certain conditions.


but when we use rd.include(req,res),the html output of source servlet component and destination component(can be anything servlet/JSP) together goes to browser window as response...and here no html output will be discarded.. also don't commit(closing stream objects )in source servlet component before rd.forward(req,res) method calls.This may fail entire servlet-chaining...


finally I would like to say that :
1) if both source and destination components residing in same web application or different of same server better to go with ServletContext(so answer would be ServletContext object)
2)if both source and destination components residing in two different web applications of different servers better to go with response.sendRedirect(s2url) (where s2url represents the destination web resource component url pattern(that could be either sevrlet or JSP).

let me please know whether I am correct ..?
Aman Tandon
Greenhorn

Joined: May 19, 2012
Posts: 10
Yes, you is absolutely right we can also use it too..
in my mind that i idea come...but you did the good job to tell the difference about include() and forward() if somebody who don't know about can get the wrong output in his aspect..

Thanks Swetha.
Swetha Bhagavathula
Ranch Hand

Joined: Jan 04, 2011
Posts: 112

I am thankful to javaranch...
Aman Tandon
Greenhorn

Joined: May 19, 2012
Posts: 10
yeah!...
Sumit Aggarwal
Greenhorn

Joined: Jul 31, 2011
Posts: 11
Thanks for making it an interactive and knowledgeable thread. Got my answer
Aman Tandon
Greenhorn

Joined: May 19, 2012
Posts: 10
@sumit pleasure
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: An objective question on servlets