如何获得jsp页面的url参数

本文主要介绍的是关于如何获得jsp页面的url参数的内容,相信很多小伙伴们都会好奇在jsp页面中如何获得url参数呢?现在就让中国E盟小编带领大家去看一下吧。

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

复制代码 代码如下:

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String name = request.getParameter("name");//用request得到
%>


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

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

复制代码 代码如下:


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

不知道大家是否已经学会了如何获得jsp页面的url参数的内容,学习这一个知识也是很有帮助的哟。