网络编程
位置:首页>> 网络编程>> Jsp编程>> jsp学习之scriptlet的使用方法详解

jsp学习之scriptlet的使用方法详解

作者:Hackerman  发布时间:2023-06-27 11:06:37 

标签: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>

来源:https://www.cnblogs.com/Hackerman/p/7610957.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com