var str="variable";
}
这是一个javascript函数,我想在JSP里访问这个函数里的变量str,不知可行不可行,请高手指点指点
这个示例代码我发过一次了,再给你看看吧
autorefresh.jsp,调用这个页面看效果的
[CODE]
<%@ page contentType="text/html;charset=gb2312" language="java" %>
<head>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
</head>
<script language="javascript">
var XMLHttpReq;
//创建XMLHttpRequest对象
function createXMLHttpRequest() {
if(window.XMLHttpRequest) { //Mozilla 浏览器
XMLHttpReq = new XMLHttpRequest();
}
else if (window.ActiveXObject) { // IE浏览器
try {
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
}
//发送请求函数
function sendRequest() {
createXMLHttpRequest();
var url = "auto.jsp";
XMLHttpReq.open("GET", url, true);
XMLHttpReq.onreadystatechange = processResponse;//指定响应函数
XMLHttpReq.send(null); // 发送请求
}
// 处理返回信息函数
function processResponse() {
if (XMLHttpReq.readyState == 4) { // 判断对象状态
if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息
DisplayHot();
setTimeout("sendRequest()", 1000);
} else { //页面不正常
window.alert("您所请求的页面有异常。");
}
}
}
function DisplayHot() {
var name = XMLHttpReq.responseXML.getElementsByTagName("time")[0].firstChild.nodeValue;
document.getElementById("time").innerHTML = name;
}
</script>
</SCRIPT>
<body onload =sendRequest()>
<table style="BORDER-COLLAPSE: collapse" borderColor=#111111
cellSpacing=0 cellPadding=0 width=100% bgColor=#f5efe7 border=0>
<tr>
<td height="20" id="time">
</td>
</tr>
</body>
</table>
[/CODE]
auto.jsp
[CODE]
<%@ page contentType="text/html; charset=gb2312" %>
<%
//设置输出信息的格式及字符集
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control","no-cache");
out.println("<response>");
String pattern = "今天是 yyyy 年 MM 月 dd 日 EEE,现在时间是 a hh 点 mm 分 ss 秒";
String mytime=new java.text.SimpleDateFormat(pattern).format(new java.util.Date());
for(int i=0;i<2;i++){
out.println("<time>"+mytime+"</time>");
}
out.println("</response>");
out.close();
%>
[/CODE]为什么是昨天,我早就根你说过,建议你学习AJAX.
...
刚搞了个例子,如下:
JSP页面:
[CODE]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Ajax Test</title>
<script>
var XMLHttpReq;
//创建XMLHttpRequest对象
function createXMLHttpRequest() {
if(window.XMLHttpRequest) { //Mozilla 浏览器
XMLHttpReq = new XMLHttpRequest();
}
else if (window.ActiveXObject) { // IE浏览器
try {
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
}
function onreadystatechange() {
if (XMLHttpReq.readyState == 4) { // 判断对象状态
if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息
//var xmldoc = XMLHttpReq.responseXML;
//var root_node = xmldoc.getElementsByTagName('name').item(0);
//document.getElementById("showMsg").innerText = root_node.firstChild.data;
document.getElementById("showMsg").innerHTML = XMLHttpReq.responseText;
} else { //页面不正常
//window.alert("您所请求的页面有异常。");
}
}
}
function checkUser() {
var userName = document.getElementById("userName").value;
var url = "ajax?userName="+userName;
createXMLHttpRequest();
XMLHttpReq.onreadystatechange = onreadystatechange;
XMLHttpReq.open("GET",url,true);
XMLHttpReq.send(null);
}
</script>
</head>
<body>
<form>
<p><input id="userName" type="text" /></p>
<p><input type="button" value="ShowFileName" onclick="checkUser();"></p>
</form>
<div id="showMsg">
</div>
</body>
</html>
[/CODE]
***********************************************************************************
Servlet代码:
[CODE]
package com.service;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Service extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{
String userName=request.getParameter("userName");
PrintWriter out=response.getWriter();
response.setContentType("text/xml; charset=gb2312");
response.setHeader("Cache-Control", "no-cache");
if(userName.equals("flash")){
out.println("<font color=#FF0000>OK</font>");
}else{
out.println("<font color=#0000FF>False</font>");
}
out.close();
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{
doGet(request,response);
}
}
[/CODE]