servlet 提交【get post】
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class charset extends HttpServlet { /** * Constructor of the object. */ public charset() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // String str = request.getParameter("xh"); // String str2 = new String(str.getBytes("iso-8859-1"), "utf-8"); // System.out.println(str); // System.out.println(str2); //这句话是告诉浏览器 怎么去按照编码解析 // response.setHeader("Content-type", "text/html;charset=UTF-8"); // // response.getOutputStream().write(str2.getBytes("utf-8")); // response.getWriter().write(str2); //告诉servlet 按照编码解析 //response.setCharacterEncoding("utf-8"); gettj(request, response);//方法2 } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String str1 = request.getParameter("xh"); response.setContentType("text/html;charset=utf-8"); response.getWriter().write(str1); } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occurs */ public void init() throws ServletException { // Put your code here } public static void gettj(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Content-type", "text/html;charset=utf-8"); String str1 = request.getQueryString(); String str2 = java.net.URLDecoder.decode(str1, "utf-8"); String[] arr = str2.split("&"); for (String str : arr) { String[] str3 = str.split("="); response.getWriter().write(str3[1]); } } }
如果 get表单里面内容多 直接修改tomcat URLEncoding="utf-8"