Jsp學習筆記(三)-----Jsp語法!
2024-09-05 00:19:30
供稿:網友
1 <html>
<head>
<title>
helloworld example
</title>
</head>
<body>
--------------
<%
string msg="this is a jsp test";
out.print("hello world");
%>//java模塊 申明msg為字符串變量
--------------
<h2> <%=msg%></h2>//輸出變量
</body>
</html>
2 在客戶端的源代碼中顯示注釋:
<!--this file displays the user login screen,and the time is <%=(new java.util.date()).tostring()%>-->//
<html>
<head>
<title>
helloworld example
</title>
</head>
<body>
<%
string msg="我是tomcat";
out.print("hello world");
%>
<h2> <%=msg%></h2>
</body>
</html>
3 隱藏注釋
<!--this file displays the user login screen,and the time is <%=(new java.util.date()).tostring()%>-->
<%@page language="java"%>//客戶端看不到注釋
<html>
<head>
<title>
helloworld example
</title>
</head>
<body>
<%
string msg="我是tomcat";
out.print("hello world");
%>
<h2> <%=msg%></h2>
</body>
</html>
4 聲明變量,方法和新的類
<!--this file displays the user login screen,and the time is <%=(new java.util.date()).tostring()%>-->
<%@page language="java"%>
<html>
<head>
<title>
helloworld example
</title>
</head>
<body>
<%!
public class university
{
string name,city;
university(string name,string city)
{
this.name=name;
this.city=city;
}
public string getname()
{
return name;
}
public string getcity()
{
return city;
}
}
%>
<%
university university1=new university("山東科技大學","青島市");
university university2=new university("山東大學","濟南市");
out.println("第一所大學是:");
out.println(university1.getname()+"<br>");
out.println(university1.getcity()+"<br>");
out.println("第二所大學是:");
out.println(university2.getname()+"<br>");
out.println(university2.getcity());
%>
</body>
</html>
5 表達式
<%@page language="java"%>
<html>
<head>
<title>
helloworld example
</title>
</head>
<body>
<h2>a test of expression</h2>
<%=(new java.util.date()).tostring()%>//表達式的結尾無;結束
</body>
</html>
菜鳥學堂: