Help needed to call a servlet from an applet

Hi,
can someone help me with the way to call a servlet from an applet.
actually on click of a button i wanna call my servlet and save some data in DB.
i am not able to call my servlet. see the sample code which i am trying..
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equals("Upload")) {
try {
System.out.println("Upload button has been clicked -----------");
URL url = new URL("http://localhost:8084/uploadApp/TestServlet");
URLConnection urlConnection = url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
ObjectOutputStream objOut = new ObjectOutputStream (urlConnection.getOutputStream());
objOut.writeBytes(userId); // pass parameter to servlet
objOut.flush();
objOut.close();
} catch (MalformedURLException ex) {
Logger.getLogger(ButtonListener.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ButtonListener.class.getName()).log(Level.SEVERE, null, ex);
is there any problem with the code? please suggest.

hii,
checkout my add from, HTH
[http://forums.sun.com/thread.jspa?threadID=5419921&messageID=10885709#10885709]
... kopik

Similar Messages

  • Is there any way to call the servlet from the applet

    hi all,
    My problem is that i have a applet .After the applet completes execution a servlet should be invoked.Is it possible to do so.
    I also want to pass a string array to the servlet.
    Is it possible to pass a string array to aservlet.
    If yes please do inform me that method.
    And from that servlet we should be able to redirect the the response of the applet to the servlet or JSP.
    The flow is some what lILE THIS
    APPLET TO SERVLET TO SERVLET/JSP

    Stop double posting. It won't help
    http://forum.java.sun.com/thread.jspa?threadID=642015
    and take a look at this.
    http://www.google.com/search?hl=en&q=applet+servlet+communication

  • Help Needed urgently - MKTCALENDAR button removal from java applet

    Hi Experts ,
    We have a requirement to remove buttons from the markeing calendar/projects applet .
    In Marketing->Search->Marketing Calendar , when we click on search button , result is displayed in the result view . This is a java applet and has a toolbar with buttons like new,save,copy etc,fields with drop down and also the result list below it.
    We have the option to prevent the button functionalities by controlling authorization to SAP objects.But my requirement is to completely hide these buttons from getting displayed in the applet view.
    Can anyone help me regarding how to remove the buttons from the java applet  .
    Appreciate the help a lot.
    Thanks
    Swapna.

    hii,
    checkout my add from, HTH
    [http://forums.sun.com/thread.jspa?threadID=5419921&messageID=10885709#10885709]
    ... kopik

  • Calling second servlet from first servlet

    I need to call a servlet from a servlet.
    I need to call the specific doGet method of servlet 2 from
    servlet 1. I also need to return the control to servlet 1
    after servlet 2 done its job.
    I did it now by creating a new instance of servlet 2 from servlet1
    and then call the doGet method like
    in servlet1 : Servlet2 serv2 = new Servlet2();
    I really don't like this idea of creating a new instance in servlet 1.
    Any thoughts/idea/comments would be appreciated.

    request.getRequestDispatcher("path/to/otherServlet").forward(request, response);
    or
    request.getRequestDispatcher("path/to/otherServlet").include(request, response);will do what you are looking for. See the API documentation on details. Basically the difference is that forward gives FULL control (including that of the headers) to the invoked servlet while include mearly "includes" the results.

  • A servlet  From an applet

    Dear friends,
    Hope you are doing well.
    I am facing a problem of how to call a servlet from an applet.
    Any help , please forward it.
    thanks
    vishal

    You can communicate with servlet from an applet, using URL connection, and then send POST/GET methods through HTTP protocol, here is a code snippet,
    // applet side, this method will send a POST method to the servlet and then get url openstream to read a
    // returned object from the servlet, params should be in the form param1=value&param2=value etc...
    public Object getObjectFromServlet(String servletName,String params) {
        Object object = null;
        try {
          URL url = new URL(codeBase,
                            contextRoot + servletName + params);
          URLConnection con = url.openConnection();
          con.setUseCaches(false);
          InputStream in = con.getInputStream();
          ObjectInputStream result = new ObjectInputStream(in);
          object = result.readObject();
          result.close();
          in.close();
        catch (ClassNotFoundException ex) {
          System.err.println("ClassNotFoundException caught:" + ex.getMessage());
        catch (MalformedURLException ex) {
          System.err.println("MalformedURLException caught:" + ex.getMessage());
        catch (IOException ex) {
          System.out.println("IOException caught:" + ex.getMessage());
        return object;
    //servlet side
    public void doPost(HttpServletRequest request, HttpServletResponse response){
          //Extract all the necessary supplied parameters from the request object and configurations.
           String param1 = request.getParameter("param1");
           String param2 = request.getParameter("param2");
      try {
            ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
            out.writeObject(object);
            out.close();
          catch (IOException ioe) {
            System.err.println("IOException caught:" + ioe.getMessage());
    }

  • Calling a servlet from java

    How to call a servlet from java?
    your help is greatly appreciated..

    Welcome to the forum
    You seem to be misunderstanding something. Do some googling to learn what you need about servlets. Simplified, servlets are java code that's hosted by a web container, such as Tomcat. When a servlet gets called as a consequence of someone requesting a url from the container, it takes in a request object and a response object. All the magic is then in creating an appropriate response (e.g. html) for the incoming request.

  • How can i call a servlet from a servlet ?

    Hello,
    Can i call a Servlet from within a Servlet ?
    and is it "right thing" to Do ?
    Thanks

    JMO, but I wouldn't do it like this.
    Don't have a remote object like a servlet doing database queries for your app. That will mean TWO network hops for every query if the database resides on another machine. The network is the biggest bottleneck you've got.
    Write a JavaBean that does the database stuff for you and just have the first servlet instantiate it when it needs it. A Bean has a better chance of being reused, too. Write a TableGateway or DAO for your object.
    Or write an EJB to do it.
    The only time I've done a servlet-to-servlet connection like that was for one servlet running in a DMZ that would authenticate a user and then forward the request to another servlet running inside the second firewall.
    I wouldn't encapsulate database query logic in a servlet like that. JMO - MOD

  • Call a servlet from Faces Portlet in Websphere

    Hi All,
    I have created a JSR-168 Faces Portlet project and using RAD(IDE) and WebSphere Portal server.
    I want to call a servlet from my jsf portlet.But i do not know where to place my servlet in the project directory.And how to invoke a call to my servlet from my portlet.
    Please help me find an answer.
    Best Regards,
    Pallavi

    hi Pallavi,
    Yes you can do that, what you have to do is you can get the ExternalFacesContext and forward your request to the servlet, you have entry for your servlet in web.xml...
    I am sure about it but you can try this
    Hope it will work.
    Surya

  • Calling a servlet from a Java Stored Procedure

    Hey,
    I'm trying to call a servlet from a Java Stored Procedure and I get an error.
    When I try to call the JSP-class from a main-method, everything works perfectly.
    Java Stored Procedure:
    public static void callServlet() {
    try {
    String servletURL = "http://127.0.0.1:7001/servletname";
    URL url = new URL(servletURL);
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestProperty("Pragma", "no-cache");
    conn.connect();
    ObjectInputStream ois = new ObjectInputStream(conn.getInputStream());
    Integer client = (Integer)ois.readObject();
    ois.close();
    System.out.println(client);
    conn.disconnect();
    } catch (Exception e) {
    e.printStackTrace();
    Servlet:
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    Integer id = new Integer(10);
    OutputStream os = response.getOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(os);
    oos.writeObject(id);
    oos.flush();
    oos.close();
    response.setStatus(0);
    Grant:
    call dbms_java.grant_permission( 'JAVA_USER', 'SYS:java.net.SocketPermission','localhost', 'resolve');
    call dbms_java.grant_permission( 'JAVA_USER','SYS:java.net.SocketPermission', '127.0.0.1:7001', 'connect,resolve');
    Package:
    CREATE OR REPLACE PACKAGE pck_jsp AS
    PROCEDURE callServlet();
    END pck_jsp;
    CREATE OR REPLACE PACKAGE BODY pck_jsp AS
    PROCEDURE callServlet()
    AS LANGUAGE JAVA
    NAME 'JSP.callServlet()';
    END pck_jsp;
    Architecture:
    AS: BEA WebLogic 8.1.2
    DB: Oracle 9i DB 2.0.4
    Exception:
    java.io.StreamCorruptedException: InputStream does not contain a serialized object
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java)
    The Servlet and the class work together perfectly, only when I make the call from
    within the database things go wrong.
    Can anybody help me.
    Thank in advance,
    Bart Laeremans
    ... Desperately seeking knowledge ...

    Look at HttpCallout.java in the following code sample
    http://www.oracle.com/technology/sample_code/tech/java/jsp/samples/jwcache/Readme.html
    Kuassi

  • How to call a method in the servlet from an applet

    I want to get the data for the applet from the servlet.
    How to call a method that exits in the servlet from the applet? I want to use http protocol to do this.
    Any suggestions? Please help.
    thanks

    Now that Web Services is around, I'd look at possibly implement a Web Service call in your applet, which can then be given back any object(s) on return. Set up your server side to handle Web Service calls. In this way, you can break the applet out into an application should you want to (with very little work) and it will still function the same

  • I need to call main window from a Standard program

    I need to call main window from a Standard program for SAP Script. I have wrote the code like this but it is not working. Kindly help me on that.
          FORM OPEN_AND_START_FORM                                     
    FORM open_and_start_form.
      CALL FUNCTION 'OPEN_FORM'
        EXPORTING
          device   = 'PRINTER'
          dialog   = space
          form     = 'ZOTC_SLI'
         language = print_co-spras
         OPTIONS  = pr_options
        EXCEPTIONS
          canceled = 01
          device   = 02
          form     = 03
          OPTIONS  = 04
          unclosed = 05.
      CHECK sy-subrc IS INITIAL.
      CALL FUNCTION 'WRITE_FORM'
        EXPORTING
          window  = 'MAIN'.
    ENDFORM.                    "OPEN_AND_START_FORM
          FORM CLOSE_AND_END_FORM_FORM                                  *
    FORM close_and_end_form.
      CALL FUNCTION 'END_FORM'.
    ENDFORM.                    "CLOSE_AND_END_FORM

    Hi,
    FORM CLOSE_AND_END_FORM_FORM *
    FORM close_and_end_form.
    CALL FUNCTION 'END_FORM'.
    change this to CALL FUNCTION 'close_FORM'.
    as you are not using the start_form
    you can use End_form
    i hope you understand now
    ENDFORM. "CLOSE_AND_END_FORM
    reward points if helpful.
    thanks & regards,
    venkatesh

  • Calling a Servlet from a JSP within Portal Portlet

    We've created a JSP that calls a Servlet when the form is submitted. The servlet handles the request, does some basic processing and then forwards back to a JSP. This works fine outside of Portal.
    What needs to be done in order for this to work within a portlet in Portal? We can get to the first JSP which is in a portlet, but as soon as we click the submit button (which tries to call the servlet) we get the basic page not found error. We need to click the button, go to the servlet and have the servlet forward back to our portlet.
    I know this is not the ideal way of using Portal however...
    Any ideas?
    Thanks!
    -Teri Kemple
    [email protected]

    I was wondering is there any way I can call a servlet from jsp? There are two scenarios here
    1 - if the jsp is currently being executed/evaluated on the server, then you can use <jsp:include> to "call" a servlet, or <jsp:forward> to cancel the jsp and transfer the control to the servlet.
    2 - if the page is already loaded and running in the client, then the only way to call a servlet is making a HTTP request as from any other html page. Jave code does not execute on the client, so you have to use html/javascript at that point.
    Calling a servlet within another servlet.
    lets say one servlet maintains all dynamic html. I call this servlet from another servlet to create a dynamic page and then insert values into it.Close, but the standard pattern is the reverse of that. What normally happens is you retrieve the values to put in the page, and then call the page, passing in those values. Thats why often you get a servlet/actionbean getting called, which then forwards to a JSP to display the result. The servlet/bean loads all the values required into scope, and then the JSP presents the data.

  • Question? calling a servlet from EJB.

    Can anyone tell me how I would go about calling a servlet
    from a session bean that’s running in the same J2EE application.
    Application server is Oracle10g.
    Thanks.

    Welcome to the forum
    You seem to be misunderstanding something. Do some googling to learn what you need about servlets. Simplified, servlets are java code that's hosted by a web container, such as Tomcat. When a servlet gets called as a consequence of someone requesting a url from the container, it takes in a request object and a response object. All the magic is then in creating an appropriate response (e.g. html) for the incoming request.

  • 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

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

Maybe you are looking for