Getting a Request Parameter in a Servlet - Java Servlet JSP

Java examples for Servlet JSP:Servlet

Description

Getting a Request Parameter in a Servlet


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

Related Tutorials