首页 > 其他分享 >使用POST方式提交表单

使用POST方式提交表单

时间:2023-03-31 20:23:22浏览次数:24  
标签:getUri String request 表单 html 提交 new POST public

使用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

相关文章

  • 前端工程化实践 - 多人开发分支管理、Git记录提交规范(二)
    一、前言Git在工作中是很重要的一部分,如果操作不熟练或者使用不规范,很容易给工作造成很多麻烦比如习惯所有功能写在一个分支,导致无法分开上线比如提交了依赖目录,导致Git仓库的代码过大比如合并分支出现错误,将不用上线的代码提交比如分支命名不规范,导致误删分支这一篇正好是前端工程......
  • element-ui plus中如何单独出发el-upload提交
    因为单独提交才好触发el-upload中的on-success函数在Vue3中,可以通过ref引用指向upload组件,然后使用该引用调用upload的submit方法来触发上传操作。具体实现如下:<template><el-uploadref="uploadRef"action="https://www.mocky.io/v2/5cc8019d300000980a05......
  • 使用Apipost自动化测试工具来优化测试流程
    随着项目研发进程的不断推进,软件功能不断增多,对于软件测试的要求也越来越高。为了提高测试效率和减少测试成本,许多软件测试团队借助于自动化测试工具来优化测试流程。Apipost也提供了自动化测试工具,在本文中,我们将探讨如何借助Apipost自动化测试工具来优化测试流程。Apipost......
  • postrelevant
    爬虫中post相关HTTP数据传输先来看看HTTP是如何传输表单数据的。HTTP是以ASCII码传输的,建立在TCP/IP协议之上的应用层规范。规范把HTTP请求分为三个部分:状态行、请求头、消息主体。类似于下面这样:<method><url><version><headers><entity-body>#例如:#请求行......
  • Git 提交出现冲突解决方案
    原文链接:https://blog.csdn.net/baidu_35523558/article/details/1252556841、右键git->Respository-->StashChanges2、message中输入本次提交大概信息(我一般是日期+内容),点击CreateStash3、然后本地代码就会全部还原到最近更新的一次状态,这时从git上更新代码;4、更新完成后......
  • 表单登录回显
    表单登录回显页面login.html<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>登录</title></head><body><h3>登录</h3><formaction="login.do"m......
  • 自建邮件服务器 post.io
    搭建服务所选服务器的官网:https://poste.io/用docker安装吧,别去折腾了。dockerrun-d\-p880:80-p8443:443-p25:25-p110:110-p143:143-p465:465-p587:587-p993:993-p995:995-p4190:4190\-eTZ=Asia/Shanghai\-v/data/mail-data:/data\--name"mail......
  • 聊聊Spring扩展点BeanPostProcessor和BeanFactoryPostProcessor
    介绍今天聊一聊spring中很重要的两个扩展点BeanPostProcessor和BeanFactoryPostProcessor,spring之所以如次强大,是因为它提供了丰富的功能给我们使用,但是我觉得最强大的是它扩展点,因为有了各种扩展点,我们才能去开发一些自己的需求,一个框架的强大之处也在于它能否灵活的配置,能够支......
  • elementUI 表单input输入框限制整数和小数长度
    <el-inputv-model="input1"placeholder="请输入内容"@keyup.native="input1=limitControlLine(input1,5,2)"></el-input>limitControlLine(val,zs,xs){letvalue=val;if(isNaN(val)){value=String(val).replace(/[......
  • 百度普通收录API提交token推送在线版+随机URL生成+抓取内页URL工具
    普通收录:百度站长平台官方给出的使用说明如下1、普通收录工具可以向百度搜索主动推送资源,缩短爬虫发现网站链接的时间,不保证收录和展现效果。2、API提交和手动提交共享配额......