您的位置:首页 > 编程语言 > PHP开发

Servlet--ServletInputStream类,ServletOutputStream类

2015-06-15 11:22 836 查看
ServletInputStream类

定义

public abstract class ServletInputStream extends InputStream

这个类定义了一个用来读取客户端的请求信息的输入流。这是一个 Servlet 引擎提供的抽象类。一个 Servlet 通过使用ServletRequest 接口获得了对一个 ServletInputStream 对象的说明。这个类的子类必须提供一个从 InputStream 接口读取有关信息的方法。

方法

1、readLine

public int readLine(byte[] b, int off, int len) throws IOException;


从输入流的指定的偏移量开始将指定长度的字节读入到指定的数组中。 如果该行所有请求的内容都已被读取,这个读取的过程将结束。如果是遇到了新的一行,新的一行的首个字符也将被读入到数组中。

package javax.servlet;

import java.io.IOException;
import java.io.InputStream;

public abstract class ServletInputStream extends InputStream
{
public int readLine(byte[] b, int off, int len)
throws IOException
{
if (len <= 0) {
return 0;
}
int count = 0;
int c;
while ((c = read()) != -1) {
b[(off++)] = (byte)c;
count++;
if (c == 10) break; if (count == len) {
break;
}
}
return count > 0 ? count : -1;
}
}


ServletOutputStream类

定义

public abstract class ServletOutputStream extends OutputStream

这是一个由 Servlet 引擎使用的抽象类。Servlet 通过使用 ServletResponse 接口的使用获得了对一个这种类型的对象的说明。利用这个输出流可以将数据返回到客户端。这个类的子类必须提供一个向 OutputStream 接口写入有关信息的方法。在这个接口中, 当一个刷新或关闭的方法被调用时。 所有数据缓冲区的信息将会被发送到客户端,也就是说响应被提交了。请注意,关闭这种类型的对象时不一定要关闭隐含的socket
流。


方法

1、print

public void print(String s) throws IOException;

public void print(boolean b) throws IOException;

public void print(char c) throws IOException;

public void print(int i) throws IOException;

public void print(long l) throws IOException;

public void print(float f) throws IOException;

public void print(double d) throws IOException;

输出变量到输出流中

2、println

public void println() throws IOException;

public void println(String s) throws IOException;

public void println(boolean b) throws IOException;

public void println(char c) throws IOException;

public void println(int i) throws IOException;

public void println(long l) throws IOException;

public void println(float f) throws IOException;

public void println(double d) throws IOException;


输出变量到输出流中,并增加一个回车换行符。

package javax.servlet;

import java.io.CharConversionException;
import java.io.IOException;
import java.io.OutputStream;
import java.text.MessageFormat;
import java.util.ResourceBundle;

public abstract class ServletOutputStream extends OutputStream
{
private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.LocalStrings");

public void print(String s) throws IOException
{
if (s == null)
s = "null";
int len = s.length();
for (int i = 0; i < len; i++)
{
char c = s.charAt(i);

if ((c & 0xFF00) != 0)
{
String errMsg = lStrings.getString("err.not_iso8859_1");
Object[] errArgs = new Object[1];
errArgs[0] = new Character(c);
errMsg = MessageFormat.format(errMsg, errArgs);
throw new CharConversionException(errMsg);
}
write(c);
}
}

public void print(long l) throws IOException
{
print(String.valueOf(l));
}

//println无参方法
public void println() throws IOException
{
print("\r\n");
}

//print()之后  在调下println无参方法
public void println(String s) throws IOException
{
print(s);
println();
}

public void println(int i) throws IOException
{
print(i);
println();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: