<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="checkLogin.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>成功登陆</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<script language="JavaScript">
function stop()
{
<% session.setAttribute("formAction", "stop"); %>
form.action = "agentsTracker";
form.submit();
}
function start()
{
<% session.setAttribute("formAction", "start"); %>
form.action = "agentsTracker";
form.submit();
}
function tmp()
{
<% session.setAttribute("formAction", "tmp"); %>
}
</script>
<body>
<h1>欢迎您</h1>
${sessionScope.name}
<form name="form" id="form" method="post" action="agentsTracker">
<input type="button" name="startBtn" onClick="start()" value="start">
<input type="button" name="stopBtn" onClick="stop()" value="stop">
<input type="submit" name="submit" value="ok">
</form>
</body>
</html>
不管点击哪个按钮,servlet接收到的session.getAttribute("formAction")都是"tmp",刚接触jsp,不知道是哪出错了
<%@ include file="checkLogin.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>成功登陆</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<script language="JavaScript">
function stop()
{
<% session.setAttribute("formAction", "stop"); %>
form.action = "agentsTracker";
form.submit();
}
function start()
{
<% session.setAttribute("formAction", "start"); %>
form.action = "agentsTracker";
form.submit();
}
function tmp()
{
<% session.setAttribute("formAction", "tmp"); %>
}
</script>
<body>
<h1>欢迎您</h1>
${sessionScope.name}
<form name="form" id="form" method="post" action="agentsTracker">
<input type="button" name="startBtn" onClick="start()" value="start">
<input type="button" name="stopBtn" onClick="stop()" value="stop">
<input type="submit" name="submit" value="ok">
</form>
</body>
</html>
不管点击哪个按钮,servlet接收到的session.getAttribute("formAction")都是"tmp",刚接触jsp,不知道是哪出错了
解决方案 »
- oracle11g Hibernate如何映射enum枚举类型
- jsp登录界面代码??
- 求助:java中如何使用poi创建、编辑word文档!!! (急....@@@)
- 急!为什么总是报错''附近有语法错误
- jsp怎么都连不上sql2000
- jsp如何控制目录访问
- 有for eclipse3.0.1的vss插件吗?
- 关于tomcat5使用javaben的问题
- 使用jquery datatable,怎么在表格前加checkbox有全选之类的功能呢?
- HTTP method POST is not supported by this URL
- 请问怎么在ssh实现 展现层分页显示数据的list呢
- 高手请进,这里有个简单的问题,但我完全没头绪
function tmp()
{
<% session.setAttribute("formAction", "tmp"); %>
}
因为值是保存在session里边,所以你setAttribute进去的东西,它会在你没有重启浏览器或者服务的情况下存在,再者因为你start()和stop()里边的 session.setAttribute("formAction", "stop");和 session.setAttribute("formAction", "start");key都为formAction和<% session.setAttribute("formAction", "tmp"); %>中重复了,setAttribute(key,value);中的key不应该有重复的,你检查下这两方面
不杂乱吧,我就加了一个表单和三个函数,最后一个函数tmp()是为了证实是不是在网页加载时就执行了,或者是点击按钮的时候,三个函数一起执行了
你明我意思吧!
{
<% session.setAttribute("formAction", "stop"); %>
form.action = "agentsTracker";
form.submit();
}
红色的代码是服务端执行的,也就是你这个页面加载的时候就已经执行了,楼主可以访问一下这个JSP之后,在浏览器中看一下页面源代码,应该是看不到session.setAtt……的,因为它是服务端执行的东西,其他几个function中也是一样的,所以最后的session.setAttribute("formAction", "tmp");就覆盖了以前设置的formAction。不知道我有没有说明白。总之楼主需要明白什么代码是客户端执行的,什么代码是客户端执行的就OK了。
这行是jsp中使用的java代码,和js函数无关,会默认执行的。如果你需要向后台传值不应该使用这种方法。
楼主action跳转的代码和我的不一样,不知道是否是因为action类文件中只有一个方法的原因所以简写了。
我的写法是
form.action = "agentsTracker.do?method=action中方法名&formAction=stop";
form.submit();
楼主这个写法可以尝试下
form.action = "agentsTracker?formAction=stop";
form.submit();
<% session.setAttribute("formAction", "stop"); %>
这种java代码了。
果然证实了我的猜想,谢谢。
现在的问题是,我怎么使用js通过session传值呢,按理说js是不能操作服务端的session吧,
本来打算用request来传值的,但是又超过范围了,只好使用session了