requeset.getAttribute()可以获取url参数吗?

解决方案 »

  1.   

    可以的。一般用 request.getContextPath() 来获取基URl。
      

  2.   

    哥们,能说具体点吗?
    request.getContextPath() 来获取基URL。
    之后怎么用getAttribute()
      

  3.   

    楼主啥意思?获取参数用getParameter不就好啦
      

  4.   

    举个例子:
    假定你的web application 名称为news,你在浏览器中输入请求路径:http://localhost:8080/news/main/list.jsp
    则执行下面向行代码后打印出如下结果:1、 System.out.println(request.getContextPath());打印结果:/news
    2、System.out.println(request.getServletPath());打印结果:/main/list.jsp
    3、 System.out.println(request.getRequestURI());打印结果:/news/main/list.jsp
    4、System.out.println(request.getRequestURL());打印结果:http://localhost:8080/news/main/list.jsp
    5、 System.out.println(request.getRealPath("/"));打印结果:F:\Tomcat 6.0\webapps\news\test
      

  5.   

    楼上的说的很详细了。request.setAttribute()和request.getAttribute是对应的。
    楼主可以试试
      

  6.   

    谢谢你的回答。我之所以这么问,是有一道笔试题是这么问的,平时都用的getParameter.
      

  7.   

    获得参数
    String params = "";
    int i = 0;
    Enumeration names = httpRequest.getParameterNames();
    if (names != null) {
    while (names.hasMoreElements()) {
    String name = (String) names.nextElement();
    if (i > 0) {
    params = params + "&";
    }
    i++;
    String value = httpRequest.getParameter(name);
    if (value == null) {
    value = "";
    }
    params = params + name + "=" + value;
    }
    }