Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7544437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:40:18+00:00 2026-05-30T08:40:18+00:00

I have an applet that needs to submit a score to a servlet and

  • 0

I have an applet that needs to submit a score to a servlet and it is not working correctly.

This is the code for the applet

private URLConnection getConnection() throws MalformedURLException, IOException {
        URL serverAddress = null;
        URLConnection conn = null;
        serverAddress = new URL("http://localhost/GamesPortal/submitScore");
        conn = serverAddress.openConnection();
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "application/x-java-serialized-object");
        return conn;
    }

    private void sendRecievedata(GameInfo info) {
        try {
            URLConnection c = this.getConnection();
            OutputStream os =  c.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(info);
            oos.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

And this is the servlet code

    try {
        HttpSession s = request.getSession(true);

        response.setContentType("application/x-java-serialized-object");
        InputStream in = request.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(in);
        GameInfo info = (GameInfo) ois.readObject();

        if (info.getUserId() > 0) {
            Scores score = new Scores();
            score.submitScore(info);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
    }
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet submitScore</title>");            
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet submitScore at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    } catch {
        ex.printStackTrace();
    } finally {            
        out.close();
    }

Now I have tried accessing the servlet via the browser, just to make sure that the address is correct (it is), but for some reason when I try to access it from the applet itself, it does not connect. (the debugger does not even launch).

(added the ex.printStackTrace(); to each of the try catches, as per suggestion, but I have no idea what or where I’m supposed to look for this)

The code calling the applet looks similar to this:
http://roseindia.net/jsp/simple-jsp-example/applet-in-jsp.shtml

<jsp:plugin code="Pong.class" name="Games/Pong/Pong" type="applet" width="800" height="600">
    <jsp:params>
        <jsp:param name="userId" value="<%= user.getUserId()%>" ></jsp:param>
    </jsp:params>
</jsp:plugin>

Is there something that I am overlooking here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:40:19+00:00Added an answer on May 30, 2026 at 8:40 am

    I have managed to make it work.

    This is the code for the applet:

        private URLConnection getServletConnection()
            throws MalformedURLException, IOException {
        URL urlServlet = new URL("http://localhost:8080/GamePortal/submitScore");
        URLConnection con = urlServlet.openConnection();
        con.setDoOutput(true);
        con.setRequestProperty(
                "Content-Type",
                "application/x-java-serialized-object");
        return con;
    
    }
    
    private void onSendData(GameInfo info) {
    
        try {
            // send data to the servlet
            URLConnection con = getServletConnection();
            OutputStream outstream = con.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(outstream);
            oos.writeObject(info);
            oos.flush();
            oos.close();
            // receive result from servlet
            InputStream instr = con.getInputStream();
            ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
            String result = (String) inputFromServlet.readObject();
            //JOptionPane.showMessageDialog(null, result);
            inputFromServlet.close();
            instr.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    

    This is the code for the servlet:

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {        
        try {
            response.setContentType("application/x-java-serialized-object");
            // read a String-object from applet
            // instead of a String-object, you can transmit any object, which
            // is known to the servlet and to the applet
            InputStream in = request.getInputStream();
            ObjectInputStream inputFromApplet = new ObjectInputStream(in);
            GameInfo score = (GameInfo) inputFromApplet.readObject();
            System.out.println(score.getScore());
    
            GameInfo info = score;
    
            if (info.getUserId() > 0) {
                Scores instance = new Scores();
                instance.submitScore(info);
            }
    
            OutputStream outstr = response.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(outstr);
            oos.writeObject("reply");
            oos.flush();
            oos.close();
        } catch (ClassNotFoundException ex) {
        }
    }
    

    Thanks for your help and forgive me for taking too long to reply.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an applet that communicates with a servlet using Http (Not sockets). Currently,
I have an AWT applet application that needs to be ported over to GWT.
I have an applet that is communicating with a servlet. I am communicating with
I have an applet that throws this exception when trying to communicate with the
I have an Java applet that loads native code through JNI. Everything worked just
I have an applet that needs access to the host system (to launch programs,
I have a java applet that needs to do http requests to the server.
I have a website with a Java Applet and that applet needs to connect
I have to create a java applet that needs to access static data which
I have an applet (Applet, not JApplet) that has a lot of classes organized

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.