Html 会话JSP按钮单击
嗨,我想做一个JSP程序,其中有一个数字显示和一个按钮。当用户单击此按钮时,上面的数字将递增。我想在这个程序中包括会话 我所做的是: 这是html格式的表单:Html 会话JSP按钮单击,html,jsp,button,Html,Jsp,Button,嗨,我想做一个JSP程序,其中有一个数字显示和一个按钮。当用户单击此按钮时,上面的数字将递增。我想在这个程序中包括会话 我所做的是: 这是html格式的表单: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>My Form</title> </head> <body> &l
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My Form</title>
</head>
<body>
<%! public void displayNum(){ %>
Number: <%=session.getAttribute("Counter") %>
<%! } %>
<FORM METHOD=POST ACTION="getcount.jsp">
<button TYPE="button" ONCLICK= "displayNum()">Add 1</button>
</FORM>
</body>
</html>
我的表格
编号:
加1
这是我的JSP文件:
<%
AddCount addCount = new AddCount();
addCount.setCounter(addCount.addCounter(addCount.getCounter()));
int counter = addCount.getCounter();
session.setAttribute("Counter", counter);
%>
其中AddCount是一个java类,带有一个变量counter、setter和getter以及一个增加计数器AddCount(num)的函数;运行文件时,我得到的只是一个没有任何文本的按钮:/
我试了一次又一次。有人能帮我吗
谢谢 您正在html中添加java代码,这是不可能的 第二件事,即使您在AddCount中有一个静态int计数器,它也不会工作,因为许多用户可能会使用此页面,并且期望每次单击只增加一个 因此,您应该编写一个类似index.jsp的jsp文件
<%Integer counter = (Integer)request.getSession().getAttribute("counter")
if(counter==null) {counter=0;}
counter ++;
request.getSession().setAttribute("counter",counter);
%>
<div>counter=<%=counter%></div><br>
<a href="index.jsp">+1</a>
计数器=
我的表格
函数displayNum()
{
document.getElementById(“demo”).innerHTML=“”;
}
{此处将显示编号}
加1
我的表格
$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
$(“#div1”).load(“increment.jsp”);
});
});
{此处将显示编号}
加1
和increment.jsp文件:
<%
int count;
if (session.getAttribute("Counter") == null) {
count = 1;
} else {
count = (Integer) session.getAttribute("Counter");
count++;
}
session.setAttribute("Counter", count);
out.println(session.getAttribute("Counter"));
%>
我试过这个。。但当我运行它的时候,我只得到1号,没有按钮!!!感谢您帮助btwTry使用jQueryAjax,我已经添加到下一个答案中。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script src="jquery.js"></script>
<title>My Form</title>
</head>
<body>
<script>
$(document).ready(function() {
$("button").click(function() {
$("#div1").load("increament.jsp");
});
});
</script>
<div id="div1"> {number will be displayed here} </div>
<button>Add 1</button>
</body>
</html>
<%
int count;
if (session.getAttribute("Counter") == null) {
count = 1;
} else {
count = (Integer) session.getAttribute("Counter");
count++;
}
session.setAttribute("Counter", count);
out.println(session.getAttribute("Counter"));
%>