index.htm?参数1=数值1&参数2=数值2&参数3=数据3&参数4=数值4&......
静态html文件js读取url参数,根据获取html的参数值控制html页面输出。
一、字符串分割分析法。
这里是一个获取URL带REQUESTRING参数的JAVASCRIPT客户端解决方案。
相当于asp的request.querystring,PHP的$_GET,jsp的request.getParameter
//var USERCODE="<%=request.getParameter("USERCODE")%>";
//以上是用request.getParameter在浏览器端获取参数值, 也可以是用该方法在服务器端获取参数方法
函数:
<Script language="javascript">
function GetRequest() {
var url = location.search; //获取url中含"?"符后的字串
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for(var i = 0; i < strs.length; i ++) {
theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}
</Script>
然后我们通过调用此函数获取对应参数值:
<Script language="javascript">
var Request = new Object();
Request = GetRequest();
var 参数1,参数2,参数3,参数N;
参数1 = Request['参数1'];
参数2 = Request['参数2'];
参数3 = Request['参数3'];
参数N = Request['参数N'];
</Script>
以此获取url串中所带的同名参数
二、正则分析法。
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
var r = window.location.search.substr(1).match(reg);
if (r!=null) return unescape(r[2]); return null;
}
alert(GetQueryString("参数名1"));
alert(GetQueryString("参数名2"));
alert(GetQueryString("参数名3"));
三、java举例如下:
1、
<%@ page language="java" import="java.util.*,com.servlet.bean" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<script type="text/javascript">
<%
ArrayList list = (ArrayList)request.getAttribute("list");
for(int i=0;i<list.size();i++){
bean Bean = (bean)list.get(i);
%>
var name = '<%=Bean.getName()%>' //这里是从request获取参数,赋值给name这个变量
<%
}
%>
</script>
</body>
</html>
2、在servlet服务端的代码如下:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class servlet extends HttpServlet {
public servlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
bean Bean = new bean();
Bean.setName("liubin");
Bean.setPwd("123");
bean Bean2 = new bean();
Bean2.setName("aaa");
Bean2.setPwd("456");
bean Bean3 = new bean();
Bean3.setName("bbb");
Bean3.setPwd("789");
ArrayList list = new ArrayList();
list.add(Bean);
list.add(Bean2);
list.add(Bean3);
request.setAttribute("list", list);
request.getRequestDispatcher("../index.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request ,response);
}
public void init() throws ServletException {
// Put your code here
}
}
四、
javascript可以通过window.navigator来进行判断。不同的浏览器navigator对象里的名称是不同的:
window.navigator.userAgent 记录浏览器信息以及操作系统信息。
1.取变量值
var a = '<%=request.getAttribute("aaa");%>' ;
2.也可以将这个值放在页面上.再取出来.
<input type="hidden" value="<%=request.getAttribute("aaa");%>" id="aaa"/>
var a = document.getElementById('aaa').value ;
(注):对于对象,我推荐使用第二种方法.这样可以在页面中不IMPORT JAVA类..更符合现在编程
eg:
<input type="hidden" value="${student.name}" id="stuName"/>
var stuName = $('stuName').value ; // prototype.js新功能,简写.