Java 怎么在servlet中获取请求参数

2019年8月16日22:06:59Java 怎么在servlet中获取请求参数已关闭评论 225 797字阅读2分39秒

1.简介

Java 怎么在servlet中获取请求参数?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    doGetOrPost(req, resp);
}

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    doGetOrPost(req, resp);
}

private void doGetOrPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String name = "param";
    String value = req.getParameter(name);
    if (value == null) {
    } else if ("".equals(value)) {
    }
    PrintWriter out = resp.getWriter();
    resp.setContentType("text/plain");
    Enumeration enum = req.getParameterNames();
    for (; enum.hasMoreElements(); ) {
        name = (String)enum.nextElement();
        out.println(name);
        value = req.getParameter(name);
        String[] values = req.getParameterValues(name);
        for (int i=0; i<values.length; i++) {
            out.println("    "+values[i]);
        }
    }
    out.close();
}

 
编程十万个怎么办