我如何将jApplet导入jsp以通过Serializable数据对象与servlet进行通信

时间:2014-02-21 22:32:27

标签: java jsp servlets serialization applet

我有applet类,它使用许多Serializable DataObjects类连接到servlet 我在Netbeans中创建了三个项目,Project 1是具有servlet类和index.jsp文件的Web应用程序项目。 Project 2是具有jApplet类的JAVA项目应用程序,而项目3是具有所有Seri​​alizable DataObjects类和其他java类的JAVA项目应用程序。

我将项目3作为jar文件添加到项目1和2,以使数据对象类可用于servlet和jApplet。

现在我想从项目2到项目1中的index.jsp的jApplet类通过Serializable DataObjects与servlet进行通信。

我的问题:我正在做的是对的,如果是的话,我怎样才能将jApplet包含到可以与servlet通信的jsp文件中,以及如何正确设置jApplet的路径。

与服务器通信的applet功能是: 。 。

                           URLConnection connection = getServletConnection();
                                                    //connection.setUseCaches(false);
                        //   connection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
                        //j    connection.setRequestProperty("Accept","*/*");
                              System.out.println(connection);
                //myOutputStream = new ObjectOutputStream(
                //      socketToServer.getOutputStream());
                            myOutputStream = new ObjectOutputStream(
                        connection.getOutputStream());
                            myOutputStream.writeObject(myObject);
                            myOutputStream.flush();
                            myOutputStream.close();

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

                //connection.getr

                myObject = (DataObject) myInputStream.readObject();
                            if (myObject.getMessage().equals("success")){
                                dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
                                //DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
                                date = new Date();

                                System.out.println(date);
                               //CardLayout cl = (CardLayout) (javax.swing.JApplet).;

                               userName=  ((ExpertDataObject) myObject).getUser();
                               jPanel3.show(true);
                               undoB.setEnabled(false);
                               redoB.setEnabled(false);
                               jPanel4.hide();
                               createGUI();

                                myInputStream.close();


                           }
                           else{
                               ErrorText.setText(((ExpertDataObject) myObject).getError());
                                userText.setFocusable(true);
                                passText.setFocusable(true);
                           }

 private URLConnection getServletConnection()
    throws MalformedURLException, IOException, Exception {


            URL urlServlet=new URL("http://localhost:8080/ExpertToolAppletServlet2/Server");
    //URL urlServlet = new URL(getCodeBase(), "Server");
    URLConnection con = urlServlet.openConnection();
            con.setDoOutput(true);
            if (con instanceof HttpURLConnection)
            {
                ((HttpURLConnection)con).setRequestMethod("POST");
            }
            else
            {
                throw new Exception("this connection is NOT an HttpUrlConnection connection");
            }
    // konfigurieren
    con.setDoInput(true);

    con.setUseCaches(false);
             con.setDefaultUseCaches(false);


    con.setRequestProperty(
        "Content-Type","application/x-java-serialized-object");

    con.connect();
    return con;
}

servlet代码是:

  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, ClassNotFoundException, SQLException, OntologyLoadException {
    HttpSession s = request.getSession(true);

    String contentType = "application/x-java-serialized-object";

    response.setContentType(contentType);
   //PrintWriter a = response.getWriter();
  // a.write("Arwa");
   System.out.println("I am here");
   ObjectInputStream in;
    String process;
    Statement statement;
    Statement statement1;
    Statement statement2;
    Statement statement3;
    try (InputStream inputStream = request.getInputStream()) {
        in = new ObjectInputStream(inputStream);
        try {
            myObject = (DataObject) in.readObject();
        } catch (EOFException ignore) {
            System.out.println("Here i am" +ignore);
        }  process = null;
     if (myObject!=null) {
         process= ((ExpertDataObject) myObject).getSession();
     }
     else{
         System.out.println("my object is null");
     }   SQL_Connection SC= new SQL_Connection();
        statement = SC.conn.createStatement();
        statement1 = SC.conn.createStatement();
        statement2 = SC.conn.createStatement();
        statement3 = SC.conn.createStatement();
        System.out.println("Message written: " + myObject.getMessage());
    }

HTML代码

  <applet code="experttoolappletservletclient.MainScreen"    archive="ExpertToolAppletServletClient.jar" width="600" height="480"/>

这样,experttoolappletservletclient是project2下包含MainScreen jApplet的源文件夹,而ExpertToolAppletServletClient是项目2名称。

提前致谢

0 个答案:

没有答案