java form submission Chinese garbled code solution
- 2020-05-10 18:06:26
- OfStack
In this paper, the example of java form submission for you to share the Chinese messy code solution, for your reference, the specific content is as follows
Home page index xml
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>servlet demo </title>
</head>
<body>
<h2> Chinese garbled </h2>
<!-- /servletDemo_1/encode You'd better use the absolute directory , because index.jsp The location may change -->
<form action="/servletDemo_1/encode" method="post">
The name :<input type="text" name="name"/><br/>
password :<input type="password" name="pwd"/> <br/>
<input type="submit" value=" landing "/>
</form>
</body>
</html>
ServletEncoding.java
package cn.hncu.servlet_2;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class ServletEncoding implements Servlet {
private String charSet=null;
@Override
public void destroy() {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void init(ServletConfig config) throws ServletException {
charSet=config.getInitParameter("char");
System.out.println(" coding :"+charSet);
}
/*
* Solve Chinese garbled code
* 1) To change the tomcat Character encoding in the platform (server.xml-connector The properties of the )
* while tomcat It is a common platform for all projects , So don't , Try not to change
* 2) The check ISO8859-1 coding : through String In the garbled code solution
* 3) Gets the parameter Settings before :req.setCharacterEncoding("utf-8");
* Pay attention to : This way must be POST Submission of , Otherwise I can't
* 4) Gets the parameter Settings before : Through the web.xml Set the character parameter way to 3) Do work
* 5) Use a strainer 3) Do work -- After the implementation
*/
@Override
public void service(ServletRequest req, ServletResponse resp)
throws ServletException, IOException {
// //3) Gets the parameter Settings before
// req.setCharacterEncoding("utf-8");
//4) Gets the parameter Settings before : Through the web.xml Set the character parameter way to 3) Do work
req.setCharacterEncoding(charSet);
// Read information processing Chinese garbled code
String name=req.getParameter("name");
String pwd=req.getParameter("pwd");
System.out.println("name:"+name+",pwd:"+pwd);
// System.out.println(" Before encoding -name:"+name+",pwd:"+pwd);
// //2) The check ISO8859-1 coding : through String In the garbled code solution
// byte bs[]=name.getBytes("iso-8859-1");
// name=new String(bs,"utf-8");
// byte bs2[]=pwd.getBytes("iso-8859-1");
// pwd=new String(bs2,"utf-8");
// System.out.println(" After the coding -name:"+name+",pwd:"+pwd);
// // note : This works for a lot of arguments , It's not appropriate to use
resp.setContentType("text/html;charset=utf-8");// The protocol :IE no problem , But some browsers are not compatible
// Reply to the client
String str="<html><head><title></title></head><body><font color='red'>name:"
+name+",pwd:"+pwd+"</font></body></head>";
resp.getWriter().println(str);//println() Brush the cache
}
}
Configuration file web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<servlet>
<servlet-name>encode</servlet-name>
<servlet-class>cn.hncu.servlet_2.ServletEncoding</servlet-class>
<init-param>
<param-name>char</param-name>
<param-value>utf-8</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>encode</servlet-name>
<url-pattern>/encode</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>