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

2019年8月16日22:06:59 评论 24

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();
}

 

3.总结

示例代码演示了 Java 怎么在servlet中获取请求参数,具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: