所以现在我的代码将显示来自url:
URL:
http://localhost:9080/MyWebApp/MyServlet?qty=1&item=100&desc=CD+ROMS&price=9.99&action=add&addToCart=Add+to+cart
显示的内容:
Recent Queries
Item_100
以下是显示该代码的代码:
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;
}
我的问题是,鉴于网址中的值,我如何得到“item
”之外的其他内容?我如何获得desc
或price
?
答案 0 :(得分:1)
试试这个。您需要直接从请求中提取参数名称和值,而不是会话中的属性。
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;
}