request接口:
package xbt.server2;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
interface IRequest {
String getServerName();
String getServerIP();
String getRequestURL();
String getMethod();
int getServerPort();
String getRequestIP();
InputStream getInputStream();
Map<String, List<String>> getParameterMap();
Enumeration<String> getParameterNames();
Enumeration<String> getParameterValues(String name);
String getParameter(String name);
String[] getParameters(String name);
void setAttribute(String key, Object value);
Object getAttribute(String key);
Enumeration<String> getAttributeNames(String key);
String getHandle();
}
HttpServletRequest接口:
package xbt.server2;
public interface HttpServletRequest extends IRequest {
String getHttpVersion();
}
AbstractHttpServletRequest抽象类:
package xbt.server2;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
public abstract class AbstractHttpServletRequest implements HttpServletRequest {
protected InputStream inputStream;
protected String method;
protected String requestURL;
protected Map<String, List<String>> parameter;
protected Map<String, Object> attribute;
public AbstractHttpServletRequest() {
}
public AbstractHttpServletRequest(InputStream inputStream) {
this.inputStream = inputStream;
}
}
HttpServletRequestClass 实体类:
package xbt.server2;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
public class HttpServletRequestClass extends AbstractHttpServletRequest {
public HttpServletRequestClass() {
super();
parameter = new HashMap<String, List<String>>();
attribute = new HashMap<String, Object>();
}
public HttpServletRequestClass(InputStream inputStream) {
super(inputStream);
parameter = new HashMap<String, List<String>>();
attribute = new HashMap<String, Object>();
initHttpServletRequest();
}
private void close() {
if (inputStream != null)
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void initHttpServletRequest() {
byte[] bts = new byte[1024 * 5];
try {
// 读取请求数据
int length = inputStream.read(bts);
close();
String str = new String(bts, 0, length).trim();
// 解析请求方法
method = str.substring(0, str.indexOf("/")).trim();
// 解析请求URL
String url = str.substring(str.indexOf("/") + 1, str.indexOf(" HTTP")).trim();
int index = url.indexOf("?");
if (index == -1)
requestURL = url;
else {
requestURL = url.substring(0, index);
// 解析URL后缀参数
String parameterStr = url.substring(index + 1, url.length());
String[] parameters = parameterStr.split("&");
for (String p : parameters) {
String[] pt = p.split("=");
List<String> list = parameter.get(pt[0]);
if (list == null) {
list = new ArrayList<String>();
list.add(pt[1]);
parameter.put(pt[0], list);
} else
list.add(pt[1]);
}
}
// get请求
if ("GET".equals(method)) {
}
// post请求
else {
// 解析form表单数据
String parameterStr = str.substring(str.lastIndexOf("\r\n"), str.length()).trim();
String[] parameters = parameterStr.split("&");
for (String p : parameters) {
String[] pt = p.split("=");
List<String> list = parameter.get(pt[0]);
if (list == null) {
list = new ArrayList<String>();
list.add(pt[1]);
parameter.put(pt[0], list);
} else
list.add(pt[1]);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public String getServerName() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getServerIP() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return method;
}
@Override
public int getServerPort() {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getRequestIP() {
// TODO Auto-generated method stub
return null;
}
@Override
public InputStream getInputStream() {
return inputStream;
}
@Override
public Map<String, List<String>> getParameterMap() {
return parameter;
}
@Override
public Enumeration<String> getParameterNames() {
return new Vector(parameter.keySet()).elements();
}
@Override
public Enumeration<String> getParameterValues(String name) {
return null;
}
@Override
public String getParameter(String name) {
if (parameter.get(name) != null)
return parameter.get(name).get(0);
else
return null;
}
@Override
public String[] getParameters(String name) {
if (parameter.get(name) != null)
return (String[]) parameter.get(name).toArray();
else
return null;
}
@Override
public void setAttribute(String key, Object value) {
// TODO Auto-generated method stub
}
@Override
public Object getAttribute(String key) {
// TODO Auto-generated method stub
return null;
}
@Override
public Enumeration<String> getAttributeNames(String key) {
// TODO Auto-generated method stub
return null;
}
@Override
public String getHandle() {
return null;
}
@Override
public String getRequestURL() {
return requestURL;
}
@Override
public String getMethod() {
return method;
}
@Override
public String getHttpVersion() {
// TODO Auto-generated method stub
return null;
}
}