zoukankan      html  css  js  c++  java
  • servlet 提交【get post】

    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]);
    
            }
    
        }
    
    }
    View Code

     如果  get表单里面内容多  直接修改tomcat  URLEncoding="utf-8" 

  • 相关阅读:
    phpMyAdmin出现错误 Access denied for user 'root'@'localhost' (using password: NO)
    Android使用butterknife注解出现nullPointerException解决
    Fragment Touch事件泄露
    清空Fragment回退栈中某个Fragment之上的所有Fragment
    Fragment保持状态切换
    点击EditText可编辑,点击其他地方不可编辑
    android ActionBar 去掉menu分隔线
    jquery validation remote进行唯一性验证时只使用自定义参数,不使用默认参数
    php 闭包函数
    phpstorm快捷按键
  • 原文地址:https://www.cnblogs.com/xh0626/p/5515261.html
Copyright © 2011-2022 走看看