localhost/user/user123和localhost/user?user=user123和有什么区别localhost/?user=user123?
如何从servlet 中user123的 URL获取参数?localhost/user/user123
提前致谢
您可以从 HttpServletRequest 对象的 getPathInfo() 中解析。
示例代码
String urlPath = request.getPathInfo();
System.out.println("" + urlPath.substring(urlPath.lastIndexOf("/"), urlPath.length()- 1));
localhost/user/user123 看起来像是一种识别资源的 RESTful 方式。
其他两个不是,我想。
这些都可以从Servlet API访问。检查HttpServletRequest,您可以从那里访问所有信息。
实际值可能与您的 web 应用程序的部署方式不同,但通常
localhost是上下文路径?是查询字符串 - 如果你想使用,你必须解析它localhost/user/user123- 这个网址将由模式处理/user/user123localhost/user?user=user123- 此 url 将由 pattern 处理/user,user参数设置为user123(用于 GET 请求)localhost/?user=user123- 此 url 将由参数设置为的模式/处理(同样,对于 GET)useruser123我不知道如何使用纯 servletuser123从 url中检索localhost/user/user123,但是使用 web MVC 框架很容易。弹簧示例:
@Controller
@RequestMapping("/user")
public class Controller {
@RequestMapping(value = "/{user}")
public String getUser((@PathVariable String user) {
//here variable "user" is available and set to "user123" in your case
}
}
通常,您传递参数,例如
/localhost/Servlet?parameter1=one
或者对于 JSP
/localhost/mypage.jsp?parameter1=one
在 servlet 中,您可以使用请求对象访问参数。所以一般是这样的:
String parameter1 = request.getParameter("parameter1");
这是有关HttpServletRequest的 getParameter 的一些详细信息
希望这可以帮助。