您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
03-Request通用方式获取请求参数(servlet在get请求与post请求下中文乱码的彻底解决-多种方案)
发布时间:2024-11-28 15:21:12编辑:雪饮阅读()
-
摘要:servlet在get请求与post请求下中文参数值于maven的apache插件定义pom时乱码于IntelliJ IDEA控制台的彻底解决
我们来解决一个8080占用问题。
使用maven运行tomcat于IntelliJ IDEA中在控制台有报错如
java.net.BindException: Address already in use: JVM_Bind <null>:8080
但是web项目能够打开,至少静态页面可以打开,就很神奇。只是servlet不能访问。
会变成404.
C:\Users\1>netstat -ano | findstr :8080
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 26744
TCP 192.168.8.100:56199 157.148.54.28:8080 ESTABLISHED 22292
TCP [::]:8080 [::]:0 LISTENING 26744
通过netstat排查到有几个pid都占用到这个8080了,那么挨个排查,等排查到22292的时候发现。
C:\Users\1>tasklist /fi "PID eq 22292"
映像名称 PID 会话名 会话# 内存使用
========================= ======== ================ =========== ============
QQ.exe 22292 Console 3 145,548 K
竟然是qq。。。
那么所以你要在你的maven的tomcat插件配置中手动指定下默认端口如
<configuration>
<port>8081</port>
</configuration>
当然还有一种情况就是你发现占用的进程也是java。。。,这个时候你要看下你的IntelliJ IDEA中是否有多个当前项目的tomcat在底部的Run哪里。。。
接下来我们需要两个用于提交的表单,一个get提交,一个post提交,如index4.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="display: flex;justify-content: space-between;">
<form action="http://localhost:8081/servlet01/HttpServlet10" method="get" style="width: 50%;">
<h1>GET</h1>
<div>
<label>username</label>
<input name="username" value="小明"/>
</div>
<div>
<label>password</label>
<input name="password" value="123456"/>
</div>
<div>
<label>hobby</label>
<input type="checkbox" name="hobby" value="1" checked /> swim
<input type="checkbox" name="hobby" value="2" checked /> climb mountain
</div>
<div>
<input type="submit" value="submit"/>
</div>
</form>
<form action="http://localhost:8081/servlet01/HttpServlet10" method="post" style="width: 50%;">
<h1>POST</h1>
<div>
<label>username</label>
<input name="username" value="小李"/>
</div>
<div>
<label>password</label>
<input name="password" value="6788"/>
</div>
<div>
<label>hobby</label>
<input type="checkbox" name="hobby" value="1" checked /> swim
<input type="checkbox" name="hobby" value="2" checked /> climb mountain
</div>
<div>
<input type="submit" value="submit"/>
</div>
</form>
</div>
</body>
</html>
那么这里为什么要用两个表单一个post一个get呢?为了演示doGet与doPost方法公共的业务的一些封装。因为有时候有些地方的业务逻辑是相同的,比如你get或post,在这里其实只是一个参数在请求行里面写的,一个参数是在请求体里面写的。
而这在php那边好像差不了多少,但在java这边就是有些不同了。
尤其是编码这里最为明显。
那么以上的代码,只是前端代码于我们的webapp中,我们可以访问如
http://localhost:8081/servlet01/index4.html
那么实际上上面的代码如果在servlet中特别是中文参数值的时候可能就会出现intelliJ IDEA的控制台中是乱码输出的。
你可能也看到了我们的这个前端页面文档编码是UTF-8。那么在get请求要使得servlet的request对象被提取参数值的时候以正确的中文进行提取,则需要在pom.xml中tomcat插件配置编码如:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8081</port>
<!--
get请求时候,如果url中有包含中文参数,是真正的中文,不是url编码的中文的时候可以用来解决乱码问题于IntelliJ IDEA中的控制台中对其的输出乱码。
-->
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
而对于post请求我们需要用HttpServletRequest的setCharacterEncoding来设置编码了。
所以接下来我的servlet的实现类如:
package com;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
@WebServlet("/HttpServlet10")
public class HttpServlet10 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("---------get--------");
System.out.println("---------遍历所有参数--------");
Map<String,String[]> map=req.getParameterMap();
for(String key:map.keySet()){
System.out.print(key+":");
String[] values=map.get(key);
for(String value:values){
System.out.print(value+" ");
}
System.out.println();
}
System.out.println("---------获取数组类型参数--------");
//当你已经知道你某个参数的值是数组的时候可以使用该方法直接获取该参数的数组值
String[] hobbies=req.getParameterValues("hobby");
for(String hobby:hobbies){
System.out.println(hobby);
}
System.out.println("---------获取单值的输入参数--------");
String username=req.getParameter("username");
String password=req.getParameter("password");
System.out.println("username:"+username);
System.out.println("password:"+password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("---------get--------");
req.setCharacterEncoding("UTF-8");
doGet(req,resp);
}
}
这样一来就封装好了不同编码但相同的参数提取逻辑了。不用doPost的时候还要再重复读取参数了。
那么如果你的IntelliJ IDEA还是出现乱码的情况,你可以尝试如:
(1)C:\Users\1\AppData\Roaming\JetBrains\IntelliJIdea2020.3\idea64.exe.vmoptions文件中新增如
-Dfile.encoding=UTF-8
-Dconsole.encoding=UTF-8
该路径也即Help=>Edit Custom VM Options…
或如C:\Program Files\JetBrains\IntelliJ IDEA 2020.3.3\bin\idea64.exe.vmoptions
或如C:\Program Files\JetBrains\IntelliJ IDEA 2020.3.3\bin\idea.exe.vmoptions
这两个路径同上面的这个一样的配置
(2)或路径D:\os\servlet01\target\tomcat\conf\ logging.properties中新增配置如
1catalina.org.apache.juli.AsyncFileHandler.encoding = UTF-8
2localhost.org.apache.juli.AsyncFileHandler.encoding = UTF-8
3manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8
4host-manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8
java.util.logging.ConsoleHandler.encoding = UTF-8
一份局部完整实例如:
1catalina.org.apache.juli.FileHandler.level = FINE
1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.FileHandler.prefix = catalina.
1catalina.org.apache.juli.AsyncFileHandler.encoding = UTF-8
2localhost.org.apache.juli.FileHandler.level = FINE
2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.FileHandler.prefix = localhost.
2localhost.org.apache.juli.AsyncFileHandler.encoding = UTF-8
3manager.org.apache.juli.FileHandler.level = FINE
3manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
3manager.org.apache.juli.FileHandler.prefix = manager.
3manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8
4host-manager.org.apache.juli.FileHandler.level = FINE
4host-manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
4host-manager.org.apache.juli.FileHandler.prefix = host-manager.
4host-manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8
java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.encoding = UTF-8
而这个路径可以从你的maven的tomcat插件在控制台中的提示找到,如控制台输出中有包含信息如:
[INFO] Using existing Tomcat server configuration at D:\os\servlet01\target\tomcat
或路径如D:\software\apache-tomcat-8.5.68-windows-x64\apache-tomcat-8.5.68\conf\ logging.properties,这个路径是你非tomcat的maven插件的那个手动安装的tomcat安装路径里面的。配置也是同上面的这个logging.properties一样。
(3)那还有在Intellij IDEA中Settings=>Build,Excution,Deployment=>Runner=>VM Options的值配置为-Dfile.encoding=UTF-8
那还有Settings=》Editor=》File Encodings中的Global Encoding和Project Encoding以及Default encoding for properties files都确保为UTF-8以及勾选Transparent native-to-ascii conversion
那还有Settings=》Editor=>General=>Console=>Default encoding确保为UTF-8
那还有一个是据说,Settings=>Appearance & Behavior=>Appearance这个里面的Use custom font里面要选择Microsoft YaHei Ui,这种支持中文的字体,但Use custom font是否要勾选,我是没有勾选,不过我确实是选择的Microsoft YaHei Ui,也是默认的吧。没有动过。
本期词汇
Appearance 外表,外观
Behavior 性能
Encoding [计]编码
关键字词:request,servlet,get,post,中文,乱码
相关文章
- 02-Request获取请求数据-请求行&请求头&请求体
- 01-Request和Response介绍&Request继承体系(获取get请
- 16-XML配置Servlet(路由)
- 15-urlPattern配置(WebServlet注解配置各种url访问路
- 14-Servlet方法介绍
- 13-Servlet方法介绍&体系结构(HttpServlet的使用)
- 12-Servlet执行流程&生命周期(init、service、destroy
- 11-Servlet简介&快速入门
- 05-Tomcat-简介&基本使用(安装及启、停、控制台中文乱
- 10-JDBC练习-环境准备(IntelliJ IDEA整列编辑、Getter