jsp學習之scriptlet的使用方法


scriptlet的使用

jsp頁面中分三種scriptlet:

第一種:<%  %>  可以在里面寫java的代碼。定義java變量以及書寫java語句。

第二種:<%! %>  可以在里面定義全局變量以及方法,類。

第三種:<%=%> 用於打印變量或者輸出值。

 

<%  %>的使用

<html>
<head>
<title>this is java page</title>
</head>
<body>
<!--顯示注釋   注釋內容 -->

<% 
  
int x=10;
int y=20;
String str=request.getParameter("info");
out.println("<h1>"+str+"</h1>");
out.println("<h1>"+(x+1)+"</h1>");
out.println("<h2>"+y+"</h2>");

%>

</body>

</html>

 

<%!  %>的使用

<html>
<head>
<title>this is java page</title>
</head>
<body>
<!--顯示注釋   注釋內容 -->

<%!
 public static final int x=10;

%>
<%!
  public int add(int x,int y)
 {
     return x+y;
 }
%>

<%!
class person
{
  private String name;
  private int age;
  public person(String name,int age)
  {
    this.name=name;
    this.age=age;
   }

   public String toString()
  {
    return "name="+name+",age="+age;
   }

}


%>
<%!
public int li=20;

%>
<%
   person p=new person("test",10);
   out.println(p);
   out.println(li);
   out.println(add(x,20));
%>
<%
  int b=10;
 out.println(b);
%>
</body>

</html>

 

<%= %>的使用

<html>
<head>
<title>this is java page</title>
</head>
<body>
<!--顯示注釋   注釋內容 -->

<% 
  
int x=10;
int y=20;
String str=request.getParameter("info");

%>
<%=x%>
<%=y%>
<%="strinsssa"%>
</body>

</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM