• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

An objective question on servlets

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
these threads might help..

this

this

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the source of this question?
 
Sumit Aggarwal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
 
Ranch Hand
Posts: 112
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 672
4
Eclipse IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Swetha Bhagavathula
Ranch Hand
Posts: 112
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 112
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 6
Oracle MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you have to use servletConfig ,if not then in such kind of scenario which is appropriate to use can you please clarify?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasoon Kumar Mishra wrote:you have to use servletConfig


Not correct.
 
Swetha Bhagavathula
Ranch Hand
Posts: 112
Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 112
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am thankful to javaranch...
 
Aman Tandon
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah!...
 
Sumit Aggarwal
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for making it an interactive and knowledgeable thread. Got my answer
 
Aman Tandon
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@sumit pleasure
 
reply
    Bookmark Topic Watch Topic
  • New Topic