java webserver-封装响应协议
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java webserver-封装响应协议,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3701字,纯文字阅读大概需要6分钟。
内容图文
Response:
public class Response {
private BufferedWriter bw;
private Socket client;
private StringBuilder headInfo; //协议头包括状态行和请求头和回车
private StringBuilder content;
private int len=0; //正文的字节数
private final String BLANK=" ";
private final String CRLF="\r\n";
private Response()
{
content=new StringBuilder();
headInfo=new StringBuilder();
len=0;
}
public Response(Socket client)
{
this(); //调用默认构造器
try {
bw=new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
headInfo=null;
}
}
public Response(OutputStream os)
{
this(); //调用默认构造器
bw=new BufferedWriter(new OutputStreamWriter(os));
}
public Response print(String info) //流模式,不断添加内容返回自身
{
content.append(info);
len+=info.getBytes().length;
return this;
}
public Response println(String info)
{
content.append(info).append(CRLF);
len+=(info+CRLF).getBytes().length; //换行符也占长度
return this;
}
//推送响应信息
public void pushToBrowser(int code) throws IOException
{
if(null==headInfo) {
code=505;
}
createHeadInfo(code);
bw.append(headInfo); //bw.append()以追加模式写出数据,.write()刷新原有数据,只有当前数据
bw.append(content);
bw.flush();
}
//构建头信息
private void createHeadInfo(int code) //传不同的code有不同的状态
{
//1、响应行:HTTP/1.1 200 OK
headInfo.append("HTTP/1.1").append(BLANK);
headInfo.append(code).append(BLANK);
switch(code)
{
case 200:
headInfo.append("OK").append(CRLF);
break;
case 404:
headInfo.append("NOT FOUND").append(CRLF);
break;
case 505:
headInfo.append("SERVER ERROR").append(CRLF);
break;
}
//2、响应头
headInfo.append("Date:").append(new Date()).append(CRLF);
headInfo.append("Server:").append("shsxt Server/0.0.1;charset=GBK").append(CRLF);
headInfo.append("Content-type:text/html").append(CRLF);
headInfo.append("Content-length:").append(len).append(CRLF);
headInfo.append(CRLF); //响应头和正文之间有空行
}
}
Server:
public class Response {
private BufferedWriter bw;
private Socket client;
private StringBuilder headInfo; //协议头包括状态行和请求头和回车
private StringBuilder content;
private int len=0; //正文的字节数
private final String BLANK=" ";
private final String CRLF="\r\n";
private Response()
{
content=new StringBuilder();
headInfo=new StringBuilder();
len=0;
}
public Response(Socket client)
{
this(); //调用默认构造器
try {
bw=new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
headInfo=null;
}
}
public Response(OutputStream os)
{
this(); //调用默认构造器
bw=new BufferedWriter(new OutputStreamWriter(os));
}
public Response print(String info) //流模式,不断添加内容返回自身
{
content.append(info);
len+=info.getBytes().length;
return this;
}
public Response println(String info)
{
content.append(info).append(CRLF);
len+=(info+CRLF).getBytes().length; //换行符也占长度
return this;
}
//推送响应信息
public void pushToBrowser(int code) throws IOException
{
if(null==headInfo) {
code=505;
}
createHeadInfo(code);
bw.append(headInfo); //bw.append()以追加模式写出数据,.write()刷新原有数据,只有当前数据
bw.append(content);
bw.flush();
}
//构建头信息
private void createHeadInfo(int code) //传不同的code有不同的状态
{
//1、响应行:HTTP/1.1 200 OK
headInfo.append("HTTP/1.1").append(BLANK);
headInfo.append(code).append(BLANK);
switch(code)
{
case 200:
headInfo.append("OK").append(CRLF);
break;
case 404:
headInfo.append("NOT FOUND").append(CRLF);
break;
case 505:
headInfo.append("SERVER ERROR").append(CRLF);
break;
}
//2、响应头
headInfo.append("Date:").append(new Date()).append(CRLF);
headInfo.append("Server:").append("shsxt Server/0.0.1;charset=GBK").append(CRLF);
headInfo.append("Content-type:text/html").append(CRLF);
headInfo.append("Content-length:").append(len).append(CRLF);
headInfo.append(CRLF); //响应头和正文之间有空行
}
}
内容总结
以上是互联网集市为您收集整理的java webserver-封装响应协议全部内容,希望文章能够帮你解决java webserver-封装响应协议所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。