Different ways to invoke a servlet from another servlets?

Hello,
I am a bit confused today. I was thinking about all the ways by which I can invoke a servlet from another servlets. I know it can be done by creating an instance of the 2nd servlet in the 1st one. Is there any other way? such as say.... RequestDispatcher perhaps?
Here is an example to illustrate my point. I have a login.jsp which submits to login.do. login.do search through database and first authenticate email and then the corresponding password. Now its time for displayGame.do. So here how do i do? if it was a displayGame.jsp then i could have used Redirection or Forward but when it is servlets? Can I still go for Redirection or Forward??
Thank you for reading this confused post. :-)

if it was a displayGame.jsp then i could have used Redirection or Forward but when it is servlets?Question: do you know what a JSP becomes when you first run it?
Answer: a servlet.
So yes, you can use a RequestDispatcher to forward/redirect to a servlet OR a JSP, whatever you like.

Similar Messages

  • Calling a servlet from another servlet?

    Hi
    I would like to know that how can I call one Servlet from another Servlet. I have tried getServlet() method of ServletContext but it has been deprecated. I want to call a specific method of the other Servlet, is there any other way?
    Thanks

    I should have seen it from your previous post :(
    Code of Servlet 1:
    public void doGet(HttpServletRequest request,
    HttpServletResponse response) throws
    ServletException, IOException
    RequestDispatcher rDispatch = null ;
    rDispatch = getServletConfig
    ().getServletContext().getRequestDispatcher
    ("/Servlet2") ;
    rDispatch.forward(request, response) ;
    System.out.println("Back in 1");
    I assume Servlets 1and 2 are in same web app and you've mapped your servlet2 to /Servlet2 in your web.xml
    make it as
    rDispatch = request.getRequestDispatcher("/Servlet2");
    rDispatch.forward(request,response);
    Nothin's wrong with yer servlet2

  • How to call a servlet from another servlet

    hi everybody,
    i have a problem, i have to call one servlet from another one.
    Everything works on my pc, but when i install the application on the customer's server i got an error about an Uknown URL followed by the name of the machine.
    Wjat i do is the folloqing :
    String urlString = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/"+servletName;
    URL url = new URL(urlString);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    the variable servletName is the name of the servlet i have to call.
    Is there another way to call the servlet ?
    All the servlet are installed in the same server.
    Any suggestion ?
    Cheers.
    Stefano

    Sweep is correct about requestDispatcher being another approach for inter-servlet delegation; the only issue that i recall with this approach is that it defaults the method of the destination servlet to the one it was called from...for example, calling servlet2 from within servlet1.post() resulted in the dispatcher attempting to utilize servlet2.post() - i believe that i searched for a parameterize solution to no avail :( (ended up handling the request by placing a "fake" doPost() in servlet2 that simply called servlet2.doGet())
    however, if your application is functioning correctly on your pc/webserver then the problem may be external to servlet communication (e.g. client webserver's ports not configured or blocked, missing runtime classes, etc.)
    my suggestion would be to set aside the programmatic concerns for the moment - what is the response if you open a browser on a client's machine and access the URL in question (i.e. http://clientserver:port/stefanoServlet)? If it will not respond to access in this manner then it certainly won't when your application calls for it.
    It's possible that there is a coding error but, given the info supplied, i'd start examining the environment, first. Let us know if you have any luck with the test i recommended or not (please provide abundant detail). Or, if you've found the solution then you may want to post back with a quick blub so the next person knows how to escape the trap.
    D

  • How to call a specific method in a servlet from another servlet

    Hi peeps, this post is kinda linked to my other thread but more direct !!
    I need to call a method from another servlet and retrieve info/objects from that method and manipulate them in the originating servlet .... how can I do it ?
    Assume the originating servlet is called Control and the servlet/method I want to access is DAO/login.
    I can create an object of the DAO class, say newDAO, and access the login method by newDAO.login(username, password). Then how do I get the returned info from the DAO ??
    Can I use the RequestDispatcher to INCLUDE the call to the DAO class method "login" ???
    Cheers
    Kevin

    Thanks for the reply.
    So if I have a method in my DAO class called login() and I want to call it from my control servlet, what would the syntax be ?
    getrequestdispatcher.include(newDAO.login())
    where newDAO is an instance of the class DAO, would that be correct ?? I'd simply pass the request object as a parameter in the login method and to retrieve the results of login() the requestdispatcher.include method will return whatever I set as an attribute to the request object, do I have that right ?!!!!
    Kevin

  • What is the best way to call a servlet from another servlet?

    I have a project with 9 servlets (class project). The way I have been moving from servlet to servlet is like this
    doPost(...)
    {      response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head><title>Functions</title>");
    out.println(f"<form name=frm6 method=post action=/servlet2");
    out.println("<input type=submit name='btn' value='servlet2'>");
    out.println("</form>");
    So I have these 9 servlets - I call any 8 of them from the first servlet so I have 8 buttons on 8 forms <form=frm1, frm2, ...frm8 method = post...> But when I bring up the first servlet only 6 buttons show up. I was thinking about using hyperlinks instead, but I would like to do this with buttons. I wanted to do this with javascript and the location object, but I was advised to use jsp. I just want to move from one servlet to the next. Any suggestions appreciated for the best/preferred method for moving from one servlet to the next.
    Thanks

    I think you may need some clarification of terminology etc..
    First off, JSP isn't an alternative to javascript, it's an alternative to coding a servlet. A JSP is a mixture of java code and HTML and is translated into a servlet by the system. JSPs are primarilly for generating HTML pages with variable content. JSPs very frequently generate HTML which includes Javascript.
    You probably shouldn't think of what you're doing as one servlet invoking another, that does happen; a servlet can transfer an transaction to another servlet or JSP. In fact it's standard practice that a servlet does the logical stuff (like interpretting form data) then transfers to a JSP to generate the response page. However in this case it's the browser that can invoke one of the 8 servlets, the first servlet merely creates the page from which they are invoked.
    It's not obvious why only some of your buttons are showing up. In a case like this use the "view source" option on your browser to find out what HTML the servlet is actually delivering. What's wrong should be evident from that.
    You can put a hyperlink arround a button, or an image. Mostly people turn up their noses at the buttons supplied by HTML and use their own images for buttons. You
    can do somthing like this:
    <img src="/images/button3.png" border="0">
    (Of course this arrives as a GET transaction not a POST).
    Or you can do a bit of javascript like:
    <img src="/images/button3.png" style="Cursor: pointer;"
    onclick="document.locations.href='/servlet3';">

  • Calling one Servlet from another servlet

              First servlet...............>
              package javaservlets.samples;
              import javax.servlet.*;
              import javax.servlet.http.*;
              public class TableElements extends HttpServlet{
                   public void doGet(HttpServletRequest request,HttpServletResponse response)
                   throws ServletException,IOException{
                        response.setContentType("text/plain");
                        try{
                             RequestDispatcher rd = getServletContext().getRequestDispatcher("/TableFilter");
                             if(request==null){
                                  System.out.println("Request null...");
                                  System.exit(0);
                             if(response == null){
                                  System.out.println("Response null...");
                                  System.exit(0);
                             if(request!=null&&response != null){
                             System.out.println("Both are not null...");
                             rd.forward(request,response);
                        }catch(ServletException se){
                             se.printStackTrace();
                        }catch(IOException ioe){
                             ioe.printStackTrace();
              Second Servlet..............>
              package javaservlets.samples;
              import javax.servlet.*;
              import javax.servlet.http.*;
              public class TableFilters extends HttpServlet{
                   public void service(HttpServletRequest request,HttpServletResponse response)
                   throws IOException,ServletException{
                        String type = request.getContentType();
                        System.out.println("type : "+type);
              from the TableElement servlet just I am calling the TableFilter servlet and in
              the TableFilter I am trying to print the request content type. But I am getting
              NULL. i was supposed to get "TEXT/PLAIN".
              can any one tell what is the problem.
              very urgent.
              thanx in advance
              

    Your example seems to assume that the reponse of the first servlet becomes
              the request in the second servlet let this is not so. After the forward the
              request and response obejcts are the same as before the forward, except the
              referer attribute is changed.
              "Gururaj Kosigi" <[email protected]> wrote in message
              news:[email protected]...
              >
              > First servlet...............>
              >
              > package javaservlets.samples;
              >
              > import javax.servlet.*;
              > import javax.servlet.http.*;
              >
              >
              > public class TableElements extends HttpServlet{
              >
              >
              >
              > public void doGet(HttpServletRequest request,HttpServletResponse response)
              > throws ServletException,IOException{
              >
              > response.setContentType("text/plain");
              >
              >
              > try{
              >
              > RequestDispatcher rd =
              getServletContext().getRequestDispatcher("/TableFilter");
              >
              > if(request==null){
              > System.out.println("Request null...");
              > System.exit(0);
              > }
              > if(response == null){
              > System.out.println("Response null...");
              > System.exit(0);
              > }
              > if(request!=null&&response != null){
              > System.out.println("Both are not null...");
              > }
              >
              > rd.forward(request,response);
              > }catch(ServletException se){
              > se.printStackTrace();
              >
              > }catch(IOException ioe){
              > ioe.printStackTrace();
              >
              > }
              >
              > }
              > }
              >
              > Second Servlet..............>
              >
              > package javaservlets.samples;
              >
              > import javax.servlet.*;
              > import javax.servlet.http.*;
              >
              >
              > public class TableFilters extends HttpServlet{
              >
              > public void service(HttpServletRequest request,HttpServletResponse
              response)
              > throws IOException,ServletException{
              >
              >
              > String type = request.getContentType();
              > System.out.println("type : "+type);
              >
              > }
              > }
              >
              > from the TableElement servlet just I am calling the TableFilter servlet
              and in
              > the TableFilter I am trying to print the request content type. But I am
              getting
              > NULL. i was supposed to get "TEXT/PLAIN".
              >
              > can any one tell what is the problem.
              > very urgent.
              >
              > thanx in advance
              

  • Calling a servlet from a servlet?

    Is it possible to call one servlet from another servlet?

    You should use servlets to process requests. If what you're asking for is to execute a piece of logic within a servlet before its finished processing the request, then you should consider using a regular java class instead of a servlet. If you just want to send the request information to another servlet you can use getRequestDipatcher().forward(..) method. Keep in mind that by doing so you'll interupt the current servlet.
    Regards,
    D.

  • What are the 3 ways to call a form from another form?

    What are the 3 ways to call a form from another form?
    What is the command to call a report from within a form?
    How do you attach a menu to a form?

    Hi,
    1. Should be new_form, call_form and open_form
    2. The command is run_product
    3. There is a property 'Menu Module' in form, just change it to your menu file name
    Regards,
    George
    Can anyone help me with the following questions...
    What are the 3 ways to call a form from another form?
    What is the command to call a report from within a form?
    How do you attach a menu to a form?
    Thanks for your time..
    Madhu

  • Invoke a presentation from another presentation

    Hi everybody,
    I need to invoke a presentation from another presentation, to generate an e-mail, like a popup. I would prefer not to use another task to do this. In the parent presentation i need a button Mail that invokes a presentation in which i would have some attributes from the parent presentation. Please your help.
    Thanks
    Eva
    Edited by: Eva E on 22-jul-2009 11:06

    Hi Eva,
    Know there's a good reason for you not using a Screenflow for this, but I missed it somehow.
    If you had a Screenflow, you could have a transition in the screenflow back to the original presentation and retain the information you entered/changed in the original presentation.
    Thanks,
    Dan

  • HT4407 i just lost that disc. any other ways to get support disc from another pc? or recover my last mac os?

    i just lost that disc. any other ways to get support disc from another pc? or recover my last mac os?

    AlisinaBahadori wrote:
    i just lost that disc. any other ways to get support disc from another pc? or recover my last mac os?
    You are making no sense at all, are you using Lion or Snow Leopard,? did you try BobTheFishermans sugegstion for booting into OSX, please answer the questions rather than your current approach.

  • My phone was stolen and my find my iphone was turned off is there any way to turn it on from another device?

    So my iphone was stolen after I left it in a mall. I had find my iphone app off is there any way to turn it on from another device so I can see the location of my phone?

    There is no way to remotely enable that feature.  It has to already be turne don in Settings > iCloud > Find My iPhone in order for you to locate it.
    Here is some more info about Find My iPhone.

  • Servlet Calls another Servlet, Returns an Object

    Is it possible to call another servlet (which is loaded through
              Load-on-startup) from another servlet. The return should be an Object.
              I know I am kind of asking for a procedural way of programming.
              Example : Servlet "IamReady" is loaded and gets a request with binary
              data. It replies with a Binary Object.
              Servlet "serviceCall" calls IamReady to get the resoponse.
              I know that there are forward (using requestDispatcher) and "include"
              directives to have another servlet service calling servlet.
              Also, if I define a method in the (generic servlet) does each call to
              methos is executed in different thread (like doPost and doGet).
              Thanks.
              Chris
              

    "Michael Reiche" <[email protected]> wrote in message news:<[email protected]>...
              Thank you Michael,
              > It is possible to call a servlet from a servlet.
              >
              > You can 'return' an object by putting it in the httpRequest.
              You mean httpResponse.
              >
              > The call is NOT executed in a separate thread.
              >
              > From this post (and the other post about a connection pool) - I wonder if
              > you really need to be using servlets. I couldn't think of a good reason why
              > the connection pool needs to be a servlet.
              Are you suggesting I should just have a class. Reason I have it as
              servlet, b'caz I do a load-on-startup and do initialiazation etc. in
              the init method. May be if you suggest how I can do it otherwise, I
              would like to implement it that way.
              Thanks again.
              >
              > Mike
              >
              > "MOL" <[email protected]> wrote in message
              > news:[email protected]...
              > > Is it possible to call another servlet (which is loaded through
              > > Load-on-startup) from another servlet. The return should be an Object.
              > > I know I am kind of asking for a procedural way of programming.
              > >
              > > Example : Servlet "IamReady" is loaded and gets a request with binary
              > > data. It replies with a Binary Object.
              > >
              > > Servlet "serviceCall" calls IamReady to get the resoponse.
              > >
              > > I know that there are forward (using requestDispatcher) and "include"
              > > directives to have another servlet service calling servlet.
              > >
              > > Also, if I define a method in the (generic servlet) does each call to
              > > methos is executed in different thread (like doPost and doGet).
              > >
              > > Thanks.
              > >
              > > Chris
              

  • Servlets calls another servlet

    servlets calls another servlet ...how to do it ? whats the efficient way ?
    class myservlet extends HttpServlet
    // i want to call a servlet situated at another machine in the LAN whose, IP // 123.123.45.66 (say)
    the servlet which is situated in another machine
    remoteservlet extends HttpServlet
    doPost(...)
    how do i call ?
    few of the way i found by searching the forum.
    but i would like to know the good way in my situation.

    res.sendRedirect("LoginServlet?="+req.getRequestURI())
    i tested this. it does not work.
    my servlet wants to call another servlet which is
    active on IP xxx.ddd.ffff.zzz in the LAN .
    whats the way ?
    res.sendRedirect("http://xxx.ddd.ffff.zzz:<portnumber>/<context_name>/<servlet_regd_name">);The request and response objects are generated anew for that Servlet. There's no two ways about it, IMO.
    cheers,
    ram.

  • Passing parameter value via href in a servlet to another servlet

    Hi
    I have this issue with passing a value from one servlet to another servlet. the situation is is this.
    i have a servlet tstret.java which say..pulls out all the employee_id(s) from a table and displays it as hyperlinks( using the anchor tag.).
    now when i click on the hyperlink say T001 , The control is passed to another servlet which pulls out all the info about employee id T001.
    the problem i have is in the tstret.java servlet. i am passing the employee_id as a parameter in the href path itself as shown below.
    out.println("<td ><a href=\"http://localhost:7000/servlets-examples/accept_requisition.html?id="+[u]rs.getString(1)[/u]+"\" ><em>"+rs.getString(1)+"</em></a></td>");now if you see the code i am trying to pass the employee_id by attaching it to a variable id and passing it with the url, it gives me a sql exception saying no data found .
    if i pass a string say "rajiv" which i defined at the begining of code then i am able to pass it to the next servlet/html.
    the full code is as follows
    file : tstret.java
    import java.io.*;
    import java.sql.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class tstret extends HttpServlet
         PrintWriter out;
         String[] employee_identity;
         public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
              try
              response.setContentType("text/html");
              out = response.getWriter();
              String session_name="SHOBA";//request.getParameter("session_name");
              out.println("<html><head><title> My Jobs </title></head>");
              out.println("<body bgColor=#ececec leftMargin=10>");
              out.println("<table align=center>");
              out.println("<tr align=center><td><img src=\"http://localhost:7000/servlets-examples/images/topbar.gif\"></td></tr>");
              out.println("<td>Jobs for : "+session_name+"</td>");
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
              Connection connection = DriverManager.getConnection("jdbc:odbc:tst","scott","tiger");
              Statement statement=connection.createStatement();
              ResultSet rs= statement.executeQuery("select req_no from require_info where sent_to = " + "\'" + session_name + "\'");
    //          out.println("Jobs for : "+session_name);
              while(rs.next())
              /*     String[] get_id={"shoba","ping","ting","ving"};
                   for(int i=0;i<get_id.length;i++)
                   out.println(" <tr align=\"center\">");
              //     out.println("<td><a href=\"servlet/testhyper\" name="+rs.getString(1)+"><em>"+rs.getString(1)+"</em></a></td>");
                   out.println("<td ><a href=\"http://localhost:7000/servlets-examples/accept_requisition.html?id="+rs.getString(1)+"\" ><em>"+rs.getString(1)+"</em></a></td>");
                   out.println("</tr>");
                   out.println("<br>");
              }catch(SQLException se){out.println("sqlexception"+se);}
              catch(ClassNotFoundException ce){out.println("cnfexception"+ce);}
              out.println("</table></body></html>");
         public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
              doGet(request, response);
              Can some one help me and see if there is anything i missing or doing wrong.
    thanks in advance

    Try storing the id at the top of the loop, then using that for the URL and the link text, instead of calling rs.getString(1) twice:
      while(rs.next())
        String emp_id = rs.getString(1);
        out.println(" <tr align=\"center\">");
        out.println("<td ><a href=\"accept_requisition.html?id="+emp_id+"\" ><em>"+emp_id+"</em></a></td>");
        out.println("</tr>");
        out.println("<br />");
      }

  • Servlet calling another servlet in diff J2EE app

    Hi.
    Can a servlet load another servlet in different J2EE application ?
    If so, could you show me how ?
    Code example would be much appreicated.
    Thanks.

    Call the other servlet's absolute url thro' urlconnection.
    URL url=new URL("http://server:port/servlet/otherservlet");
    InputStream in=url.openConnection();
    Other servlet will be loaded. Read response if you want to.

Maybe you are looking for

  • Business Partner ID and Vendor creation

    Hi All,     My client does not want to create Vendor IDs for potential vendor during RFx until awarded. My situation is if the supplier registers to buyer Business partner id is created first.  Buyer still cannot proceed with PO processing(classic sc

  • HT2619 is it possible to convert a mp3 file in a text?

    is it possible to convert a mp3 file in a text in final cut?

  • SoaBPM 11g workspace user setup

    I am trying to get the 11g soaBPM installation on Linux 64 working. I had re-installed it and does not seem to be able to login to the Business Process workspace with the default weblogic/weblogic9 admin account. (Side issue: was having a lot of prob

  • Free  SQL server 2005 Database administration book

    Hi, From where we can download SQL server 2005 Database administration book Thanks & Regards, Raj

  • HP Pavilion dv7-4280us recovery disk

    HP Pavilion dv7-4280us Entertainment Notebook PC Serial {Removed for privacy} Product number XZ027UAR#ABA US & Canada only Model DV7-4280US Hard disk HDO Toshiba MK7559GSXP 750GB Advanced format = YES Win 7 64-bit Home Premium is the original OS I ne