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

📄 servletunithttpresponse.java

📁 这是远程web服务调用的一个包,可以将JSP直接作为服务
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * to the client. In HTTP servlets, this method sets the     * HTTP Content-Length header.     **/    public void setContentLength( int len ) {        setIntHeader( "Content-Length", len );    }//------------------------------- the following methods are new in JSDK 2.2 ----------------------    /**     * Adds a response header with the given name and value. This method allows response headers to have multiple values.     **/    public void addHeader( String name, String value ) {        synchronized (_headers) {            String key = name.toUpperCase();            ArrayList values = (ArrayList) _headers.get( key );            if (values == null) {                values = new ArrayList();                _headers.put( key, values );            }            values.add( value );        }    }    /**     * Adds a response header with the given name and value. This method allows response headers to have multiple values.     **/    public void addIntHeader( String name, int value ) {        addHeader( name, asHeaderValue( value ) );    }    /**     * Adds a response header with the given name and value. This method allows response headers to have multiple values.     **/    public void addDateHeader( String name, long value ) {        addHeader( name, asDateHeaderValue( value ) );    }    /**     * Sets the preferred buffer size for the body of the response. The servlet container     * will use a buffer at least as large as the size requested. The actual buffer size     * used can be found using getBufferSize.     **/    public void setBufferSize( int size ) {        if (getContents().length != 0) throw new IllegalStateException( "May not set buffer size after data is written" );    }    /**     * Returns the actual buffer size used for the response. If no buffering is used, this method returns 0.     **/    public int getBufferSize() {        return 0;    }    /**     * Returns a boolean indicating if the response has been committed. A committed response has     * already had its status code and headers written.     **/    public boolean isCommitted() {        return _committed;    }    /**     * Forces any content in the buffer to be written to the client. A call to this method automatically     * commits the response, meaning the status code and headers will be written.     **/    public void flushBuffer() throws IOException {        _committed = true;    }    /**     * Clears any data that exists in the buffer as well as the status code and headers.     * If the response has been committed, this method throws an IllegalStateException.     **/    public void reset() {        resetBuffer();        _headers.clear();        _headersComplete = false;        _status = SC_OK;    }    /**     * Sets the locale of the response, setting the headers (including the Content-Type's charset)     * as appropriate. This method should be called before a call to getWriter().     * By default, the response locale is the default locale for the server.     **/    public void setLocale( Locale locale ) {        _locale = locale;        if (_encoding == null) {            for (Iterator it = ENCODING_MAP.entrySet().iterator(); it.hasNext();) {                Map.Entry entry = (Map.Entry) it.next();                String locales = (String) entry.getValue();                if (locales.indexOf( locale.getLanguage() ) >= 0 || locales.indexOf( locale.toString() ) >= 0) {                    _encoding = (String) entry.getKey();                    return;                }            }        }    }    /**     * Returns the locale assigned to the response.     **/    public Locale getLocale() {        return _locale;    }//----------------------------- methods added to ServletResponse in JSDK 2.3 --------------------------------------    /**     * Clears the content of the underlying buffer in the response without clearing headers or status code.     * If the response has been committed, this method throws an IllegalStateException.     *     * @since 1.3     */    public void resetBuffer() {        if (_committed) throw new IllegalStateException( "May not resetBuffer after response is committed" );        _outputStream = null;        _servletStream = null;        _writer = null;    }//---------------------------------------------- package methods --------------------------------------------------    /**     * Returns the contents of this response.     **/    byte[] getContents() {        if (_outputStream == null) {            return new byte[0];        } else {            if (_writer != null) _writer.flush();            return _outputStream.toByteArray();        }    }    /**     * Returns the status of this response.     **/    int getStatus() {        return _status;    }    /**     * Returns the message associated with this response's status.     **/    String getMessage() {        return _statusMessage;    }    public String[] getHeaderFieldNames() {        if (!_headersComplete) completeHeaders();        Vector names = new Vector();        for (Enumeration e = _headers.keys(); e.hasMoreElements();) {            names.addElement( e.nextElement() );        }        String[] result = new String[ names.size() ];        names.copyInto( result );        return result;    }    /**     * Returns the headers defined for this response.     **/    String getHeaderField( String name ) {        if (!_headersComplete) completeHeaders();        ArrayList values;        synchronized (_headers) {            values = (ArrayList) _headers.get( name.toUpperCase() );        }        return values == null ? null : (String) values.get( 0 );     }     /**     * Return an array of all the header values associated with the     * specified header name, or an zero-length array if there are no such     * header values.     *     * @param name Header name to look up     */    public String[] getHeaderFields(String name) {        if (!_headersComplete) completeHeaders();        ArrayList values;        synchronized (_headers) {            values = (ArrayList) _headers.get(name.toUpperCase());        }        if (values == null)            return (new String[0]);        String results[] = new String[values.size()];        return ((String[]) values.toArray(results));    }//--------------------------------------- methods added to ServletRequest in Servlet API 2.4 ----------------------------    public void setCharacterEncoding(String string) {        _encoding = string;    }    /**     * Returns the content type defined for this response.     **/    public String getContentType() {        return _contentType;    }//------------------------------------------- private members ------------------------------------    private String _contentType = "text/plain";    private String _encoding;    private PrintWriter  _writer;    private ServletOutputStream _servletStream;    private ByteArrayOutputStream _outputStream;    private int _status = SC_OK;    private String _statusMessage = "OK";    private final Hashtable _headers = new Hashtable();    private boolean _headersComplete;    private Vector  _cookies = new Vector();    private void completeHeaders() {        if (_headersComplete) return;        addCookieHeader();        setHeader( "Content-Type", _contentType + "; charset=" + getCharacterEncoding() );        _headersComplete = true;    }    private void addCookieHeader() {        if (_cookies.isEmpty()) return;        StringBuffer sb = new StringBuffer();        for (Enumeration e = _cookies.elements(); e.hasMoreElements();) {            Cookie cookie = (Cookie) e.nextElement();            sb.append( cookie.getName() ).append( '=' ).append( cookie.getValue() );            if (cookie.getPath() != null) sb.append( ";path=" ).append( cookie.getPath() );            if (cookie.getDomain() != null) sb.append( ";domain=" ).append( cookie.getDomain() );            if (e.hasMoreElements()) sb.append( ',' );        }        setHeader( "Set-Cookie", sb.toString() );    }    static {        ENCODING_MAP.put( "iso-8859-1", "ca da de en es fi fr is it nl no pt sv " );        ENCODING_MAP.put( "iso-8859-2", "cs hr hu pl ro sh sk sl sq " );        ENCODING_MAP.put( "iso-8859-4", "et lt lv ");        ENCODING_MAP.put( "iso-8859-5", "be bg mk ru sr uk " );        ENCODING_MAP.put( "iso-8859-6", "ar " );        ENCODING_MAP.put( "iso-8859-7", "el " );        ENCODING_MAP.put( "iso-8859-8", "iw he " );        ENCODING_MAP.put( "iso-8859-9", "tr " );        ENCODING_MAP.put("Shift_JIS", "ja ");        ENCODING_MAP.put("EUC-KR",    "ko ");        ENCODING_MAP.put("TIS-620",   "th ");        ENCODING_MAP.put("GB2312",    "zh " );        ENCODING_MAP.put("Big5",      "zh_TW zh_HK " );    }}class ServletUnitOutputStream extends ServletOutputStream {    ServletUnitOutputStream( ByteArrayOutputStream stream ) {        _stream = stream;    }    public void write( int aByte ) throws IOException {        _stream.write( aByte );    }    private ByteArrayOutputStream _stream;}

⌨️ 快捷键说明

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