我试图计算每个用户进入网站的次数,但我的问题是即使我更改了用户名,点击次数也不会重新开始。此外,一旦我重新加载网页,用户的名称就会变为“null”。
<HTML>
<BODY>
<FORM METHOD=POST ACTION="SaveName.jsp">
Who are You ? <INPUT TYPE=TEXT NAME=username SIZE=20>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>
<%@ page import="java.io.*,java.util.*" %>
<html>
<head>
<title>Applcation object in JSP</title>
</head>
<body>
<%
String name = request.getParameter( "username" );
session.setAttribute( "theName", name );
Integer hitsCount =
(Integer)application.getAttribute("hitCounter");
if( hitsCount ==null || hitsCount == 0){
/* First visit */
out.println("Welcome to my website! ");
hitsCount = 1;
}else{
/* return visit */
out.println("Welcome back to my website!");
hitsCount += 1;
}
application.setAttribute("hitCounter", hitsCount);
%>
<%= session.getAttribute( "theName" ) %>
<br>Total number of visits: <%= hitsCount%></br>
</body>
</html>