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.

Similar Messages

  • I want to send a response from the servlet and then call another servlet.

    Hi,
    I want to send a response from the servlet and then call another servlet. can this happen. Here is my scenario.
    1. Capture all the information from a form including an Email address and submit it to a servlet.
    2. Now send a message to the browser that the request will be processed and mailed.
    3. Now execute the request and give a mail to the mentioned Email.
    Can this be done in any way even by calling another servlet from within a servlet or any other way.
    Can any one Please help me out.
    Thanks,
    Ramesh

    Maybe that will help you (This is registration sample):
    1.You have Registration.html;
    2.You have Registration servlet;
    3.You have CheckUser servlet;
    4.And last you have Dispatcher between all.
    See the code:
    Registration.html
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <HTML>
      <HEAD>
        <TITLE>Hello registration</TITLE>
      </HEAD>
      <BODY>
      <H1>Entry</H1>
    <FORM ACTION="helloservlet" METHOD="POST">
    <LEFT>
    User: <INPUT TYPE="TEXT" NAME="login" SIZE=10><BR>
    Password: <INPUT TYPE="PASSWORD" NAME="password" SIZE=10><BR>
    <P>
    <TABLE CELLSPACING=1>
    <TR>
    <TH><SMALL>
    <INPUT TYPE="SUBMIT" NAME="logon" VALUE="Entry">
    </SMALL>
    <TH><SMALL>
    <INPUT TYPE="SUBMIT" NAME="registration" VALUE="Registration">
    </SMALL>
    </TABLE>
    </LEFT>
    </FORM>
    <BR>
      </BODY>
    </HTML>
    Dispatcher.java
    package mybeans;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.ServletException;
    import java.io.IOException;
    import javax.servlet.RequestDispatcher;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class Dispatcher extends HttpServlet {
        protected void forward(String address, HttpServletRequest request,
                               HttpServletResponse response)
                               throws ServletException, IOException {
                                   RequestDispatcher dispatcher = getServletContext().
                                   getRequestDispatcher(address);
                                   dispatcher.forward(request, response);
    Registration.java
    package mybeans;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class Registration extends Dispatcher {
        public String getServletInfo() {
            return "Registration servlet";
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletContext ctx = getServletContext();
            if(request.getParameter("logon") != null) {          
                this.forward("/CheckUser", request, response);
            else if (request.getParameter("registration") != null)  {         
                this.forward("/registration.html", request, response);
    CheckUser.java
    package mybeans;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.sql.*;
    public class CheckUser extends Dispatcher {
        Connection conn;
        Statement stat;
        ResultSet rs;
          String cur_UserName;
        public static String cur_UserSurname;;
        String cur_UserOtchestvo;
        public String getServletInfo() {
            return "Registration servlet";
        public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            try{
                ServletContext ctx = getServletContext();
                Class.forName("oracle.jdbc.driver.OracleDriver");
                conn = DriverManager.getConnection("jdbc:oracle:oci:@eugenz","SYSTEM", "manager");
                stat = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
               String queryDB = "SELECT ID, Login, Password FROM TLogon WHERE Login = ? AND Password = ?";
                PreparedStatement ps = conn.prepareStatement(queryDB); 
               User user = new User();
            user.setLogin(request.getParameter("login"));
            String cur_Login = user.getLogin();
            ps.setString(1, cur_Login);
            user.setPassword(request.getParameter("password"));
            String cur_Password = user.getPassword();
            ps.setString(2, cur_Password);
         Password = admin");
            rs = ps.executeQuery();
                 String sn = "Zatoka";
            String n = "Eugen";
            String queryPeople = "SELECT ID, Surname FROM People WHERE ID = ?";
           PreparedStatement psPeople = conn.prepareStatement(queryPeople);
                      if(rs.next()) {
                int logonID = rs.getInt("ID");
                psPeople.setInt(1, logonID);
                rs = psPeople.executeQuery();
                rs.next();
                       user.setSurname(rs.getString("Surname"));
              FROM TLogon, People WHERE TLogon.ID = People.ID";
                       ctx.setAttribute("user", user);
                this.forward("/successLogin.jsp", request, response);
            this.forward("/registration.html", request, response);
            catch(Exception exception) {
    }CheckUser.java maybe incorrect, but it's not serious, because see the principe (conception).
    Main is Dispatcher.java. This class is dispatcher between all servlets.

  • 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.

  • How to call another program as root in servlet

    In my servlet, I want to call another background programs (writeen in c) to do some tasks and gather the output of them as the output of my servlet. Is there any way to do so like Suexec in apache.
    Thanks

    sorry I forgot mention that the c program required the caller had root privilege.This is the problem I facing now.
    Thanks

  • Calling another java class from a servlet

    I am trying to write a web based form handling system (for a college project)
    I have a servlet that responds to a user request for a form. I have another java program (HTMOut) that parses the xml file for the form and produces HTML output. When I call the HTMOut from the servlet it crashes the webserver (Tomcat). But if I call HTMOut from an ordinary java class it runs fine. If I call another test program from my servlet that works too.
    Any ideas?

    where does your HTMOut output? A file or a stream? I think it is better that HTMOut can output to a stream you can set externally.

  • How to calling another servlet in a servlet

    In my servlet named TransferServlet I want call another servlet named PublisherServlet. So I wrote codes :
    objURL = new URL("http://localhost:8080/servlet/PublisherServlet") ;
    hucConnection = (HttpURLConnection)objURL.openConnection() ;
    hucConnection.setDoOutput(true) ;
    hucConnection.setUseCaches(false) ;
    hucConnection.setRequestMethod("POST") ;
    hucConnection.connect() ;
    but i can't invoke PublisherServlet. Why ? pls help
    Thanks a lot

    you do not need hucConnection.connect(); hucConnection = (HttpURLConnection)objURL.openConnection() does this.
    You need to use hucConnection.getOutputStream() and write to the stream to post and if you wanted a response you would need to read from hucConnection.getInputStream()

  • Calling another servlet from a servlet

    I'm working on a servlet and would like to know how to
    call another servlet, by clicking on a form button
    or a hyperlink that was generated from the first servlet,
    is it only possible if the second servlet is called
    in an shtml page? Can you please give me an example
    of how to do this not using shtml pages?
    (I'm working with JDeveloper 2.0)
    When will the book be out for JDeveloper 2.0?
    We're also having problems deploying the servlet
    to the Java Web Server (1.1.3)
    It seems to have a problem connecting to the database.
    We get the first page of the servlet but the
    second page is generated from the doPost()
    and connects to the database using oracle JDBC thin
    gives a http 500 internal server error ,
    and we followed the instructions from JDeveloper
    can the problem be caused from the connection string that
    I used in the servlet:
    Class.forName("oracle.jdbc.driver.OracleDriver");
    DriverManager.registerDriver(
    new oracle.jdbc.driver.OracleDriver());
    con = DriverManager.getConnection(
    "jdbc:oracle:thin:@(description=(address=
    (host=10.10.10.53)(protocol=tcp)(port=1521))
    (connect_data=(sid=OR8A)))",
    "im_dev","im_dev");
    or the configuration of the web server?
    (web server: Solaris 2.7 running on Intel)
    null

    Hi
    The sample acme video demo in JDeveloper does something similar.
    It has main Servlet "WebAppServlet.java" which overides the
    doPost and doGet methods and this methods calls someother java
    classes do perform some specific business logic which return a
    html page in a String format to "WebAppServlet.java".
    Open the samples directory in JDEveloper 2.0 with WebApp_81.jws
    to look at the source code.
    Steps to run the sample are included in the help system.
    regards
    argyro (guest) wrote:
    : I'm working on a servlet and would like to know how to
    : call another servlet, by clicking on a form button
    : or a hyperlink that was generated from the first servlet,
    : is it only possible if the second servlet is called
    : in an shtml page? Can you please give me an example
    : of how to do this not using shtml pages?
    : (I'm working with JDeveloper 2.0)
    : When will the book be out for JDeveloper 2.0?
    : We're also having problems deploying the servlet
    : to the Java Web Server (1.1.3)
    : It seems to have a problem connecting to the database.
    : We get the first page of the servlet but the
    : second page is generated from the doPost()
    : and connects to the database using oracle JDBC thin
    : gives a http 500 internal server error ,
    : and we followed the instructions from JDeveloper
    : can the problem be caused from the connection string that
    : I used in the servlet:
    : Class.forName("oracle.jdbc.driver.OracleDriver");
    : DriverManager.registerDriver(
    : new oracle.jdbc.driver.OracleDriver());
    : con = DriverManager.getConnection(
    : "jdbc:oracle:thin:@(description=(address=
    : (host=10.10.10.53)(protocol=tcp)(port=1521))
    : (connect_data=(sid=OR8A)))",
    : "im_dev","im_dev");
    : or the configuration of the web server?
    : (web server: Solaris 2.7 running on Intel)
    null

  • 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
              

  • Servlet calling another servlet

    hi,
    I am writing a web-application which requires one servlet (on main server) to call another servlet (on a remote server).
    The main servlet needs to call the remote one and send some parameters to it.
    The remote servlet would be sending back XML data which is to be used by the central servlet. I tried using XML-RPC but it does'nt seem to support sending in NATIVE XML data.
    I also tried by creating URL, but did not find a way to add parameters to it !!!
    Could anyone please tell me how to call another servlet (along with sending POST/GET parameters) and get the results back into the calling servlet ?
    Any suggestions would be greatly appreciated.
    Thanks !
    Ajoy

    one possible solution would be to create an own socket connection (this would be a like a Post request) and than exchange data as you like.

  • Can I run Servlets in the Sun App Server?

    I am trying to run a servlet. I am not sure if I can run the same using the Sun app server. any help to clarify the following queries would be appreciated.
    1. Should I have to specify a path in the environmental variables?
    2. Is there a web site that tells me the same with respect to running the servlet on a Sun app server.
    Thanks in advance.
    --Srikanta                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

    I'm not sure what you want in regards to question, but I can assure you that you can run servlets in S1AS7. My recommendation is to just package your servlet into a web archive (war) and deploy it. You can deploy it via the commandline (asadmin) or the web based administrative client (likely http://localhost:4848). For more information on this I suggest reading:
    http://docs.sun.com/source/816-7150-10/index.html
    You must likely want to read the section on assembling and deploying web applications.
    Matt Bauer

  • Calling another app?

    Hello!
    In my app's code ... I want to call another app in my package (also having its own Main) to run when called for.
    How can I do this?
    Thanks!

    "AddRFSwitchMain" ... is my main running app.
    I want to to start another app that is also in my
    class package
    "public class SerialPortSnooper implements Runnable,
    SerialPortEventListener " This app also has a "main
    class" so I can run it by it self .
    I still dont see how I compose the code to call this
    second app.I think i would call
    SerialPortSnooper.main(aaaaaaaaaaaaaarrrrrrrrrrrgggggggggggggssssssssss);

  • How to run a background process in a J2EE App ?

    Hi guys,
    Here is the requirement which totally stumped me.
    " The web app should have a feature like a background process or something like cron jobs.. which keeps on running in the background and at a particular time each day calls a method". User intervention is not an option.
    Now had this been a stand alone app .. I could have done this using Threads.. but i cant possibly use threads in a J2EE app. Any pointers.. how I can go about realizing this
    Thanks for your replies.
    Cheers!

    You could take the approach that SoulTech mentioned and spawn a new thread from the Servlet's init method. To have the servlet invoke this method with no intervention, you would need to set the following parameter in your servlet definition in your web.xml:
    <servlet>
      <servlet-name>MyStartupServlet</servlet-name>
      <servlet-class>com.my.company.MyStartupServlet</servlet-class>
      *<load-on-startup>1</load-on-startup>*
    </servlet>When you deploy your application, the servlet will be loaded, and its init method will be called.
    If your application is running in a servlet container, I believe you are allowed (by the specification) to spawn your own threads, but you should do so carefully. Since you're writing a scheduler and not some kind of worker threads that are messing with your database, I think you should be okay. Still, just to keep it clean you should add any clean-up logic you may need to your servlet's destroy method. I know that in an EJB container, spawning your own threads can definitely be a problem and the container makes no guarantees that they will function correctly. The J2SE Timer class could be used if you're not using EJB, but the Timer class doesn't have support for J2EE - that's at least part of what led to the introduction of the timer beans in J2EE 1.4. If you're already using EJB, especially EJB 3.0, then the timer service provides a convenient built-in mechanism to do exactly what you want.
    There are always multiple ways to solve any problem...
    Edited by: proflux on May 15, 2008 1:04 PM

  • J2EE app server not able to compile JSP

    Hi,
    I have just installed J2EE app server and am trying to deploy a JSP. The following error is generated. can anyone help me please.
    Thank you,
    Deepika.
    org.apache.jasper.JasperException: Unable to compile class for JSP
    No Java compiler was found to compile the generated source for the JSP.
    This can usually be solved by copying manually $JAVA_HOME/lib/tools.jar from the JDK
    to the common/lib directory of the Tomcat server, followed by a Tomcat restart.
    If using an alternate Java compiler, please check its installation and access path.
         org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127)
         org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:348)
         org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:424)
         org.apache.jasper.compiler.Compiler.compile(Compiler.java:464)
         org.apache.jasper.compiler.Compiler.compile(Compiler.java:448)
         org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:551)
         org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:300)
         org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
         org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
         sun.reflect.GeneratedMethodAccessor82.invoke(Unknown Source)
         sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         java.lang.reflect.Method.invoke(Method.java:324)
         org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:284)
         java.security.AccessController.doPrivileged(Native Method)
         javax.security.auth.Subject.doAsPrivileged(Subject.java:500)
         org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:306)
         org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:200)

    This is a known issue.
    Here's an explanation of the behaviour you are seeing:
    The misleading error message is printed only in the case where a javac
    compilation error line number cannot be mapped back to a line number
    in the JSP source code.
    The internal representation of a JSP element ("JSP node") in the JSP compiler
    contains the following info:
    - The begin line number of the JSP element in the JSP page
    - The begin and end line numbers ("range") of the Java code that was
    generated for the JSP element
    When attempting to map a javac error line number back to the JSP
    source line, we determine the range into which the javac error line
    number falls, and retrieve the JSP source number of the JSP element
    associated with that range.
    However, in some cases, it is impossible to trace a javac error line
    number back to the JSP element that caused the error. For example,
    consider the following scriptlet:
    <%
    String query = request.getParameter("query");
    if(query != null) {
    out.println(query);
    } else {
    out.println("(null)");
    // } // <-- !Missing brace syntax error
    %>
    Since the JSP compiler wraps the generated code into a
    try-catch-finally block, the above syntax error in the scriptlet
    causes havoc with that try-catch-finally block, and results in 3 javac
    errors:
    <file>jsp.java:<line1>: 'catch' without 'try'
    } catch (Throwable t) {
    ^
    <file>jsp.java:<line2>: 'try' without 'catch' or 'finally'
    try {
    ^
    <file>jsp.java:<line3>: '}' expected
    Notice that all of the above javac error numbers are outside the range
    of the java line numbers corresponding to the scriptlet that caused the
    error, and therefore cannot be mapped back to any line numbers in the
    JSP page source.
    AS 8.0 PE does not handle this case correctly, and prints out the
    misleading error message (though the real error message can be found
    in the server log).
    Notice that this problem is going to be fixed in AS 8.1, so that if a
    javac error line number cannot be mapped back to a JSP page line
    number, the javac error is still returned to the client.
    In AS 8.0 PE, you need to check your server log to find out why the compilation failed.
    If you still have any problems, send me your JSP, and I will take a look at it.
    Thanks,
    Jan

  • J2EE App Servers

    Hi
    I am currently investigating the setup we'll need to develop java apps - we're looking to develop multi-tier applications using jsp,servlets and ejb's. We are however currently tied into using Microsoft IIS webservers. From what I can gather we therefore have no choice but to purchase an (expensive) J2EE App Server to handle the jsp, servlet and ejb's - like Borland's AppServer 4.5 both for development and deployment of our final application to our customers (assuming they want to host the application themselves).... Does anyone know of some less expensive options to handle this kind of Java development or can someone suggest an alternative architecture?
    Many thanks.

    As some has said before there is a free app server for J2EE based applications, and you can find it at:
    http://www.jboss.org/
    As for web server you can use the Tomcat which is also free.

  • Usage of Object Cache for Java in J2EE apps

    Hi,
    we are investigating on whether we can use the Object Cache for Java
    (OCS4J) for our requirements. The question we have come across is:
    What is the designated way of integration for the Object cache to fit
    into the J2EE environment? Unfortunately, although the current manuals
    group OCS4J into the "Oracle Containers for J2EE Services Guide" and the
    suggested package name for the whole thing in JSR 107 seems to be
    javax.util.jcache, there is very little documentation on how the
    designers would like J2EE programmers to use the cache from within a
    J2EE app and all examples given are not from within a J2EE environment.
    We are in particular thinking about a hierarchy of several cache
    "compartments" (Region->Subregion->group) for different topics and using
    the hierarchical name of the cache (region.subregion.group) as the
    primary key for BMP Entity beans, each of them having their own
    CacheAccess object. Then we would have an API of stateless Session beans
    on top of that, which would be determining the cache "compartment", get
    the appropriate Entity Bean with the Cache Access object and then do the
    required operations on the cache.
    But then we immediately run into the question of how the mapping between
    Cache Objects and CacheAccess objects will be done etc.
    So is there anybody that can give us any hints how to use the OCS4J in
    an EJB scenario?
    Thanks in advance for any help!
    Andreas Loew
    [email protected]

    We have Java client requesting over HTTP to application server. We would like to cache some of the objects created by the servlet while serving the request. Can I use the OCS4J for caching the Java objects. Do I require any software or just copying the JAR file and importing the class would serve the purpose?
    Regards
    Arun

Maybe you are looking for

  • Custom Field value is not getting populated in Display mode

    Hi Experts , I have created a custom field on sales tab for BP Role Bill to prty in BP transaction in SAP CRM. The problem i am facing is that , after i chose my sales area field value is not populating in first time no matter whether i am in display

  • Saving report output on client machine

    Hi All, I'm using reports 6i. I'm running a report on the web which runs 5 reports inside it and it should save the outputs of these five reports in PDF format to the client machine. I can not give mime type as i don't want to show it in the acro. re

  • Help! With new update apps keep starting on it's own - even after closed

    Help! My battery cannot last the day now. Even with Advanced Task Killer, my applications are starting to open all day long every 5 minutes on their own.  They never did this before the update.  This includes apps like Facebook, Words with Friends, M

  • Aperture no longer publishing to Mobile Me gallery

    I was publishing 3 large albums to Mobile Me galleries. The first one published correctly, but the last two did not. The last two show up in the sidebar as Mobile Me galleries, but they are not showing up on the website. When I click the "MobileMe Ga

  • Monitor color off - PDF text lines highlighted

    When I open any PDF file, my monitor wallpaper becomes pixelated and changes colors and looks like a negative.  The lines of text in the file are either blacked out or highlighted with other colors.  I have had tech support clean my computer system t