Calling servlets in another jar/war

I am working on an integration project.
I have a war file that has many of the components that I need.
In my new project (Sun Studio IDE), I am trying to call up a servlet that exists in this other war file.
I can mount the war file as a directory, just like mounting a jar for use like classes12.zip whatever or similar to including a jar in your classpath.
Can I call a servlet in another jar, war, or zip file from my existing code?
How will the existing code or jsp know where to find this servlet outside the project?
Usually you have to either map the servlet or set this info in the web.xml, I think.
If the servlet mappings are in the web.xml, does that mean that if I access a servlet in another war, with it's own web.xml, does that mean that I have to map that servlet in my current projects web.xml also?
Thanks

Can I call a servlet in another jar, war, or zip file
from my existing code?
How will the existing code or jsp know where to find
this servlet outside the project?When you say 'call' I assume you mean forward. Yes, you can forward to servlets in other contexts/wars.
If the other context is say 'appB' you can forward the request to a servlet mapped as say '/test' in web.xml of appB in the following way:
getServletContext().getContext("/appB").getRequestDispatcher("/test").forward(request,response);
Usually you have to either map the servlet or set this
info in the web.xml, I think.
If the servlet mappings are in the web.xml, does that
mean that if I access a servlet in another war, with
it's own web.xml, does that mean that I have to map
that servlet in my current projects web.xml also?No. You don't need to map it again. However as you might have noticed, you do need to know the url pattern of the other servlet, so that you can forward properly.
But beware - Session info will not be shared in cross-context forwarding

Similar Messages

  • Deploying servlets (not in jar/war/ear!) to OC4J?

    I must be missing some very tiny but crucial issue....
    For over 2 days now (shame) I have been trying but not
    succeeding to deploy servlets to OC4J behind Apache. The 'behind
    Apache'-part is easy. Just set up a proxy. This is because
    Apache configuration is easy and well documented. But here is my
    real problem. I am using JDev 3.2 (9i too large) and this is
    what I want:
    I want to code a servlet in Jdeveloper, compile it to a class-
    file, not a jar, war, ear or whatever-r. Then I want to set up
    OC4J in such a way that I can access my servlets from the
    directory where the class files are. I do NOT want to use the
    default-web-app subdirectory for obvious reasons. Please, how do
    I set up OC4J to just look in my own classes-directory with my
    own servlets???? No jar, no war, no ear.
    I have been reading all sorts of information on this and tried
    everything, but am not succeeding and getting all sorts of
    error/not found messages.
    Extra info:
    I am using OC4J on windows. To start I use java -jar orion.jar,
    to restart I use ^C and repeat the start command. Is that ok or
    do I need to restart in a different way? I tried to setup OC4J
    as a service but the link to runexecsvc went astray to judosoft
    Any help will be greatly appreciated!

    Avi,
    Thanks for your swift reply. I have indeed read the docs, but
    the exploded directory format does not seem to work for one
    reason or another (got some error messages on file not found or
    something). Have you succeeded in deployment using exploded
    directories? If so, I would be greatly interested in your xml-
    files and directory structure!
    But, please do not spend too much time on this, because I have
    now decided that I will deploy using a war. I have written a
    small script to make a war out of my whole project and copy it
    to de OC4J file system. This works and everybody is happy.
    Thanks again,
    Martijn

  • Calling servlet from another method

    Hi,
    I am writing a program which requires executing another servlet from within
    thie main program. I do not care to return the information from the servlet
    request.
    http://usx500.crn.st.com:7000/TPMCenter_V2/insertAutomationValue.do?reqCode=confirmed
    Thanks for the help.

    The reason I continue posting this question, is because I cannot find a solution.
    I have looked through the link provided, as well as many other solutions on the web,
    none which have been successful. Following are two solutions which I have tried, and
    niether have worked. I still am in need of some help in resolving this. If you would like
    to assist me in this it would be greatly appreciated.
    Dr Clap--
    If you do not have a suggested solution, simply do not reply.
    Solution 1 ---
        try {
            // Construct data
            String data = URLEncoder.encode( "reqCode", "UTF-8") + "=" + URLEncoder.encode( "show", "UTF-8" ) ;
            // Create a socket to the host
            String hostname = "usx500.crn.st.com" ;
            int port = 7000 ;
            InetAddress addr = InetAddress.getByName( hostname );
            Socket socket = new Socket( addr, port );
            // Send header
            String path = "/TPMCenter_V2/insertAutomationValue.do";
            BufferedWriter wr = new BufferedWriter( new OutputStreamWriter( socket.getOutputStream(), "UTF8" ) ) ;
            wr.write( "POST " + path + " HTTP/1.0\r\n" );
            wr.write( "Content-Length: " + data.length() + "\r\n" ) ;
            wr.write( "Content-Type: application/x-www-form-urlencoded\r\n" ) ;
            wr.write("\r\n");
            // Send data
            wr.write(data);
            wr.flush();
            wr.close();
        } catch (Exception e) {
            e.printStackTrace() ;
        }Solution 2 ---
    try{
        URLConnection c = new URL("http://usx500.crn.st.com:7000/TPMCenter_V2/insertAutomationValue.do?reqCode=show").openConnection();
        c.connect();
    }catch(MalformedURLException murle){
        System.out.println(murle);
    }catch(IOException ioe){
        System.out.println(ioe);
    }

  • Trying to understand servlet jar/war interaction

    I am working on an integration project.
    I have a war file that has many of the components that I need.
    In my new project (Sun Studio IDE), I am trying to call up a servlet that exists in this other war file.
    I can mount the war file as a directory, just like mounting a jar for use like classes12.zip whatever or similar to including a jar in your classpath.
    Can I call a servlet in another jar, war, or zip file from my existing code?
    How will the existing code or jsp know where to find this servlet outside the project?
    Usually you have to either map the servlet or set this info in the web.xml, I think.

    I am working on an integration project.
    I have a war file that has many of the components that I need.
    In my new project (Sun Studio IDE), I am trying to call up a servlet that exists in this other war file.
    I can mount the war file as a directory, just like mounting a jar for use like classes12.zip whatever or similar to including a jar in your classpath.
    Can I call a servlet in another jar, war, or zip file from my existing code?
    How will the existing code or jsp know where to find this servlet outside the project?
    Usually you have to either map the servlet or set this info in the web.xml, I think.

  • Calling servlets from main()?

    Hi
    I would like to know that while calling servlets from another class having main(), how can we set the type of request i.e. put, post or get?
    Thanks

    use the method - setRequestMethod( "POST") of HttpURLConnection for setting the method to POST.Similarly for other methods..
    You could find [url http://java.sun.com/developer/JDCTechTips/2004/tt0210.html#2]this useful

  • 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

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

  • Problem calling servlet from doget method of another servlet

    hi,
    Iam trying to post an html form written in the doGet() method
    of a servlet to pass this information to another servlet's doPost() method. Iam giving the following URL:
    "<FORM ACTION=http://localhost:8080/examples/servlet/UpdateProcessServlet" +
    "METHOD=POST>"
    But its not happening,the error says that "the page cannot be found" The servlet is not getting called at all. would someboy please help me in this regard.
    Thanks

    #1 Iam calling servlet 2 from here
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    session=request.getSession(false);
    out.println
    (ServletUtilities.DOCTYPE +
    "<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.0 Transitional//EN>"+
    "<HTML>" +
    "<BODY>" +
    "<P CLASS=LARGER>" +
    "<FORM ACTION=http://localhost:8080/examples/servlet/UpdateProcessServlet METHOD=POST>"
    "<INPUT TYPE=SUBMIT NAME=submitButton Value=submit>" +
    "</BODY> " +
    "</HTML>" );
    #2 This should get called and print me "Iam in doPost method
    public void doPost(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,IOException
    url = "jdbc:odbc:Resume";
    System.out.println("Iam in doPost method");
    response.setContentType("text/html");
    out = response.getWriter();
    out.println("This is the last servlet for this project:");
    bool=false;
    check=false;
    Thank...:)

  • Compiled Tomcat Servlet Example & got JAR, not WAR?

    Is there a basic reference on how to create a simple servlet with NetBeans 4.1 and deploy it to Tomcat?
    When I created the project I chose Samples->Web->Tomcat Servlet Example, and yet it does not create a WAR file on compilation.
    And of course once I'm past that I have to figure out how to edit the XML file, I gather.
    I need a reference that will hold me by the hand and walk me through it once to kick start me.
    After that I think I can make it without having to ask such newbie questions.

    Hi,
    not sure to what extent you are clear in build.xml file (done thru ant).
    what i can tell you is, ant creates any archive file (be it .jar, .war, .ear) with the tasks named "jar" (<jar jarfile="yourfilename.jar"..>), "war" and "ear". But both war and ear are the extensions of the jar only.
    You can achieve creation of any archive file with the jar task itself.
    you need to take care of the following...
    (1) you can /should edit the extension of the files at every stage.. first .jar and then .war and then .ear (if necessary) => basic flow
    (2) at every step, you have to have a file inclusion inside the corresponding archive file..
    say for example, for a .jar file you should include all the compiled .class files, and ejb-jar.xml (incase you use ejbs) and other built-in jar files....
    In case of .war files (web archive) you should include the web.xml file, any static files (html, jsp etc), any tld files - it goes with your application's nature and requirement.....
    fyi, i m including a sample build.xml file which i use for a webapplication, so it deals with war...
    <?xml version="1.0" encoding="UTF-8"?>
    <project name="ServletsReqDispFwd" default="deploy">
         <property name="top" value="."/>
         <property name="webinf" value="${top}/WEB-INF"/>
         <property name="lib" value="${webinf}/lib"></property>
         <property name="classes" value="WEB-INF/classes"/>
         <property name="jboss.dir" value="D:\jboss-3.2.2RC3\server\default\deploy"></property>
         <property name="tomcat.dir" value="D:\jakarta-tomcat-4.1.27\webapps"></property>
         <property name="warfile" value="ServletsReqDispFwd.war"></property>
         <property name="src" value="${top}/src"/>
         <property name="web.xml" value="WEB-INF/web.xml"/>
         <target name="package">
              <javac srcdir="${src}" destdir="${classes}"
                     listfiles="true" debug="true"  verbose="true"
                          classpath="${lib}/*.jar"/>
              <war warfile="${warfile}" webxml="${web.xml}">
                   <webinf dir="${top}">
                        <include name="**/*web*.xml"/>
                        <include name="**/*application*.xml"/>
                        <exclude name="src/**/*.xml"/>
                   </webinf>
                   <lib dir="${lib}">
                        <include name="*.jar"/>
                   </lib>
                   <fileset dir="." includes="**/*.html"/>                    
                   <classes dir="${classes}" includes="**/*.class"></classes>                         
              </war>
         </target>
         <target name="deploy" depends="clean,package">
              <copy file="${warfile}" todir="${tomcat.dir}" overwrite="true"></copy>
         </target>
         <target name="clean">
                <delete dir="${classes}">
                     <filename name="*.class"/>
              </delete>
              <delete>
                   <fileset dir="${top}" includes="${warfile}"/>
              </delete>
              <delete>
                   <fileset dir="${tomcat.dir}" includes="${warfile}"/>
              </delete>
         </target>
    </project>hope this contribues to a certain extent to help you edit the buildfile (xml file as you go) to meet your requirement.....

  • Calling One Servlet From Another

    Thanks in advance for any advice!
    I would like to call servletB from servletA then use the response returned from servletB within the processing of servletA.
    I have checked the forums and jGuru but nothing I've found really matches the problem. RequestDispatcher has "forward" and "include" but I do not want to include the reponse from the called servlet in the response of the original one.
    Here is the situation. I have a web application that uses an applet front-end using a mainframe datasource. The applet displays data in various JTables and prints various reports.
    The applet calls a servlet(B) with an SQL statement in the request that responds with a Vector of data to be displayed in the JTable. This is working fine.
    The applet also calls servlet(A) which uses iText from SourceForge to create a PDF report that is sent back to the browser for display in Adobe Reader. This also works fine.
    Because I cannot figure out how to call servletB from within servletA, my current workaround is to embed the code of servletB inside servletA. Not very good OOP and as the application grows maintaining this will become a problem.
    Any suggestions would be greatly appreciated.
    Max

    Why don't you just take the code that the applet uses to "call" servlet B and replicate that code into servlet "A"? (Or better still, create a class to do that, which could be called both from the applet and from servlet A.)

  • Error while calling servlet in WebLogic

    Hi,
    We are trying to call servlet in WebLogic by using http://scrmskr.apac.nsroot.net:7801/forms/HelloWorldServlet URL and getting
    below error.
    ========================================================================
    Error 404--Not Found
    From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1:
    10.4.5 404 Not Found
    The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent.
    If the server does not wish to make this information available to the client, the status code 403 (Forbidden) can be used instead. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address.
    ========================================================================
    Here we are calling HelloWorldServlet servlet which is kept in below path
    /rcrmap2/weblogic/bea/user_projects/domains/frmrep_rcrms/servers/WLS_FORMS/tmp/_WL_user/formsapp_11.1.1/e18uoi/war/WEB-INF/classes
    Entries for HelloWorldServlet in web.xml:
    ========================================================================
    <servlet>
    <servlet-name>HelloWorldServlet</servlet-name>
    <servlet-class>HelloWorldServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>frmservlet</servlet-name>
    <url-pattern>/frmservlet/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>HelloWorldServlet</servlet-name>
    <url-pattern>/HelloWorldServlet*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>lservlet</servlet-name>
    <url-pattern>/lservlet/*</url-pattern>
    </servlet-mapping>
    ========================================================================
    HelloWorldServlet.java:
    ===============================================================================
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    public class HelloWorldServlet extends HttpServlet {
    public void service(HttpServletRequest req,
    HttpServletResponse res)
    throws IOException
    // Must set the content type first
    res.setContentType("text/html");
    // Now obtain a PrintWriter to insert HTML into
    PrintWriter out = res.getWriter();
    out.println("<html><head><title>" +
    "Hello World!</title></head>");
    out.println("<body><h1>Hello World!</h1></body></html>");
    ===============================================================================
    Please share your inputs as to what needs to be done for the same.
    Thanks & Regards,
    Harish

    Hi Jay,
    Please also find forms.conf_ entries
    ========================================================================
    # Name
    # forms.conf - Forms component Apache directives configuration file.
    # Purpose
    # It should include the weblogic managed server (routing) directives for
    # the servers where Forms applications are deployed and other miscellaneous
    # Forms component OHS directives.
    # Remarks
    # This file is included with the OHS configuration under
    # $OI/config/OHS/<OHS Node Name>/moduleconf sub-directory.
    # virtual mapping for the /forms/html mapping.
    RewriteEngine on
    RewriteRule ^/forms/html/(..*) /workaroundhtml/$1 [PT]
    RewriteRule ^/rcrms/html/(..*) /workaroundhtml/$1 [PT]
    AliasMatch ^/workaroundhtml/(..*) "/rcrmap2/weblogic/bea/rcrms/config/FormsComponent/forms/html/$1"
    AliasMatch ^/forms/frmjscript/(..*) "/rcrmap2/weblogic/bea/user_projects/domains/frmrep_rcrms/servers/WLS_FORMS/tmp/_WL_user/formsapp_11.1.1/e18uoi/war/frmjscript/$1"
    AliasMatch ^/rcrms/icons/(..*) "/rcrmap2/weblogic/bea/ORA_PFRD/forms/java/$1"
    # Virtual path mapping for Forms Java jar and class files (codebase)
    AliasMatch ^/forms/java/(..*) "/rcrmap2/weblogic/bea/ORA_PFRD/forms/java/$1"
    # Virtual path mapping for testcrms (codebase)
    AliasMatch ^/rcrms/java/(..*) "/rcrmap2/weblogic/bea/ORA_PFRD/forms/java/$1"
    AliasMatch ^/forms/(..*) "/rcrmap2/weblogic/bea/user_projects/domains/frmrep_rcrms/servers/WLS_FORMS/tmp/_WL_user/formsapp_11.1.1/e18uoi/war/WEB-INF/classes/$1"
    AliasMatch ^/rcrms/(..*) "/rcrmap2/weblogic/bea/user_projects/domains/frmrep_rcrms/servers/WLS_FORMS/tmp/_WL_user/formsapp_11.1.1/e18uoi/war/WEB-INF/classes/$1"
    AliasMatch ^/rcrms/WebCLAuth/* rcrms
    <Location /forms>
    SetHandler weblogic-handler
    WebLogicHost kauh0079
    WebLogicPort 8001
    </Location>
    ========================================================================
    Thanks & Regards,
    Harish

  • Jar, War or Ear file ?

    Hi,
    I am a total newbie in J2EE technology.
    I am currently using Sun One Application. Using the sample application, it produces all the Jar, War and Ear files.
    My understanding is Jar is produced to be a library-like usage.
    War is for web application.
    And Ear is for what?? oops EJB? Can someone tell me?
    Anyway, my concern is, if I am to produce Ear, war, or Jar file after deploy, I have problems doing the testing. Let's say I want to modify the Jsp file, then I have to use ant to compile all, undeploy the instance and deploy again.
    Any better way?

    your jar files contain your EJBs, your WAR file contains your web application (JSPs, servlets and support) and these are all bundled into your EAR file. All of these files are just zip-formatted archives, you can look at them all via the jar tool (or something like WinZip, if you are so inclined). Each of these will contain a descriptor file (in XML format) - check out the J2EE spec on it.
    Lee

  • Versioning JAR/WAR/EAR and CLASS files

    Does anyone know of a standard, defined and recommended way of versioning JAR/WAR/EAR files?
    I guess I can put version information in the Manifest.mf file, but is there a recommended way?
    Another related question is how do I go about versioning individual class files?

    There's not only a recommended way, but a standard way. See http://java.sun.com/products/jdk/1.2/docs/guide/versioning/spec/VersioningSpecification.html#PackageVersioning).
    However, I've found that this practice is unfortunately not as widespread as one would like. sigh
    What do you mean by "versioning" individual class files (in a JAR, I assume you mean)? In general that's a bad idea. But perhaps I misunderstand what you mean.

  • Problem accessing classes when moved into another jar

    Hi,
    I have written some classes and want to put them in an external jar
    as a library. However, when I do this, I have to include the full path
    for them to work- what am I doing wrong? I am using JBuilder6
    Example:-
    I have a class called LogWriter in package org.MyClasses.utils which I
    am using in a class in another package (com.MyCode). If they are all in
    my src directory it works fine and I import org.MyClasses.utils
    If I compile org.MyClasses into a jar and add the jar as a library, I can
    see the classes with CodeInsight (so I know they are there). However, the line
    Logwriter.writeLog() ; (call to static method), which used to work, now only works
    if I change it to org.MyClasses.utils.Logwriter.writeLog() ;
    Any advice?
    MArk

    If the class was called MyWriter and is in org.MyClasses.utils, I have to replace all instances of MyWriter with org.MyClasses.utils.MyWriter.
    MyWriter has not altered in any way. It's now compiled separately and put into another jar rather than all classes being comipled at the same time.
    MArk

Maybe you are looking for