java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。本站提供基于Java框架struts,spring,hibernate等的桌面应用、web交互及移动终端的开发技巧与资料

保持永久学习的心态,将成就一个优秀的你,来 继续搞起java知识。

获取请求头内容和数据的方法:

1import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import zgq.User;

public class RequestDemo2 extends HttpServlet {
    //获取请求数据(获取请求数据时一般来说都要先检查再使用)
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("---------获取数据方式1---------------");

        String value = request.getParameter("username");
        if (value != null && !value.trim().equals("")) {
            System.out.println(value);
        }

        System.out.println("---------获取数据方式2---------------");

        Enumeration e = request.getParameterNames();
        while (e.hasMoreElements()) {
            String name = (String) e.nextElement(); // username passwrod
            value = request.getParameter(name);
            System.out.println(name + "=" + value);
        }

        System.out.println("----------获取数据方式3--------------");

        String[] values = request.getParameterValues("username");
        for (int i = 0; values != null && i < values.length; i++) {
            System.out.println(values[i]);
        }

        System.out.println("----------获取数据方式4--------------");

        Map<String, String[]> map = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user, map); // username String[] //用map集合数据填充bean
            // BeanUtils.copyProperties(user, formbean) bean的拷贝
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        System.out.println(user);

        System.out.println("----------获取数据方式5--------------");

        InputStream in = request.getInputStream();
        int len = 0;
        byte buffer[] = new byte[1024];
        while ((len = in.read(buffer)) > 0) {
            System.out.println(len);
            System.out.println(new String(buffer, 0, len));
        }

        test1(request);
    }

    // 获取头相关的方法
    private void test1(HttpServletRequest request) {
        String headValue = request.getHeader("Accept-Encoding");
        System.out.println(headValue);

        System.out.println("------------------------");

        Enumeration e = request.getHeaders("Accept-Encoding");
        while (e.hasMoreElements()) {
            String value = (String) e.nextElement();
            System.out.println(value);
        }

        System.out.println("------------------------");

        e = request.getHeaderNames();
        while (e.hasMoreElements()) {
            String name = (String) e.nextElement();
            String value = request.getHeader(name);
            System.out.println(name + "=" + value);
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

解决乱码问题:

1import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestDemo4 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //test3(request, response);

        //test2(request);

        test1(request);

    }

    // 解决get提交的乱码(手工处理)
    private void test1(HttpServletRequest request)
            throws UnsupportedEncodingException {
        String username = request.getParameter("username");
        username = new String(username.getBytes("iso8859-1"), "UTF-8");
        System.out.println(username);
    }

    // 解决post提交的乱码
    private void test2(HttpServletRequest request)
            throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8"); // 只对post提交有效
        String username = request.getParameter("username");
        System.out.println(username);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

java

因为水平有限,难免有疏忽或者不准确的地方,希望大家能够直接指出来,我会及时改正。一切为了知识的分享。

后续会有更多的精彩的内容分享给大家。