Home Answers Viewqa Servlet-Interview-Questions Calling servlet from servlet .

 
 


Java Coder
Calling servlet from servlet .
1 Answer(s)      a year and a month ago
Posted in : Servlet Interview Questions

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 Pages:
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
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 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 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 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
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 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 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
Server calling of .exe file in the client
Server calling of .exe file in the client   I have a requirement... I call a .exe file from client machine? I want to run .exe from webpage like servlet. any idea
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 .   ...(); } catch(Exception e){ out.println(e); } } } 3)Do the servlet
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><
servlet
servlet  how to interact with a servlet from a swing program
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
servlet
servlet  can i stoar record in variable which selected from table in servlet
Java from JSP - JSP-Servlet
Calling Java from JSP  Does anyone have an example of Calling Java from JSP
servlet
servlet  how to read a file from different folder using filereader in servlet   Hello Friend, Please visit the following link: http://www.roseindia.net/servlets/servlet-read-file.shtml Here you will get an useful
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
servlet
"); PreparedStatement p1=con.prepareStatement("select * from Logintab where Userid=? and Username...); } } } this is the code for .java servlet am able to run sucessful but when i give wrong
servlet
com.ilp.tsi.pm.services.StockService; /** * Servlet implementation class AddServlet1 */ //Servlet for Adding the stock public class AddStockServlet extends...; import com.ilp.tsi.utils.*; /** * Servlet implementation class Changepwd
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
("empPassword"); String sql1="select PMSNUM_EMP_ID from PMST_EMP_MST
SERVLET
("empPassword"); String sql1="select PMSNUM_EMP_ID from PMST_EMP_MST
Calling Servlet to build a List of data from database and show this on the JSP page in table
Calling Servlet to build a List of data from database and show this on the JSP...;    In this example we are calling a servet to retrieve all the data from database and then add the data into list. The list
JSP-Servlet - JSP-Servlet
JSP-Servlet   how to pass the value or parameter from jsp page to servlet and view the passed value
JSP-Servlet - JSP-Servlet
JSP-Servlet   how to pass the value or parameter from jsp page to servlet and view the passed value
JSP-Servlet - JSP-Servlet
JSP-Servlet   how to pass the value or parameter from jsp page to servlet and view the passed value
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
servlet - Servlet Interview Questions
. For initialization of servlet instances the init method is used. The container initializes the servlet instance by calling the init method of the Servlet... new qustion is: why we do not need to use main method in a 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 Interview Questions
, Service method in called by the servlet container to process a request from the browser. When user calls a servlet, it's service method is executed by servlet container to process the user request. So, service method is called
JSP-Servlet - JSP-Servlet
JSP-Servlet   how to pass the value or parameter from jsp page to servlet and view the passed value.   Hi Friend, Please visit the following links: http://www.roseindia.net/tutorial/servlet/passParameters.html
servlet - Servlet Interview Questions
servlet  how to take a value from user in generic servlet using html form.please give me the code
servlet session - JSP-Servlet
servlet session  hi, im working on a real estate web site.....n im stucked....actually if a user login from any page on web site suppose hi calls login servlet fromabout us page now i want the servlet to redirect me
destroy() in servlet
by the container, will the servlet be immediately destroyed? What happens to the tasks(threads) that the servlet might be executing at that time?   Yes, but Before calling the destroy() method, the servlet container waits for the remaining
Servlet
Servlet  What is Servlet
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 Interview Questions
servlet  why servlet has three names ? give an example?  Hi Friend, A Servlet can have three names, 1) file path name-It defines the full path name of the servlet to the server. 2) deployment name-It is a secret
servlet cookies - JSP-Servlet
servlet cookies  helo sir i need a servlet program which create a cookie and add userid's and passwords and read the userid and password from... in the webinf.xml and access them in the servlet by using the getInitParameters() method
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
Servlet
Servlet  how to navigate one servlet page to another servlet page
servlet - JSP-Servlet
servlet  I m new in servelet i need help to know abt servlet how to install tomcat, how to run tomcat, how to make servlet program, how run that program in short i need help to know step by step from starting to end i dont know
Servlet Finalization
Servlet Finalization In this tutorial you will learn about what happens when a servlet is going to be finalize. A servlet container can calls the destroy() method of Servlet interface when it found that the servlet should be deleted
disable the form - JSP-Servlet
are different.when I click the form button,one function is calling and the input values(only one input value is going to servlet) are going to servlet class using... Multiple form are created in Jsp. Servlet is used to insert the data. Ajax is used
servlet
servlet  How many times the servlet is accessed
servlet
servlet  is there any way to include pdf's in servlet
servlet
servlet  what are the methods and interfaces in the servlet api ?   Servlet Tutorials