使用POST方式提交表单
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <h3>登录</h3> <form action="/login.do" method="psot"> <p>账号:<input type="text" name="account"></p> <p>密码:<input type="password" name="password"></p> <p><input type="submit" value="登 录"></p> </form> </body> </html>
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>主页</title> </head> <body> <h1>主页</h1> <a href="login.html" target="_self">登录</a> <hr> <img src="1.jpg"> </body> </html>
Request类
package web; import com.sun.org.apache.xml.internal.utils.Hashtree2Node; import java.util.HashMap; import java.util.Map; /** * 请求类 */ public class Request { private String method; private String uri; private Map<String,String> paramMap = new HashMap<>(); public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public String getUri() { return uri; } public void setUri(String uri) { this.uri = uri; } public Map<String, String> getParamMap() { return paramMap; } public void setParamMap(Map<String, String> paramMap) { this.paramMap = paramMap; } }
MyHttpServer类
package web; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class MyHttpServer { private static int count = 1; private static char arr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; public static void main(String[] args) throws IOException { byte b = 0x3f; byte2(b); ServerSocket server = new ServerSocket(8080); System.out.println("服务器已经启动,监听端口在8080..."); while (true){ Socket socket = server.accept(); InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); byte[] buf = new byte[1024*1024]; new Thread(new Runnable() { @Override public void run() { int len = 0; try { len = is.read(buf); if (len == -1) return; System.out.println("读到的字节数量是:"+len); String hexMsg = bytes2(buf, len); System.out.println(hexMsg); String s = new String(buf,0,len); System.out.println(s); Request request = parseRequest(s); // HTTP1.1协议 状态码200 正确 回车换行 String line1 = "HTTP/1.1 200 OK\r\n"; // 内容的类型是普通文本,回车换行 String line2 = "Content-Type:text/plain\r\n"; // 回车换行 final String line3 = "\r\n"; String file = null; if (request.getUri().equals("/") || request.getUri().equals("/index.html")){ line2 = "Content-Type:text/html\r\n"; file = "index.html"; }else if (request.getUri().equals("/favicon.ico")){ line2 = "Content-Type: image/x-icon\r\n"; file = "favicon.ico"; }else if (request.getUri().endsWith(".jpg") || request.getUri().endsWith(".jpeg")){ line2 = "Content-Type: image/jpeg\r\n"; file = request.getUri().substring(1);//过滤首字母斜杠(/) }else if (request.getUri().endsWith(".html")){ line2 = "Content-Type: text/html\r\n"; file = request.getUri().substring(1);//过滤首字母斜杠(/) }else if (request.getUri().equals("/login.do")) { String account = request.getParamMap().get("account"); String password = request.getParamMap().get("password"); if ("admin".equals(account) && "123".equals(password)) { line1 = "HTTP/1.1 302 Found\r\n"; line2 = "Localhost: index.html\r\n"; } else { line2 = "Content-Type: text/html\r\n"; file = "login.html"; } os.flush(); return; } // 固定的响应三行 os.write(line1.getBytes()); os.write(line2.getBytes()); os.write(line3.getBytes()); // 如果file不为空,说明需要回文件给浏览器 if (file != null){ FileInputStream fis = new FileInputStream(file); while ((len=fis.read(buf)) !=-1){ os.write(buf,0,len); } }else { // 返回浏览器的文本内容 os.write(("" + count).getBytes()); count++; } // 主动刷新一次 os.flush(); } catch (IOException e) { e.printStackTrace(); }finally { if (socket != null){ try { // 关闭资源 socket.close(); }catch (IOException e){ e.printStackTrace(); } } } } }).start(); } } /** * 解析封装为Request对象 * @param * @return */ private static Request parseRequest(String s){ Request req = new Request(); String[] split = s.split("\r\n"); String[] spl = split[0].split(" "); req.setMethod(spl[0]); if (spl[1].contains("?")){ String[] split1 = spl[1].split("[?]");//因为?是特殊字符所以要用中括号给括起来 req.setUri(spl[0]); String[] split2 = split1[1].split("&"); for (String s1 :split2){ int idx = s1.indexOf("="); req.getParamMap().put(s1.substring(0,idx),s1.substring(idx+1)); } }else { req.setUri(spl[1]); int i = s.indexOf("\r\n\r\n"); if (i != -1){ String m = s.substring(i + 4); String[] split2 = m.split("&"); for (String s1 :split2){ int idx = s1.indexOf("="); req.getParamMap().put(s1.substring(0,idx),s1.substring(idx+1)); } } } return req; } private static String bytes2(byte[] buf,int len){ StringBuffer sBuffer = new StringBuffer(); StringBuffer sb1 = new StringBuffer(); StringBuffer sb2 = new StringBuffer(); int cnt = 0; for(int i=0;i<len;i++){ sb1.append(byte2(buf[i]) + " "); if (buf[i] >= 0x20 && buf[i] <= 0x7e){ sb2.append((char)buf[i]); }else { sb2.append("."); } cnt++; if (cnt % 8 == 0) sb1.append(" "); if (cnt % 16 == 0) { sBuffer.append(sb1).append(sb2).append("\r\n"); sb1 = new StringBuffer(); sb2 = new StringBuffer(); cnt = 0; } } if (cnt != 0) sBuffer.append(sb1).append(" ").append(sb2).append("\r\n"); return sBuffer.toString(); } private static String byte2(byte bt){ int lo = bt & 0b00001111; int hi = (bt & 0b11110000) >> 4;//位移运算符 移四位,得到四个1 char clo = arr[lo]; char chi = arr[hi]; return chi+ "" + clo; } }
标签:getUri,String,request,表单,html,提交,new,POST,public From: https://www.cnblogs.com/yu3304/p/17277401.html