Use JSP+JavaBean architecture to implement a simple counter function:
The relevant documents are as follows:
1. d:/demo/WEB-INF/classes/count/counter.java:
package count;
public class counter
{
int count = 0;
public int getCount()
{
count++;
return this.count;
}
public void setCount(int count)
{
this.count = count;
}
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
2. d:/demo/counter.jsp:
<%@ page language=”java” import=”java.util.*” pageEncoding=”gb2312″%>
<html>
<head>
<title>counter</title>
</head>
<body>
<h1>Counter</h1>
<jsp:useBean id=”bean0″ scope=”session” class=”count.counter” />
<%
out.println(”The Counter is :”+bean0.getCount()+”<br>”);
%>
The Counter is: <jsp:getProperty name=”bean0″ property=”count” /><br>
</body>
</html>
I encountered the following problems during operation:
1. When jsp is running, it appears: The value for the useBean class attribute is invalid. Cause of the problem:
The file counter.java is not placed in the directory d:/demo/WEB-INF/classes, and counter.java is not compiled.
2. When java files call each other, an error occurs when compiling a single file:
If you are in the same directory and there is a problem when compiling a single file, you can switch the command to the directory and execute javac *.java
This will compile all java files in the directory.