Jsp页面获得url参数的方式

来源:互联网 发布:初级会计考试软件 编辑:程序博客网 时间:2024/05/09 22:55

url为  http://localhost:8080/demo/hello.jsp?name=susan

1)java代码 request获取

<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; String name = request.getParameter("name");//用request得到 %>
在页面中显示该值

<html><body>hello:<%=name%></body></html>


2)使用jstl 方式

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
因为当使用jstl时,url请求参数被放置到隐含对象param中,所以可以用 ${name} 或者${param.name}直接获取到。

比如页面中:

<html><body>hello:${param.name}</body></html>


js中:

$(function(){ alert(${param.name}); }); 

3)js代码取得

function getUrlPara1(strName){var strHref = document.location.href;var intPos = strHref.indexOf("?");var strRight = strHref.substr(intPos + 1);var arrTmp = strRight.split("&");for(var i = 0; i < arrTmp.length; i++ ) {var arrTemp = arrTmp[i].split("=");if(arrTemp[0].toUpperCase() == strName.toUpperCase()) return arrTemp[1];}return 0;}


使用正则表达式的写法:

function getUrlPara(strName){     var reg = new RegExp("(^|&)"+strName+"=([^&]*)(&|$)","i"); var r = decodeURI(window.location.search).substr(1).match(reg); if(r!=null) return (r[2]); return null; }


调用

var testUrl=getUrlPara("name");







0 0