search
HomeCommon ProblemWhat are the declarations in jsp used for?

Declarations in jsp are used to declare variables, methods and calling classes in jsp pages. A declaration is a pair of tags starting with []. The tags can contain any number of legal Java declaration statements.

What are the declarations in jsp used for?

Declaration (declaration) is used to declare variables and define methods in JSP pages. A declaration is a tag starting with and ending with <code>%>, which can contain any number of legal Java declaration statements. The following is an example of a JSP declaration:

<%! int count = 0; %>

The above code declares a variable named count and initializes it to 0. The declared variable is initialized by the container only once when the page is first loaded. After initialization, the value is maintained in subsequent requests.

Variables, methods and classes can be declared in JSP pages. The declaration format is as follows:

<%!声明变量、方法和类的代码%>

Pay special attention to the fact that there should be no spaces between "

Declare variables

<%!
    int x,y=100,z;
    String tom=null,jery="Love JSP";
    Date date;
%>

Declare methods

<%@ 
page contentType="text/html; charset=utf-8" %><%!int num = 0;                    
//声明一个计数变量
synchronized void add(){
    //该方法实现访问次数的累加操作
    num++;
}
%>
<% add(); %>
<html>
    <body>
        <center>您是第<%=num%>位访问该页面的游客!</center>
    </body>
</html>

What are the declarations in jsp used for?

Declaration class

<%@ 
page contentType="text/html; 
charset=utf-8"
%>
<html>
<head>
<title></title>
</head>
<body>
<p><font size="4">请输入圆的半径:<br></font></p>
<form action="" method="get" name="form" id="form">
<font size="4"><input type="text" name="cat" value="1"> 
<input type="submit" value="送出" name="submit"></font>
</form>
<%!
public class Circle{
    double r;
    Circle(double r){
        this.r = r;
}
double 求面积(){
    return Math.PI*r*r;}}
%>
<%
    String str = request.getParameter("cat");
    double r;
    if(str != null){
        r = Double.parseDouble(str);
    }else{
        r = 1;
    }
    Circle circle = new Circle(r);
%>
<p><font size="4">
圆的面积是:
<%=circle.求面积()%>
</font></p>
</body>
</html>

What are the declarations in jsp used for?

The above is the detailed content of What are the declarations in jsp used for?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment