Calling servlet from servlet .

Calling servlet from servlet .

How to call a servlet from another servlet in Java?

View Answers

May 11, 2012 at 11:24 AM

You can call another servlet by using requestDispatcher() or sendRedirect() according to application requirement.

     import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletContext;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;


public class CallServlet extends HttpServlet {



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Calling another servlet by using RequestDispatcher...");
        ServletContext context=this.getServletContext();
        RequestDispatcher requestDispatcher=context.getRequestDispatcher("/servlet1");
        requestDispatcher.forward(request, response);
            System.out.println("Calling another servlet by using SendRedirect...");
          response.sendRedirect("/Servlet2");
    }


}

In RequestDispatcher the target servlet contain the same request/response object.So you can pass data between them by setting attribute as request.setAttribute() and get data by request.getAttribute().

In sendRedirect() new request is generated from the client side so you can pass data only through session or setting parameter as (url?name=value);









Related Tutorials/Questions & Answers:
Calling servlet from servlet .
Calling servlet from servlet .  How to call a servlet from another... ServletException, IOException { System.out.println("Calling another servlet by using...); System.out.println("Calling another servlet by using SendRedirect
calling servlet from jsp
calling servlet from jsp  how to call a servlet from jsp
Advertisements
Calling a jsp page from Servlet
Calling a jsp page from Servlet  How can I do this? Suppose I have jsp page aaa.jsp. From aaa.jsp on form action I have made a call to a servlet xxx.java. In xxx.java I have written code to retrieve data from database through
calling servlet from JS and return response from servlet to JS
calling servlet from JS and return response from servlet to JS  hello... page when user clicks on a button i am calling a javascript function(eg myFunc) which inturn calls a servlet(myServlet).servlet performs some DB related
calling servlet from jsp in netbeans ide
calling servlet from jsp in netbeans ide  I have tried to call servlet from jsp code in netbeans for checking the database values . but while running it showing the error that the resource not available. i dono wat mistake i did
calling a session bean bean from servlet using netbeans - EJB
calling a session bean from servlet using netbeans  How to call a session bean from servlet using netbeans in Java
calling one jsp from another jsp - JSP-Servlet
calling one jsp from another jsp  Hi All, In my web application... in two.jsp by using jsp declarative tag. Now from one.jsp file I want to call... start from where we call this two.jsp files method. i.e. in one.jsp file at line
calling function - JSP-Servlet
calling function  Hai, How to call a java file in jsp file?  Hi friend, Step to be remember for solving the problem : 1.Create a jsp page "test.jsp" Example of Extends Attribute of page
Redirect from servlet to servlet
Redirect from servlet to servlet  I want to insert data from Text box... to redirectServlet , and from this servlet I want to again forward my page to insertServlet(i.e...; <servlet-name>redirectServlet</servlet-name> <servlet
Sending form data from HTML page to SQLserver 2005 database by calling servlet code
Sending form data from HTML page to SQLserver 2005 database by calling servlet... from html page to database by calling servlet code from html page .   ... the servlet mapping in web.xml: <servlet> <servlet-name>
calling java beans - JSP-Servlet
calling java beans  Sir, I want to know where to place the java beans java file and class file inside tomcat web server. and how to call them from jsp file.  Hi Friend, Java Bean is placed in classes\form
calling zipping function - JSP-Servlet
calling zipping function  Hi, Is it possible to zip a jsp file. I wrote a java function for zipping a file.How to call that java file (zipping function ) in JSP ? pls help me Thanks.... 
calling java method from html form with out using javascript - JSP-Servlet
calling java method from html form with out using javascript  How can i call java method from a HTML form, java script should be disabled?  Hi <html><head><title>Test Input Validation</title><
Call servlet from javascript - JSP-Servlet
Call servlet from javascript  Hi in my application i have jsp that designs view, javascript for validation and servlet that perform business logic.../jqueryPostData.shtml Thanks    i am calling servlet in this manner var answer2
Navigate from jsp to servlet - JSP-Servlet
Navigate from jsp to servlet   Hi Friends, Sample code for Navigating a page from jsp to servlet. Thanks
how to fetch data from servlet ????
how to fetch data from servlet ????  how to fetch data from servlet
ArrayList from JSP to Servlet - JSP-Servlet
ArrayList from JSP to Servlet  Hi, I have an arraylist declared in a scriplet in a jsp page. how can i access the arraylist in a servlet which...)); %> How can I access this arraylist in a servlet. How
Servlet
Servlet  What is Servlet
servlet
servlet  how to interact with a servlet from a swing program
Servlet
Servlet  how to navigate one servlet page to another servlet page
write to file from servlet - JSP-Servlet
write to file from servlet  Hi, I have a jsp file where I input data and retrive data through servlet. However; when I edit data it is not showing... and POST methods. * @param request servlet request * @param response
Getting data from servlet into javascript
Getting data from servlet into javascript  How do i get json data from my servlet on to a variable in javascript n bind the data to display onto sigma grid.Has anyone Idea how to do
servlet
servlet  can i stoar record in variable which selected from table in servlet
Pass a dom object from jsp to servlet
Pass a dom object from jsp to servlet  I am creating a dom object in my jsp page. now i want to pass that object in a servlet through calling servlet in jsp. can anyone help me
servlet
servlet  is there any way to include pdf's in servlet
servlet
servlet  How many times the servlet is accessed
servlet
servlet  what are the methods and interfaces in the servlet api ?   Servlet Tutorials
servlet
servlet  how to read a file from different folder using filereader in servlet   Hello Friend, Please visit the following link:ADS_TO_REPLACE_1 http://www.roseindia.net/servlets/servlet-read-file.shtml Here you
servlet
servlet  what are the all necessary configuration to run a servlet
servlet
servlet  I designed 1 html form & a servlet but when I click on form I don't get output of servlet Please help
servlet
servlet  I designed 1 html form & a servlet but when I click on form I don't get output of servlet Please help
servlet
servlet  i want to create a login page with servlet using database mysql? only in servlet not in jsp plzz help me out
Calling from JSP
Calling from JSP  how can we call a class file from JSP   Hi, Do you want to call a servlet or a Java Bean through JSP? Please clarify this. For more information, visit the following link: JSP Tutorials Thanks
Servlet
override Service method in the servlet when you extend GenericServlet to create servlet as it is mandatory to override it. But, when you extend HttpServlet to create a servlet then you can't override service method as there is a need to override
Java from JSP - JSP-Servlet
Calling Java from JSP  Does anyone have an example of Calling Java from JSP
servlet
servlet  i want a program for counting the no of times the servlet has been invoked
Servlet
Servlet  What must be implemented by all Servlets?    The Servlet Interface must be implemented by all servlets
servlet
servlet  how to create a login form using servlet using submit,edit delete button
servlet
servlet  dear sir servlet and html not run on eclips plz help me
servlet
of the Servlet API. It contains the classes necessary for a standard, protocol-independent servlet. Every servlet must implement the Servlet interface in one
servlet
servlet file which prints out the user's inputs. I need to use the post method to pass the data from html to the java servlet and also use both doGet and doPost methods in the servlet. I think, but unfortunately I have a terrible teacher
redirect to multiple links from servlet
redirect to multiple links from servlet  hello , In my servlet page , i'm using response.redirect("www.somewebsiteaddress.com"); after it shows the output in browser,i need to redirect to another link like the above line. i
Servlet
Servlet  I want to know the steps to write a simple servlet program... .   Hello Friend, Follow these steps: Put servlet-api.jar inside the lib folder of apache tomcat. 1)create a servlet. import java.io.*; import
Servlet
Servlet   Why is Servlet so popular?   Because servlets are platform-independent Java classes that are compiled to platform-neutral byte code that can be loaded dynamically into and run by a Java technology-enabled Web
servlet
servlet  i want to use servlet application on my web page then what should i do. I have already webspace.   Hi Friend, Please visit the following link:ADS_TO_REPLACE_1 Servlet Tutorials Thanks
Servlet
Servlet  Can a user defined function be included in a servlet? I need information regarding servlet syntax and not JSP.   Yes, you can create user defined function in Servlets. Have a look at the following link: http
servlet
servlet  I want the full coding for uploading a file in local drive D usin jsp,java class and servlet... help me thanks in advance....   ... and servlet please and thanks in advance
Servlet
Servlet  Hi, Can any one please expalin me below topics SERVLET ENGINE 2.WHY SUPER.INIT(); Thanks alot in advance!! Regards: Akash
servlet
servlet   Dear Deepak, is it compulsary to write the sevice() becoz i ve seen some example which does not ve sevice()..is it tue? plz replay me with thanks praveen
servlet
servlet  plz can anyone give me the link of javax library jar file. i badly need that. thanks in advance   Please visit the following link: Download Servlet API

Ads