Sei sulla pagina 1di 7

Calling Servlet from Servlets in Java | Techartifact

1 of 7

http://www.techartifact.com/blogs/2011/03/calling-servlet-from-servlet...

About
Disclaimer

564Subscribers 237Fans 70Followers


Home
Oracle ADF
Java
Spring
.NET
News
Join Us
4

Calling Servlet from Servlets in Java


Vinay 02 Mar 2011 Java, servlets Views: 6,554 views
I gathered information from environment and sharing you this information with all.
When Ever you want to call any servlet from another servlet We can use two ways:A servlet can make an HTTP request of another servlet. Opening a connection to a URL
A servlet can call another servlets public methods directly, if the two servlets run within the same
server.
I will let you know the second way to calling the servlet. To call another servlets public methods directly,
you must:
You Should know the name of servlet that you want to call.
Acquire access to that servlets Servlet object
Calling the servlets public method
To get the object of servlet, use the ServletContext objects getServlet method. Get the ServletContext
object from the ServletConfig object stored in the Servlet object. An example should make this clear.
When the EmployeeDetail servlet calls the BookDB servlet, the EmployeeDetail servlet obtains the
EmployeeDB servlets Servlet object like this:
Once you have the servlet object, you can call any of that servlets public methods. For example, the
EmployeeDetail servlet calls the EmployeeDB servlets get getEmployeeDetail method:
You Should take care of the few things.If your servlet is following the singlethreadedModel interface then
your call violate that single threaded model. Then you should implement the first way..
public class EmployeeDetail extends HttpServlet {
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{

13-02-2013 15:58

Calling Servlet from Servlets in Java | Techartifact

2 of 7

http://www.techartifact.com/blogs/2011/03/calling-servlet-from-servlet...

...
EmployeeDBdatabase = (EmployeeDB)
getServletConfig().getServletContext().getServlet(employeedB);
EmployeeDetail bd = database.getEmployeeDetails(empId);
...
}
}
29 Votes
0

Vinay
I am software professional, working on Java/J2EE/ADF technologies i.e
Java,J2ee,Oracle ADF,hibernate,J2ee,PL/sql,Apps for 5+ years.I am
passionate about learning new technologies.I am sharing my knowledge. Give
your views and suggestion on vinay[at]techartifact.com
More Posts - Website
Follow Me:

Related Posts
creating a basic skeleton to invoke an AM in Java Class in Oracle ADF
01 Feb 2013
Java program to SORT MAP based on KEYS
08 Jan 2013
Why use hashMap when ConcurrentHashMap is there | Techartifact
12 Dec 2012

4 Responses to Calling Servlet from Servlets in Java

1.
Mirko
Mar 02, 2011
Why dont you use a RequestDispatcher?

13-02-2013 15:58

Calling Servlet from Servlets in Java | Techartifact

3 of 7

http://www.techartifact.com/blogs/2011/03/calling-servlet-from-servlet...

reply

2.
Pravin Jain
Mar 02, 2011
The method getServlet has long been deprecated for security reasons, and will not give direct
access to a Servlet, you may however use the getnamedDispatcher method to get a
RequestDispatcher and then forward to it.
reply

Ricky
Aug 16, 2011
A bit surpisred it seems to simple and yet useful.
reply

3.
Misterz
Mar 17, 2011
As already said, getServlet is deprecated, other than call in HTTP or using RequestDispatcher, that
are clean method of invocation, its possible to use the same aim of using public method, you can
use ServletContext
Example.
interface EmployDAO{
Employee findById(String id);

}
MyServlet implements EmployeeDAO{
init(){
getServletConfig()
.getServletContext()
.setAttribute(EMPL::DAO,this);
}
}
MyOtherServlet{
public void doGet (HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException

13-02-2013 15:58

Calling Servlet from Servlets in Java | Techartifact

4 of 7

http://www.techartifact.com/blogs/2011/03/calling-servlet-from-servlet...

EmployeeDAO dao = getServletConfig()


.getServletContext()
.getAttribute(EMPL::DAO);
Employee empl = dao.findById(request.getParameter(E_ID));

}
}
but i think its not so clean
BYE
reply

Leave a Reply
Name (*)

Email (will not be published) (*)

Website

Web Page Blocked


You have tried to access a web page
which is in violation of your internet
usage policy.
URL:
googleads.g.doubleclick.net/pagead
/ads?client=ca-

13-02-2013 15:58

Calling Servlet from Servlets in Java | Techartifact

5 of 7

http://www.techartifact.com/blogs/2011/03/calling-servlet-from-servlet...

Archives

Web Page Blocked

Top Post
Thread Pool in java - 122,281 views
Implementation of thread Pool in java - 44,349 views
Oracle ADF interview Question Part 1 - 40,108 views
Serialization Vs Externalization - 36,453 views
Anonymous Classes in Java - 35,272 views
What is Ext-Js(Extended JavaScript) - 31,741 views
Interview question on thread in Java - 30,427 views
Interface vs Abstract Class - 29,911 views
Marker interface in java - 29,040 views
Hello World Tutorial in ADF - 23,440 views

13-02-2013 15:58

Calling Servlet from Servlets in Java | Techartifact

6 of 7

http://www.techartifact.com/blogs/2011/03/calling-servlet-from-servlet...

You have tried to


access a web page
which is in
violation of your
internet usage
policy.
URL:
googleads.g.doubleclick.net/pagead
/ads?client=capub-7884501643823458&
output=html&
h=600&
slotname=2524100578&
w=160&
lmt=1360750991&
flash=11.4.402&
url=http%3A%2F
%2Fwww.techartifact.com%2Fblogs%2F2011
servletfrom-servletsin-java.html&
dt=1360750993447&

Tags

ADF ajax ASP.NET ASP.NET 2.0 C#.NET C#.NET 2.0 code example column Design Pattern
Example extjs features get hashmap hibernate J2EE Java Java design Pattern Javascript Jetspeed2 Jquery
JSF JSF 2.0 lifecycle managed bean Memory method Microsoft Mysql Open Portal Open Source

Oracle ADF portal programmatically row Spring sql server table Task flow Techartifact Tips value
view object Web Development XML

Go To Top
Copyright 2009-2012 Techartifact.com

Web Page Blocked


Disclaimer
About
13-02-2013 15:58

Calling Servlet from Servlets in Java | Techartifact

7 of 7

http://www.techartifact.com/blogs/2011/03/calling-servlet-from-servlet...

13-02-2013 15:58

Potrebbero piacerti anche