所以现在我有将显示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;
}
我的问题是,给定URL中的值,我如何获得除“
item
”以外的其他内容?如何获得desc
或price
? 最佳答案
尝试这个。您需要直接从请求中提取参数名称和值,而不是从会话中提取属性。
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;
}
关于java - 如何获得特定的请求属性?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5768178/