在jsp页面如何获得url参数

当一个url过来时,如:http://localhost:8080/pro/demo/hello.jsp?name=john,在hello.jsp页面,我们可以这样得到name的值:

1 <% 
2 String path = request.getContextPath(); 
3 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
4 String name = request.getParameter("name");//用request得到 
5 %> 
View Code

然后在<body>hello:<%=name%></body>中显示。

也可以在body中直接用${}得到,因为当使用jstl时,url请求参数被放置到隐含对象param中。所以可以这样写: 

1 <body>hello:${param.name}</body> 
2 依据此逻辑,在使用jquery时,也可以用同样的方法得到,如: 
3 $(function(){ 
4 alert(${param.name}); 
5 }); 
View Code

 

posted @ 2016-01-04 18:42  flay  阅读(447)  评论(0编辑  收藏  举报