在目前的互联网中,请求和响应无处不在,只是我们在一个网页中去查询一些信息或者注册用户,都会得到一些结果,在这过程中数据的流向和传输是什么样的?
大家应该比较熟悉socket了,作为服务端可以监听指定的端口并在有其他socket客服端链接这个端口的时候做对应的操作,其实我们的http请求中 也是类似的。http的服务端也是通过监听指定端口,并等待客服端去链接,之后判断客服端要请求的链接去做对应的操作,并通过流的形式的把操作的结果传到客服端。
下面模拟一个最简单的计算请求和响应的情景:
具体思路:
1.使用socket做服务端,并监听9090端口,等待客服端的登陆请求的访问(每个请求做成一个线程,提交到线程池中运行)。
2.拼接好请求url,并使用浏览器去访问这个链接
3.把serverSocket.accept();得到的对象封装为MyRequest、MyResponse对象,进行用户信息比对,返回登陆结果。
socket服务端:Server.java
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import com.mytomcat.test.socketaccept.SocketacceptRunnable;
/**
* 服务请求类
*
*/
public class Server {
private static ExecutorService serviceThreadPool = Executors.newFixedThreadPool(20);
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(9090);
while (true) {
socket = serverSocket.accept();//这里还有很多优化的地方,线程池的设置和接收的方式等
serviceThreadPool.submit(new SocketacceptRunnable(socket));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
把接收到的 serverSocket.accept(); 对象中请求的流转为字符串,这样我们就能看到请求的格式和字符,这样方便截取请求的url和url中传过来的参数:
MyRequest.java:
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public class MyRequest {
private final Socket socketAccpet;
private StringBuilder builder = new StringBuilder();
private InputStream in = null;
public MyRequest(Socket socketAccpet) {
this.socketAccpet = socketAccpet;
}
public String getRequestAllString() throws IOException{
in = this.socketAccpet.getInputStream();
byte[] buff = new byte[1024];
int length = 0;
if ((length = in.read(buff)) > 0) {
builder.append(new String(buff, 0, length));
}
System.out.println("接收到请求:"+builder.toString());
return builder.toString();
}
public String getURL() throws IOException{
String allStr = getRequestAllString();
if (allStr.indexOf("HTTP/1.1") != -1) {
return allStr.substring(allStr.indexOf("/"), allStr.indexOf("HTTP/1.1")-1);
} else {
return "";
}
}
}
MyResponse.java
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class MyResponse {
private final Socket socketAccpet;
private StringBuilder builder = null;
OutputStream out = null;
public MyResponse(Socket socketAccpet) {
this.socketAccpet = socketAccpet;
}
public void setResponseStr(StringBuilder builder){
this.builder = builder;
}
public void writeStr(String str) throws IOException{
out = this.socketAccpet.getOutputStream();
OutputStreamWriter streamWriter = new OutputStreamWriter(out);
streamWriter.write(str);
streamWriter.flush();
out.close();
streamWriter.close();
}
}
登录的处理类:LoginHttp.java
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.mytomcat.test.reqandres.MyRequest;
import com.mytomcat.test.reqandres.MyResponse;
public class LoginHttp {
private String name;
private String pwd;
public void service(MyRequest request,MyResponse response){
Calendar calendar = Calendar.getInstance();
try {
String urlStr = request.getURL();
String[] params = urlStr.substring(urlStr.indexOf("?")+1).split("&");
for (int i = 0; i < params.length; i++) {
String[] info = params[i].split("=");
if ("name".equals(info[0])) {
this.name = info[1];
} else if ("pwd".equals(info[0])) {
this.pwd = info[1];
}
}
if ("abc".equals(name) && "123abc".equals(pwd)) {
response.writeStr("登陆成功:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));
} else {
response.writeStr("登陆失败:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
每次请求都是应该线程:SocketacceptRunnable.java
import java.net.Socket;
import com.mytomcat.test.hanadler.LoginHttp;
import com.mytomcat.test.reqandres.MyRequest;
import com.mytomcat.test.reqandres.MyResponse;
public class SocketacceptRunnable implements Runnable {
private final Socket socketAccpet;
public SocketacceptRunnable(Socket socketAccpet) {
this.socketAccpet = socketAccpet;
}
@Override
public void run() {
try {
// BufferedReader bufferedReader = new BufferedReader(new FileR);
MyRequest myRequest = new MyRequest(socketAccpet);
System.out.println(myRequest.getRequestAllString());//先让MyRequest中的builder属性赋值
LoginHttp http = new LoginHttp();//请求处理类,这个可以做成配置
MyResponse response = new MyResponse(socketAccpet);
http.service(myRequest, response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试:
运行Server.java:
在IE浏览器中访问连接:http://localhost:9090/myhello?name=abc&pwd=123abc
观察控制台:
我们请求连接的时候,我们把socket的流转为字符:
GET /myhello?name=abc&pwd=123abc HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, */*
Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-GB;q=0.5,en;q=0.3
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)
Accept-Encoding: gzip, deflate
Host: localhost:9090
Connection: Keep-Alive
这个是按照http协议的一些参数,而get的请求方式的连接GET /myhello?name=abc&pwd=123abc HTTP/1.1,所以我们截取字符的时候有明确的位置,POST的请求方式是另一种
myTest001.html:
提交之后得到的请求内容为:
可见针对POST请求方式,我们获取请求url的时候和参数方式也有改变,这里的请求处理类只是指定 了LoginHttp,如果我们有其他的请求过来了,我们应该怎么处理?
1.我们先有创建应该xml配置文件,
web.xml
Login
com.mytomcat.test.hanadler.LoginHttp
LoginOut
com.mytomcat.test.hanadler.LoginOutHttp
2.解析这个文件,把请求的url和对应的class放到map中,
HttpServletMapUtil.java:
import java.io.File;
import java.util.HashMap;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.mytomcat.test.hanadler.IHttpServlet;
public class HttpServletMapUtil {
private static HashMap httpServletMap = null;
static {
httpServletMap = new HashMap<>();
SAXReader reader = new SAXReader();
File file = new File("config/web.xml");
try {
Document document = reader.read(file);
Element rootElement = document.getRootElement();
List elements = rootElement.elements("servlet");
for (Element element : elements) {
String servletname = element.element("servlet-name").getText().trim();
String servletClass = element.element("servlet-mapping").getText().trim();
httpServletMap.put(servletname, servletClass);
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static IHttpServlet getHttpServlet (String name) throws Exception{
IHttpServlet servlet = null;
try {
String className = httpServletMap.get(name);
servlet = (IHttpServlet) Class.forName(className).newInstance();
} catch (Exception e) {
throw new Exception("找不到对应处理类!!!"+"ServletName="+name);
}
return servlet;
}
}
3.定义应该处理接口,让处理类都实现这个接口:
IHttpServlet.java:
import com.mytomcat.test.reqandres.MyRequest;
import com.mytomcat.test.reqandres.MyResponse;
public interface IHttpServlet {
void service (MyRequest request,MyResponse response);
}
4.改进LoginHttp.java:
我们让其继承IHttpServlet即可:
public class LoginHttp implements IHttpServlet{}
5,新增退出处理:
LoginOutHttp.java:
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.mytomcat.test.reqandres.MyRequest;
import com.mytomcat.test.reqandres.MyResponse;
public class LoginOutHttp implements IHttpServlet{
public void service(MyRequest request,MyResponse response){
Calendar calendar = Calendar.getInstance();
try {
response.writeStr("退出成功:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
6.在请求分发的时候去找这个请求应该是哪个处理类处理。
改进SocketacceptRunnable.java:
@Override
public void run() {
try {
// BufferedReader bufferedReader = new BufferedReader(new FileR);
MyRequest myRequest = new MyRequest(socketAccpet);
System.out.println(myRequest.getRequestAllString());
String url = myRequest.getURL();
String nameUrl = url.substring(url.lastIndexOf("/")+1, url.indexOf("?"));
IHttpServlet servlet = HttpServletMapUtil.getHttpServlet(nameUrl);
// LoginHttp http = new LoginHttp();
MyResponse response = new MyResponse(socketAccpet);
servlet.service(myRequest, response);
} catch (Exception e) {
e.printStackTrace();
}
}
测试:
登陆:
退出:
当然,我们几乎不可能把这个实际使用,只是作为学习使用。