首页 > Web开发 > 详细

day_07JSP编程----基本语法

时间:2015-12-24 02:21:34      阅读:284      评论:0      收藏:0      [点我收藏+]

JSP编程----基本语法

jsp传统语法:

? ? 声明Declaration

? ? 脚本 Scriptlet

? ? exception

? ? 注释comment

? ? Directives

? ? Action动作指令

? ? 内置对象

JSTL

JSF

其他taglib(如Struts)

?

?

一、变量声明方法,解释需要看编译出的java文件

?

<%! int accessCount = 0;

%>

<%= ++accessCount%>

是声明的全局变量,由于只有一个servlet,所以每次都会加1。

?

<% int accessCount = 0;

%>

<%= ++accessCount2%>

是声明的局部变量,由于每次调用都会初始化,所以结果会0-->1

?

代码案例:

访问account0是成员变量,只声明一次;

访问account1是局部变量,每次刷新都会声明。

<html>
<head></head>
<body>
<% out.println("HelloWorldJSP");%>
 <br/>
<%! int account0=0;%>
<% int account1 =0; %>
 <%=(++account0)%>
 <br>
 <%=(++account1)%>
</body>
</html>

?运行结果:

HelloWorldJSP?

7?

1

注意:

?可以在<%! %>声明成员方法,因为是全局的;

不可以在<% %>声明方法,因为本身就是在方法里。

?

?

二、scriptlet:

基本语法:

? ?<%程序代码区%>

可以放入任何的java程序代码

例如:

? ? ? <%

? ? ? for(int i=0;i++;i++){....

? ? }

? ? ?%>

BGClolo.jsp

?

?

代码案例:

<html>
 <%
 	String bgColor = request.getParameter("bgColor");
 	boolean hasColor;
 	if(bgColor!=null){
 		hasColor=true;
 	}else{
 		hasColor=false;
 		bgColor="WHITE";
 	}
  %>
<body BGCOLOR=<%=bgColor%>>
<%
	if(hasColor){
		out.println("bgcolor="+bgColor);
	}else{
		out.println("bgcolor = white");
	}
 %>
<br>
<hr>
<% out.println("HelloWorldJSP");%>
 <br/>
<%! int account0=0;%>
<% int account1 =0; %>
 <%=(++account0)%>
 <br>
 <%=(++account1)%>
 <br>
 <!-- lala-->
</body>
</html>

?运行结果:

http://localhost:8080/Day07_HelloWordJsp/HelloWorldJsp.jsp?bgColor=yellow

页面展示

bgcolor=yellow?

?

三、注释格式:

<%--........--%>

<%//.....%?

<%/*.....*/%>

?

?

四、表达式

基本语法:

? <%= ....%>

? =后面必须是字符串变量或者可以被转换成字符创的表达式

?不需要以;结束

?只有一行

例子:

? ? ?<%= "HELLO WORLD"%>

? ? <%= I+1%>

? ? <%= request.getParametesr("name")%>

?

?

代码案例:

<%@page import="java.util.Date"%><html>
 <%
 	String bgColor = request.getParameter("bgColor");
 	boolean hasColor;
 	if(bgColor!=null){
 		hasColor=true;
 	}else{
 		hasColor=false;
 		bgColor="WHITE";
 	}
  %>
<body BGCOLOR=<%=bgColor%>>
<%
	if(hasColor){
		out.println("bgcolor="+bgColor);
	}else{
		out.println("bgcolor = white");
	}
 %>
<br>
<hr>
<% out.println("HelloWorldJSP");%>
 <br/>
<%! int account0=0;%>
<% int account1 =0; %>
 <%=(++account0)%>
 <br>
 <%=(++account1)%>
 <br>
 <!-- lala-->
 <hr>
 <ul>
 	<li><%= new Date() %></li>
 	<li><%= session.getId() %></li>
 	<li><%= request.getParameter("HAHA") %></li>
 </ul>
</body>
</html>

?运行结果:

http://localhost:8080/Day07_HelloWordJsp/HelloWorldJsp.jsp?HAHA=%22ZHUHW%22

?

Tue Dec 22 23:06:39 CST 2015

ECD1BCDB63BE321433725A9BC2EB8687

"ZHUHW"

?

?

星期二, 十二月 22, 2015 ?23:07:37

day_07JSP编程----基本语法

原文:http://yuzhouxiner.iteye.com/blog/2265875

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!