This article has shared with you the solution to submit Chinese garbled code in Java form for your reference. The specific content is as follows
Home index.xml
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html> <head> <title>servlet demonstration</title> </head> <body> <h2>Chinese garbled</h2> <!-- /servletDemo_1/encode is best used in absolute directories, because the index.jsp location may change --> <form action="/servletDemo_1/encode" method="post"> Name:<input type="text" name="name"/><br/> Password:<input type="password" name="pwd"/> <br/> <input type="submit" value="Login"/> </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("Encoding:"+charSet); } /* * Solve Chinese garbled* 1) Change the character encoding in the tomcat platform (the property of server.xml-connector) * And tomcat is the public platform of all projects, so don't do it, try not to change* 2) Back-check ISO8859-1 encoding: solve the problem through the garbled code in String * 3) Set before getting the parameters: req.setCharacterEncoding("utf-8"); * Note: This method must be submitted in POST mode, otherwise it will not work * 4) Set before getting the parameters: 3) do it by setting character parameters in web.xml * 5) Use filters to do it - in the future */ @Override public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {// //3) Set before getting the parameters // req.setCharacterEncoding("utf-8"); //4) Set before getting parameters: 3) Req.setCharacterEncoding(charSet); //Read information processing Chinese garbled 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) Back-check ISO8859-1 encoding: Solve the garbled method in String// 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 encoding-name:"+name+",pwd:"+pwd);// // Note: This method is not suitable for many parameters. Resp.setContentType("text/html;charset=utf-8");// Set protocol: IE is no problem, but some browsers are incompatible//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() with flash 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 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>
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.