简体   繁体   English

转换JSP Java代码

[英]Convert JSP java code

I have a web application, I'm using JSP, so when I insert java code I start opening it as <%%> but I want to use <%!%> how can I do it? 我有一个Web应用程序,我正在使用JSP,所以当我插入Java代码时,我开始以<%%>打开它,但是我想使用<%!%>怎么做?

There is my code: 有我的代码:

  <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>

So, how can I do that? 那么,我该怎么做呢?

The <%! %> <%! %> <%! %> is for JSP Declarations . <%! %>用于JSP声明

The linked Java 5 EE Tutorial says (in part) 链接的Java 5 EE教程说(部分)

A JSP declaration is used to declare variables and methods in a page's scripting language. JSP声明用于以页面的脚本语言声明变量和方法。

So you'd need to declare something 所以你需要声明一些东西

<%!
    static final int getANumber() {
        return 101;
    }
%>

then you can call that function in a scriptlet. 那么您可以在脚本中调用该函数。 But you really should prefer to use a more modern Java web technology (and avoid scriptlets). 但是您确实应该更喜欢使用更现代的Java Web技术(并避免使用scriptlet)。

All JSP files will be converted into Servlet (.java and finally .class) files by the Application server. 应用程序服务器将所有JSP文件转换为Servlet(.java,最后是.class)文件。

JSP Declarations: JSP声明:

<%! <%! int count = 0; 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. %> ===>在服务器生成的Servlet类中声明“ count”实例变量,该变量对于所有请求都是通用的,除非有充分的理由,否则不鼓励使用此变量。

Scriptlets: 小脚本:

<% int count =0; <%int count = 0; %> ===> Declares Local Variable inside service() method, this variable is local for each request, so there is no problem of usage. %> ===>在service()方法内声明局部变量,该变量对于每个请求都是局部的,因此没有使用问题。 However, scriptles inside JSP are generally considered as bad practice. 但是,JSP中的脚本通常被认为是不好的做法。

<℅!℅>,仅用于变量和方法声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM