java服务器的简单实现过程记录
一、http
http请求
一般一个http请求包括以下三个部分:
1 请求方法,如get,post
2 请求头
3 实体
一个http请求的实例如下:
get /index.jsp http/1.1
host: localhost:8080
user-agent: mozilla/5.0 (windows nt 5.1; rv:15.0) gecko/20100101 firefox/15.0
accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
accept-language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
accept-encoding: gzip, deflate
connection: keep-alive
注意红色部分的url,这是我们待会要截取出来的。
http响应
与http请求类似,http响应也包括三个部分
1 协议-状态码-描述
2 响应头
3 响应实体段
二 socket类
套接字是网络连接的断点。套接字使得应用程序可以从网络中读取数据,可以向网络中写入数据。不同计算机上的应用程序可以通过套接字发送或接受字节流。java中提供了socket类来实现这个功能,通过getinputstream和getoutputstream可以获取网络中的输入输出流。
但是,光靠socket类还是不能够实现我们构建一个服务器应用程序的功能的,因为服务器必须时刻待命,因此java里面提供了serversocket类来处理等待来自客户端的请求,当serversocket接受到了来自客户端的请求之后,它就会创建一个实例来处理与客户端的通信。
三 服务器应用程序的实现
首先,我们要构建一个封装请求信息的类requst,一个响应请求的类response,还要有一个主程序httpserver来处理客户端来的请求。
package com.lwq; import java.io.ioexception; import java.io.inputstream; /** * @author joker * 类说明 * 将浏览器发来的请求信息转化成字符和截取url */ public class request { //输入流 private inputstream input; //截取url,如http://localhost:8080/index.html ,截取部分为 /index.html private string uri; public request(inputstream inputstream){ this.input = inputstream; } public inputstream getinput() { return input; } public void setinput(inputstream input) { this.input = input; } public string geturi() { return uri; } public void seturi(string uri) { this.uri = uri; } //从套接字中读取字符信息 public void parse(){ stringbuffer request = new stringbuffer(2048); int i = 0; byte[] buffer = new byte[2048]; try { i = input.read(buffer); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); i = -1; } for(int j = 0;j<i;j++){ request.append((char)(buffer[j])); } system.out.println(request.tostring()); uri = parseuri(request.tostring()); } //截取请求的url private string parseuri(string requeststring){ int index1 = 0; int index2 = 0; index1 = requeststring.indexof(' '); if(index1!=-1){ index2 = requeststring.indexof(' ',index1+1); if(index2>index1){ return requeststring.substring(index1+1,index2); } } return null; } }
下面是封装了响应请求的类response:
package com.lwq; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import java.io.outputstream; import java.io.printwriter; /** * @author joker * @version 创建时间:sep 5, 2012 9:59:53 pm * 类说明 根据相应信息返回结果 */ public class response { private static final int buffer_size = 1024; request request; outputstream output; public response(outputstream output){ this.output = output; } public void sendstaticresource() throws ioexception{ byte[] bytes = new byte[buffer_size]; fileinputstream fis = null; file file = new file(httpserver.web_root,request.geturi()); if(file.exists()){ try { fis = new fileinputstream(file); int ch = fis.read(bytes,0,buffer_size); while(ch != -1){ output.write(bytes,0,ch); ch = fis.read(bytes,0,buffer_size); } } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); }catch(ioexception e){ e.printstacktrace(); }finally{ if(fis !=null){ fis.close(); } } }else{ //找不到文件 string errormessage = "http/1.1 404 file not found\r\n" + "content-type: text/html\r\n" + "content-length: 23\r\n" + "\r\n" + " file not found "; try { output.write(errormessage.getbytes()); output.flush(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } public request getrequest() { return request; } public void setrequest(request request) { this.request = request; } public outputstream getoutput() { return output; } public void setoutput(outputstream output) { this.output = output; } public static int getbuffer_size() { return buffer_size; } }
接下来是主程序,
package com.lwq; import java.io.file; import java.io.inputstream; import java.io.outputstream; import java.net.inetaddress; import java.net.serversocket; import java.net.socket; /** * @author joker * 类说明 */ public class httpserver { /** * @param args */ //web_root是服务器的根目录 public static final string web_root = system.getproperty("user.dir")+file.separator+"webroot"; //关闭的命令 private static final string shutdown_command= "/shutdown"; public static void main(string[] args) { // todo auto-generated method stub httpserver server = new httpserver(); server.await(); } public void await(){ serversocket serversocket = null; int port = 8080; try { serversocket = new serversocket(port,1,inetaddress.getbyname("127.0.0.1")); while(true) { try { socket socket = null; inputstream input = null; outputstream output = null; socket = serversocket.accept(); input = socket.getinputstream(); output = socket.getoutputstream(); //封装request请求 request request = new request(input); request.parse(); //封装response对象 response response = new response(output); response.setrequest(request); response.sendstaticresource(); socket.close(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); continue; } } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } }
运行httpserver,在浏览器中打下http://localhost:8080/index.jsp,就能看到服务器响应的结果了。
总结
到此这篇关于java服务器的简单实现的文章就介绍到这了,更多相关java服务器实现内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!