实现功能为登陆,注册和这两个页面的跳转。登陆的页面为login.html,注册的页面为reg.html
HttpContext:
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException;
//获取配置协议
public class HttpContext {
// 存储格式的协议
static Map
static {
initContext();
}
private static void initContext() {
// 读取xml文件
try {
SAXReader reader = new SAXReader();// 主要读xml文件
Document document = reader.read(new File("web.xml"));
Element element = document.getRootElement();
List
for (Element el : elements) {
Element ell = el.element("extension");
String string = ell.getText();
Element el2 = el.element("mime-type");
String string2 = el2.getText();
// System.out.println(string + "," + string2);
map.put(string, string2);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 获取格式方法 传的html
public static String getContext(String name) {
//通过key 获取 值
return map.get(name);
}
public static void main(String[] args) {
//new HttpContext();
System.out.println(HttpContext.getContext("html"));
}
}
HttpRequest:
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
//请求类
public class HttpRequest {
//创建map集合存储客户端发来的信息
static Map
// 字节输入流
private InputStream is;
// 请求方式
private String method;
// 请求地址
private String Url;
// 请求协议
private String protrol;
// 服务端套接字
private Socket socket;
//进一步解析地址
private String RequestUrl;
public String getRequestUrl() {
return RequestUrl;
}
public HttpRequest(Socket socket) {// 类的构造器
try {
this.socket = socket;
this.is = socket.getInputStream();
read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getMethod() {
return method;
}
public String getUrl() {
return Url;
}
public String getProtrol() {
return protrol;
}
// 给请求属性赋值
void read() {
String string = readLine();
// 请求 客户端向服务器发送的请求 空格
String str[] = string.split("\\s");
this.method = str[0];
this.Url = str[1];
this.protrol = str[2];
//进一步解析
readjybjx();
}
private void readjybjx() {
// index.html
if (this.Url.contains("?")) {
System.out.println("ok了");
String str[] = this.Url.split("\\?");
System.out.println(Arrays.toString(str)+"哈");
this.RequestUrl = str[0];
String[] str2=str[1].split("&");
System.out.println(Arrays.toString(str2)+"哈哈哈");
for (String string:str2) {
String str3 []= string.split("=");
map.put(str3[0], str3[1]);
}
System.out.println("这是账号密码"+map);
}else {
this.RequestUrl=this.Url;
}
}
//通过key获取value
public static String getParameter(String name){
return map.get(name) ;
}
// 得到第一行字符串
public String readLine() {
StringBuilder builder = null;
try {
builder = new StringBuilder();
// InputStream is = socket.getInputStream();
// 换行符的accsi码值
char c = 10, d = 13;
int n = -1;
while ((n = is.read()) != -1) {
char l = (char) n;
if (l == c || l == d) {
break;
}
builder.append(l);
// System.out.println(n);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return builder.toString();
}
}
HttpResponse:
import java.io.*;
import java.net.Socket;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
//响应类
public class HttpResponse {
// 输出流
private OutputStream out;
// 文件对象
private File file;
public HttpResponse(Socket socket) {
try {
out = socket.getOutputStream();
} catch (Exception e) {
// TODO: handle exception
}
}
Map
public void setFile(File file) { // index.html
if (file.getName().contains(".")) {
String str[] = file.getName().split("\\.");
System.out.println(Arrays.toString(str));
String str1 = HttpContext.getContext(str[1]);// text/html
map.put("Content-Type ", str1);
map.put("Content-Length ", file.length()+"");
}
this.file = file;
}
void flush() {// 主方法
// 写协议部分
sendHtml();
// 写文件内容部分
sendXY();
sendFile();
}
private void sendFile() {
try {
FileInputStream fis = new FileInputStream(file);
byte b1[] = new byte[1024];
fis.read(b1);
out.write(b1);
} catch (Exception e) {
}
}
private void sendHtml() {
String line = "HTTP/1.1 200 OK";
print(line);
// 响应一个html 格式
// line = "Content-Type: text/html";
// print(line);
// // 响应内容的长度
// line = "Content-Length: " + file.length();
// print(line);
//
// print("");
}
private void sendXY() {
for (Entry
print(st.getKey() + ":" + st.getValue());
}
print("");
}
void print(String line) {
try {
byte b[] = line.getBytes();
out.write(b);
out.write(10);
out.write(13);
} catch (Exception e) {
}
}
}
HttpServer:
import java.util.HashMap;
import java.util.Map;
public class HttpServer {
static Map
static {
// 存储类的名字和请求
init();
}
private static void init() {//查询
// TODO Auto-generated method stub
map.put("/login", "Http.LoginServlet");
map.put("/reg", "Http.RegServlet");
}
//获取包名类名的一个方法
public static String getClass(String name){
return map.get(name);
}
//判断key值
public static boolean iskey(String key){
return map.containsKey(key);
}
public static void main(String[] args) {
System.out.println(getClass("/login"));
System.out.println(iskey("/reg"));
}
}
HttpServlet:
import java.io.File;
public abstract class HttpServlet {
//所有servlet 都必须写这个方法
abstract void service(HttpRequest httpRequest,HttpResponse httpResponse);
//响应地址 转发的过程 就是跳转页面
void forword(String Url,HttpRequest httpRequest,HttpResponse httpResponse){
httpResponse.setFile(new File(Url));
}
}
LoginServlet:
import java.io.File;
public class LoginServlet extends HttpServlet{
@Override
void service(HttpRequest httpRequest, HttpResponse httpResponse) {
//获取用户名
String name = httpRequest.getParameter("name");//login.html标签
//获取密码
String pwd = httpRequest.getParameter("pwd");
//更数据库比较 文本文档
System.out.println(name+","+pwd);//用文本文档实现登陆效果
httpResponse.setFile(new File("success.html"));
forword("webapps/success.html", httpRequest, httpResponse);
}
}
RegServlet:
import java.io.File;
public class RegServlet extends HttpServlet{
@Override
void service(HttpRequest httpRequest, HttpResponse httpResponse) {
// 获取账号密码
String name= httpRequest.getParameter("name");
String pwd= httpRequest.getParameter("pwd");
System.out.println("注册成功");
forword("webapps/success.html", httpRequest, httpResponse);
}
}
Server:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
//GET / HTTP/1.1
public class Server {
// 服务端套接字
private ServerSocket server;// 服务器
private Socket socket; // 客户端
private OutputStream out;//输出流 写
// 初始化服务端套接字
public Server() {
try {
System.out.println("等待客户端连接");
server = new ServerSocket(9799);
// 等待客户端连接的方法
/**
* 如果客户端没访问 就一直停在这个地方
*/
// 获取字节流
// out = socket.getOutputStream();
// System.out.println("客户端连接");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void start() {
while (true) {
try {
socket = server.accept();
HttpRequest httpRequest = new HttpRequest(socket);
HttpResponse httpResponse = new HttpResponse(socket);
// 获得请求地址
String Url = httpRequest.getRequestUrl();//
File file = new File("webapps" + Url);
// 判断
if (HttpServer.iskey(Url)) {
String name = HttpServer.getClass(Url);
System.out.println(name);
try {
Class class1 = Class.forName(name);
HttpServlet servlet = (HttpServlet) class1.newInstance();
servlet.service(httpRequest, httpResponse);
} catch (Exception e) {
// TODO: handle exception
}
} else if (file.exists()) {
System.out.println("文件在");
httpResponse.setFile(file);
} else {
System.out.println("文件不在");
httpResponse.setFile(new File("404.html"));
}
// 响应页面
httpResponse.flush();
} catch (Exception e) {
}
}
}
public static void main(String[] args) {
Server server = new Server();
server.start();
}
}
login.html:
reg.html:
success.html: