如何获得写的链接值? - JSP

时间:2012-02-10 06:21:08

标签: jsp java-ee parameters request

我想从用户编写的链接中获取值。 例如,我想转到页面pageone.jsp?x=abc,然后如何获取变量“x”(用url写的?x = abc,然后是prinln(x)?

1 个答案:

答案 0 :(得分:0)

使用request.getParameterNames()

 Enumeration paramNames = request.getParameterNames();
    while(paramNames.hasMoreElements()) {
      String paramName = (String)paramNames.nextElement();
      out.println("<TR><TD>" + paramName + "\n<TD>");
      String[] paramValues = request.getParameterValues(paramName);
      if (paramValues.length == 1) {
        String paramValue = paramValues[0];
        if (paramValue.length() == 0)
          out.print("<I>No Value</I>");
        else
          out.print(paramValue);
      } else {
        out.println("<UL>");
        for(int i=0; i<paramValues.length; i++) {
          out.println("<LI>" + paramValues[i]);
        }
        out.println("</UL>");
      }
    }

refr http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html