Gerardo Ventas
Gerardo Ventas

Reputation: 119

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?

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?

Upvotes: 0

Views: 1158

Answers (3)

Maran Manisekar
Maran Manisekar

Reputation: 931

<℅!℅>, is used for just variable and method declaration.

Upvotes: 0

Vasu
Vasu

Reputation: 22422

All JSP files will be converted into Servlet (.java and finally .class) files by the Application server.

JSP Declarations:

<%! 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.

Scriptlets:

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

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201439

The <%! %> is for JSP Declarations.

The linked Java 5 EE Tutorial says (in part)

A JSP declaration is used to declare variables and methods in a page’s scripting language.

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

Upvotes: 2

Related Questions