Servlet_JSP(2) JSP
1.最简单的JSP
HelloWorld.jsp
<html>
<head>
<title>Hello</title>
<body>
<%
out.println("Hello World!");
%>
</body>
</head>
</html>
---------------------------------------------------------------------------------------------------------------------------------------
2.JSP中的全局变量和局部变量
AccessCounts.jsp
<html>
<head>
<title>JSP Declaration</title>
</head>
<body>
<%!
// 全局变量
int accessCount = 0;
%>
<%
// 局部变量
int accessCount2 = 0;
%>
<h2>AccessCount:
<br>Overall Variable:<%= ++accessCount %>
<br>Local Variable:<%= ++accessCount2 %>
</h2>
</body>
</html>
测试结果:访问同一页面,每刷新一次,accessCount增1,accessCount2不变(每次出现一个新的局部变量)。
----------------------------------------------------------------------------------------------------------------------------------
3.注释、当前页面从URL中取背景色参数
BGColor.jsp
<html>
<head>
<title>Color Testing</title>
</head>
<!--
HTML注释
客户端可以看见
-->
<%--
JSP注释
客户端看不见
--%>
<%
//注释2
/*
注释3
*/
// 将请求中参数bgColor的值拿过来,假如没有传这个参数,则值为null
String bgColor = request.getParameter("bgColor");
boolean hasColor;
if(bgColor != null) {
hasColor = true;
} else {
hasColor = false;
bgColor = "white";
}
%>
<!--显示背景色-->
<body bgcolor="<%= bgColor%>">
<h2 align="center">Color Testing</h2>
<%
if(hasColor) {
out.println("You supplied a backgroud color of " + bgColor + ".");
} else {
out.println("Use Default backgroud color of white");
}
%>
</body>
</html>
-----------------------------------------------------------------------------------------------------------------------------
4.表达式
Expressions.jsp
<html>
<head>
<title>JSP Expressions</title>
</head>
<!--表达式-->
<body>
<h2>JSP Expressions</h2>
<UL>
<!--获取当前本地时间-->
<LI>Current Time:<%= new java.util.Date().toLocaleString() %>
<LI>Your HostName:<%= request.getRemoteHost() %>
<!--获取当前页面的SessionID-->
<LI>Your Session Id:<%= session.getId() %>
<LI>The <code>testParam</code> from parameter:<%= request.getParameter("testParam") %>
</UL>
</body>
</html>
----------------------------------------------------------------------------------------------------------------------------------------
5.@page指示语句的测试
TestDirective.jsp
<%@page import="java.util.*" %>
<%@page contentType="text/html;charset=gb2312" %>
<!--@page指示语句的测试-->
<!--将当前系统时间转变成我们本地常用的形式输出-->
<%= new Date().toLocaleString() %>
<br><br>
<%
out.println("你好!");
%>
--------------------------------------------------------------------------------------------------------------------------------
6.错误页跳转测试
①TestErr.jsp
<%@page errorPage="ErrPage.jsp" %>
<!--如果本页面出错则跳转到ErrPage.jsp页面-->
<%
String str = "123abc";
int i = Integer.parseInt(str);
out.println("str= " + str + ",i= " + i);
%>
②ErrPage.jsp
<%@page contentType="text/html;charset=gb2312" %>
<%@page isErrorPage="true" %>
<!--本页面是个错误信息显示页-->
<html>
<body text="red">
错误信息:<%= exception.getMessage()%>
</body>
</html>
-----------------------------------------------------------------------------------------------------------------------------
7.include指令”%@ include file“和include动作指令“jsp:include page”
前者是先包含进来再编译执行;后者是先各自编译执行再包含进来
①include.jsp
<html>
<head>
<title>include test</title>
</head>
<body bgcolor="white">
<font color="red">
The current time and date are:<br>
<!--先将date.jsp的内容包含进来,再一起进行转换、编译和执行-->
<%@include file="date.jsp" %><br>
<!--先将date.jsp进行转换、编译和执行,再将结果包含进来-->
<jsp:include page="date.jsp" flush="true" />
</font>
</body>
</html>
②date.jsp
<%@page import="java.util.*" %>
<%--a string representation of this date, using the locale conventions.--%>
<%= (new Date()).toLocaleString() %>
Servlet_JSP(2) JSP
最新推荐文章于 2024-11-05 15:26:40 发布