当前位置 : 主页 > 编程语言 > c++ >

java web 获取表单提交

来源:互联网 收集:自由互联 发布时间:2021-07-03
gistfile1.txt getParameter(String name):通过key,返回一个value。getParameterValues(String name):通过key返回一个string数组(多个值)getParameterNames():返回form表单中的所有key值。//代码@WebServlet({ "/FormSer
gistfile1.txt
getParameter(String name):通过key,返回一个value。
getParameterValues(String name):通过key返回一个string数组(多个值)
getParameterNames():返回form表单中的所有key值。

//代码
@WebServlet({ "/FormServlet", "/form" })
public class FormServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;//序列化

    public FormServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/json;charset=utf-8");

        PrintWriter out = response.getWriter();
         //获取表单提交name 属性集合
        Enumeration
 
    paramNames =  request.getParameterNames();

        while (paramNames.hasMoreElements()) {
            String  name = paramNames.nextElement();

            String[] values =  request.getParameterValues(name);

            if(values!=null && values.length>0){
                StringBuilder builder = new StringBuilder(); 
                for (int i = 0; i < values.length; i++) {
                    builder.append(values[i]+" ");
                }
                out.println(name+" : "+builder.toString());
            }
        }
    }

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

}
 
上一篇:maven 安装jar
下一篇:okhttp日志拦截
网友评论