开发者

How can I get specific Request Attributes?

So right now I have code that will display the from the url:

URL:

http://localhost:9080/MyWebApp/MyServlet?qty=1&item=100&desc=CD+ROMS&price=9.99&action=add&addToCart=Add+to+cart

What is shown:

    Recent Queries
    Item_100

And here is the code that will display that:

public String getRecentQueries(HttpServletRequest request)
{
    String queries = "";
    HttpSession session = request.getSession(false);
       if (session != null)
       {
          Enumeration e = session.getAttributeNames();
          if ( e.hasMoreElements() )
          {
             queries += "<h4>Recent Queries</h4><ul>";
          }
          while ( e.hasMoreElements() )
          {
             String name = (String) e.nextElement();
             String value =
                (String) session.getAttribute(name);
             queries += "<li><a href=\"" + value + "\">" + 
                name + "</a></li>";
          }
          queries += "</ul></p>";
       }
       return queries;
}

My question is, given the values in the url, how do I get somet开发者_JS百科hing other than just 'item'? How would I get desc, or price?


Try this. You need to be pulling the parameter names and values directly from the request, not the attributes from the session.

public String getRecentQueries(HttpServletRequest request)
{
    String queries = "";
    Enumeration e = request.getParameterNames();

    if ( e.hasMoreElements() )
    {
        queries += "<h4>Recent Queries</h4><ul>";
    }

    while ( e.hasMoreElements() )
    {
        String name = (String) e.nextElement();
        String value = (String) request.getParameter(name);
        queries += "<li><a href=\"" + value + "\">" + name + "</a></li>";
    }
    queries += "</ul></p>";

    return queries;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜