本文執行個體為大家分享了java表單提交中文亂碼的解決方案,供大家參考,具體內容如下
首頁index.xml
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html> <head> <title>servlet示範</title> </head> <body> <h2>中文亂碼</h2> <!-- /servletDemo_1/encode最好用絕對目錄,因為index.jsp位置可能會更改 --> <form action="/servletDemo_1/encode" method="post"> 姓名:<input type="text" name="name"/><br/> 密碼:<input type="password" name="pwd"/> <br/> <input type="submit" value="登陸"/> </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("編碼:"+charSet); } /* * 解決中文亂碼 * 1)更改tomcat平台中字元編碼(server.xml-connector的屬性) * 而tomcat是所有項目的公用平台,所以不要該,盡量不改 * 2)反查ISO8859-1編碼:通過String中的亂碼解決方式 * 3)擷取參數之前設定:req.setCharacterEncoding("utf-8"); * 注意:此種方式必須要是POST方式提交,否則不行 * 4)擷取參數之前設定:通過在web.xml中設定字元參數方式把3)做活 * 5)用過濾器把3)做活--以後實現 */ @Override public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {// //3)擷取參數之前設定// req.setCharacterEncoding("utf-8"); //4)擷取參數之前設定:通過在web.xml中設定字元參數方式把3)做活 req.setCharacterEncoding(charSet); //讀取資訊處理中文亂碼 String name=req.getParameter("name"); String pwd=req.getParameter("pwd"); System.out.println("name:"+name+",pwd:"+pwd);// System.out.println("編碼前-name:"+name+",pwd:"+pwd);// //2)反查ISO8859-1編碼:通過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("編碼後-name:"+name+",pwd:"+pwd);// //注:這種方式對於參數很多時,就不合適使用了 resp.setContentType("text/html;charset=utf-8");//設定協議:IE沒問題,但是有的瀏覽器不相容 //向用戶端應答 String str="<html><head><title></title></head><body><font color='red'>name:" +name+",pwd:"+pwd+"</font></body></head>"; resp.getWriter().println(str);//println()帶刷緩衝 }}
設定檔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>
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。