前端页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="container">
<form action="" id="form1" method="get">
<label for="name">Name</label>
<input type="text" name="name" placeholder="name" value="name">
<br>
<label for="sex">sex</label>
<input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女">女
<br>
<label for="addr">Address</label>
<input type="text" name="addr" placeholder="addr">
<input type="button" value="submit" onclick="tijiao()">
</form>
</div>
<script>
function tijiao(){
var fd=new FormData(document.getElementById("form1"));
fetch('/change/yu?'+encodeURI(Array.from
(fd.entries()).map(el=>el.join("=")).join("&"))
).then(function (aaa){return
aaa.text();}).then(function(bb){console.log(bb);});
}
</script>
</body>
</html>
部署到tomacat中:
在文件D:\apache-tomcat-7.0.92\conf\Catalina\localhost\yu.xml(
也就是配置tomacat的xml)
代码如下
<Context docBase="C://Users/mcfeng/Desktop/html"></Context>
复制html上一层的目录;
启动服务器:
访问地址:
localhost:8080/yu(配置xml的名字)/demo.html(前端名字);
提交表单后
fetch: 跳转到localhost:8080/change/yu(tomacat-path/url-pattern);
解决编码(有点复杂):
只有初步的解决方案:
String a=request.getParameter("name");
System.out.println(new String(a.getBytes("ISO-8859-1"),"utf-8"));
有点麻烦;