转换 JSP java 代码
Convert JSP java code
我有一个网络应用程序,我正在使用 JSP,所以当我插入 java 代码时,我开始以 <%%>
打开它,但我想使用 <%!%>
怎么办?
这是我的代码:
<body>
<h1>Factorial</h1>
<form action="" method=post>
<input type="number" name="cantidad" min="1">
<input type= "submit" value=submit>
</form>
<%
String numero=request.getParameter("cantidad");
if(numero != null){
Integer num = Integer.parseInt(request.getParameter("cantidad"));
if(num != null){
int res =1;
for (int s=1; s <=num; s++){
res *=s;
}
out.print(res);
}
}
%>
</body>
那么,我该怎么做呢?
<%! %>
用于 JSP Declarations。
链接的 Java 5 EE 教程说(部分)
A JSP declaration is used to declare variables and methods in a page’s scripting language.
所以你需要声明一些东西
<%!
static final int getANumber() {
return 101;
}
%>
然后您可以在 scriptlet 中调用该函数。但您确实应该更喜欢使用更现代的 Java 网络技术(并避免使用 scriptlet)。
所有 JSP 文件将由应用程序服务器转换为 Servlet(.java,最后是 .class)文件。
JSP 声明:
<%! int count = 0; %> ===> Declares 'count' Instance Variable inside the
Servlet Class generated by the Server, this variable is common for all requests, usage of this is discouraged, unless there is a strong reason.
小脚本:
<% int count =0; %> ===> Declares Local Variable inside service()
method, this variable is local for each request, so there is no problem of usage. However, scriptles inside JSP are generally considered as bad practice.
<℅!℅>,仅用于变量和方法声明。
我有一个网络应用程序,我正在使用 JSP,所以当我插入 java 代码时,我开始以 <%%>
打开它,但我想使用 <%!%>
怎么办?
这是我的代码:
<body>
<h1>Factorial</h1>
<form action="" method=post>
<input type="number" name="cantidad" min="1">
<input type= "submit" value=submit>
</form>
<%
String numero=request.getParameter("cantidad");
if(numero != null){
Integer num = Integer.parseInt(request.getParameter("cantidad"));
if(num != null){
int res =1;
for (int s=1; s <=num; s++){
res *=s;
}
out.print(res);
}
}
%>
</body>
那么,我该怎么做呢?
<%! %>
用于 JSP Declarations。
链接的 Java 5 EE 教程说(部分)
A JSP declaration is used to declare variables and methods in a page’s scripting language.
所以你需要声明一些东西
<%!
static final int getANumber() {
return 101;
}
%>
然后您可以在 scriptlet 中调用该函数。但您确实应该更喜欢使用更现代的 Java 网络技术(并避免使用 scriptlet)。
所有 JSP 文件将由应用程序服务器转换为 Servlet(.java,最后是 .class)文件。
JSP 声明:
<%! int count = 0; %> ===> Declares 'count' Instance Variable inside the Servlet Class generated by the Server, this variable is common for all requests, usage of this is discouraged, unless there is a strong reason.
小脚本:
<% int count =0; %> ===> Declares Local Variable inside service() method, this variable is local for each request, so there is no problem of usage. However, scriptles inside JSP are generally considered as bad practice.
<℅!℅>,仅用于变量和方法声明。