⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 starsserverconnector.java

📁 《Java网络程序设计.rar》包括三个网络程序的源代码。
💻 JAVA
字号:
/* * StarsServerConnector.java * * Created on January 14, 2002, 11:58 PM */package org.impact.stars.appclient.common;import java.util.*;import java.io.*;import java.net.*;import java.sql.Date;import javax.swing.JTextArea;import org.impact.stars.appclient.util.ClientUtil;import org.impact.stars.control.event.StarsEventSupport;/** * * @author  cai * @version * * This Class is the shared server connector for data model tranasmittion */public class StarsServerConnector {        JTextArea statusTextArea = null;    /** Creates new StarsServerConnector */    public StarsServerConnector() {    }        protected void setStatusTextArea(JTextArea sa)    {        statusTextArea = sa;    }    protected URLConnection getObjectConnectiontoServer(String connection) {        try{            log("Connecting to servlet...");            URL connectionURL = new URL( connection );            URLConnection servletConnection = connectionURL.openConnection();            log("Connected");                        // inform the connection that we will send output and accept input            servletConnection.setDoInput(true);            servletConnection.setDoOutput(true);                        // Don't used a cached version of URL connection.            servletConnection.setUseCaches(false);            log("SET REQUEST PROPERTY");                        // Specify the content type that we will send binary data            servletConnection.setRequestProperty            ("Content-Type", "application/octet-stream");            return servletConnection;        }        catch (Exception ex) {            System.out.println(ex);        }        return null;    }            protected URLConnection getNormalConnectiontoServer(String connection) {        try{            log("Connecting to servlet...");            URL connectionURL = new URL( connection );            URLConnection servletConnection = connectionURL.openConnection();            log("Connected");                        // inform the connection that we will send output and accept input            servletConnection.setDoInput(true);            servletConnection.setDoOutput(true);                        // Don't used a cached version of URL connection.            servletConnection.setUseCaches(false);            log("SET REQUEST PROPERTY");                        return servletConnection;        }        catch (Exception ex) {            System.out.println(ex);        }        return null;    }    /**     *  Reads a response from the servlet.     */    protected String readServletResponse(URLConnection servletConnection) {        BufferedReader inFromServlet = null;                try {            // now, let's read the response from the servlet.            // this is simply a confirmation string            inFromServlet = new BufferedReader(new InputStreamReader(servletConnection.getInputStream()));            String str ="";            String line;            while (null != ((line = inFromServlet.readLine()))) {                log("Reading servlet response: " + line + "; ");                str= str + line;            }                        inFromServlet.close();            return str;        }        catch (IOException e) {            log(e.toString());        }        return "NO Response";        //return "OK";    }      /**     *  Sends a perspective object to a servlet.  The perspective object is serialized over the URL connection     */    protected void sendEventToServlet(URLConnection servletConnection, StarsEventSupport theEvent) {        ObjectOutputStream outputToServlet = null;                try {            // send the perspective object to the servlet using serialization            log("Sending the perspective to the servlet...");            outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());                        // serialize the object            outputToServlet.writeObject(theEvent);                        outputToServlet.flush();            outputToServlet.close();            log("Complete.");        }        catch (IOException e) {            log(e.toString());        }    }              /**     * Called whenever the part throws an exception.     * @param exception java.lang.Throwable     */    protected void handleException(java.lang.Throwable exception) {                /* Uncomment the following lines to print uncaught exceptions to stdout */        // System.out.println("UNCAUGHT EXCEPTION:" + exception);        exception.printStackTrace(System.out);    }        /**     *  Simple logging method for status message in the text area.     */    protected void log(String msg) {        if (statusTextArea!=null)        {        statusTextArea.append(msg + "\n");        }        else         {            System.out.println(msg);        }              }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -