0

I building web application program using Netbeans 7.4 by creating jApplet file that communicate with servlet by Serializable object. Servlet file will communicate with DB and send results to applet and also applet has to use the Serializable object to send and receive data from servlet. I actually created both servlet and JApplet files under the source folder directory. The error is I ma not getting any communication between JApplet and servlet, and keeps giving me "protocol doesn't support output". Here is my Applet code that connecting to servlet:

    myObject = new ExpertDataObject();

    ((ExpertDataObject) myObject).setSession("EnterUser");

    ((ExpertDataObject) myObject).setUser(userText.getText());
    ((ExpertDataObject) myObject).setPassword(passText.getText());
    URL currentPage = getCodeBase();
    System.out.println(currentPage);

    String urlSuffix = "Server";
    URL urlServlet = new URL(getCodeBase(), "Server");
    System.out.println(urlServlet);

    URLConnection con = urlServlet.openConnection();

    con.setUseCaches(false);
    con.setDoOutput(true);
    con.setDoInput(true);
    con.setRequestProperty("Content-Type", "application/x-java-serialized-object");

    myOutputStream = new ObjectOutputStream(con.getOutputStream());

    myInputStream = new ObjectInputStream(con.getInputStream());

    myOutputStream.writeObject(myObject);

    myObject = (DataObject) myInputStream.readObject();
    if (myObject.getMessage().equals("success")) {
        dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");

....

Can anyone help me showing the steps of creating jApplet communicating with servlet +mySQL as web application in Netbeans.

Thanks in advance

Arwa

Here is what I found in Server log Feb 12, 2014 11:53:23 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [Server] in context with path [/ExpertToolAppletServlet2] threw exception java.io.EOFException at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2325) at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2794) at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801) at java.io.ObjectInputStream.(ObjectInputStream.java:299) at Server.processRequest(Server.java:73) at Server.doGet(Server.java:177) at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

And here is here is my servlet code under processRequest

   String contentType = "application/x-java-serialized-object";
   response.setContentType(contentType);
   InputStream inputStream = request.getInputStream();
   ObjectInputStream in = new ObjectInputStream(inputStream);
   myObject = (DataObject) in.readObject();
   String process= ((ExpertDataObject) myObject).getSession();
6
  • What is the output of System.out.println(urlServlet); ? Commented Feb 12, 2014 at 11:24
  • Hi peeskillet, thanks for reply, It is file:/Users/arwawali/Documents/NetBeansProjects/ExpertToolAppletServlet/build/web/WEB-INF/Server, Server is my servlet class name, and I looked to the path, I did not find the class there, how we can get servlet class path on the server other than using getCodeBase()? Commented Feb 12, 2014 at 13:58
  • The file:/ protocol does not allow writing. Read further here: stackoverflow.com/questions/4975743/… Commented Feb 12, 2014 at 14:10
  • Thanks PeterMmm for reply, I have change urlServlet to URL urlServlet = new URL("localhost:8080/ExpertToolAppletServlet2/Server"); Now I am getting this error: Server returned HTTP response code: 500 for URL: localhost:8080/ExpertToolAppletServlet2 Commented Feb 12, 2014 at 16:44
  • 500 - Internal Server Error look into the server log for the cause. Commented Feb 12, 2014 at 16:56

1 Answer 1

0

Concerning the server's 500 error message: As the docs for EOFException is saying: This exception is mainly used by data input streams to signal end of stream. you may try to catch the Exception:

   String contentType = "application/x-java-serialized-object";
   response.setContentType(contentType);
   InputStream inputStream = request.getInputStream();
   ObjectInputStream in = new ObjectInputStream(inputStream);

   myObject = null;
   try {
        myObject = (DataObject) in.readObject();
   } catch (EOFException ignore) {
   }

   if (myObject!=null) { 
      String process= ((ExpertDataObject) myObject).getSession();
   }
Sign up to request clarification or add additional context in comments.

5 Comments

I catched the exception, but I really do not know if there is any other way to read object from jApplet. Is that mean I have to run the code only from index.jsp file, or I can run it from source folder itself knowing that both jApplet and servlet in the same folder under web application project? Thank again for help.
Applets are running in the browser with Java plugin on client side.
Is this the right way to refer to jApplet in the source folder from index.jsp, <applet code="localhost:8080/ExpertToolAppletServlet2/MainScreen" width="700" height="500"></applet>, (MainScreen is jApplet class) ?
No, code is the FQN of the JApplet class. Read further here: docs.oracle.com/javase/tutorial/deployment/applet/html.html
Thanks so much PeterMmm. I will try your to catch the Exception

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.