欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

《JavaWeb项目开发教程》第二章代码 博客分类: 周国烛 jsp 

程序员文章站 2024-03-21 12:28:04
...
 //使用JSP的脚本和指令
 
//chap02.sample2-1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%!
//声明一个变量
String str = "Object-oriented programming";
//声明一个字符串截取的方法
public String subStr(String str){
if(str == null || "".equals(str))
return "";
if(str.length() > 20)
return str.substring(0, 20) + "...";
else
return str;
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   <title>JSP中声明的使用</title>
  </head>
  
  <body>
    <h3>原字符串是:<%=str %>></h3>
    <h3>截取后字符串是:<%=subStr(str)%>></h3>
  </body>
</html>
//chap02.sample2-2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
    <title>JSP中表达式的使用</title>
  </head>
  
  <body>
    今天是:<%=new Date() %>
  </body>
</html>
//chap02.sample2-3.jsp
<%@ page language="java" import="java.util.*,java.text.*" pageEncoding="utf-8"%>
<html>
  <head>
    <title>输出当前时间</title>
  </head>
  
  <body>
    欢迎光临! <br>今天是<br>
    <%
    SimpleDateFormat formater = new SimpleDateFormat("yyyy年MM月dd日");
    String strCurrentTime = formater.format(new Date());
    out.print(strCurrentTime);
     %>
  </body>
</html>
//chap02.sample2-4.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file = "/logo.html" %>
<html>
  <body>
    <br>
    <CENTER><%@ include file = "/sample2_3.jsp" %></CENTER>
    <hr/>
    <CENTER><%@ include file = "/sample2_1.jsp" %></CENTER>
  </body>
</html>
 
//chap02.asmple2_5.jsp
<%@ page language="java" import="java.util.*" errorPage = "sample2_6.jsp" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 
  <body>
    大家都知道,零不能做除数<br>
    在程序设计时,如果出现零作除数的现象,程序将产生异常<br>
    我们可以指定一个JSP页面,由该页面来处理当前页面中抛出但没有捕获的任何异常<br>
    下面我们在程序中设置一个除法异常,大家观察页面运行效果,提货errorPage属性的作用<br>
    <%=100/0 %>
  </body>
</html>
//chap02.asmple2_6.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  <body>
    您所看到的是sample2_6.jsp页面的处理页面!<br>
  </body>
</html>
//chap02.asmple2_7.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
    
  </head>
  
  <body>
    <jsp:include page = "/sample2_6.jsp"/>
    <br/>
    <CENTER><jsp:include page = "/sample2_3.jsp"/> </CENTER>
    <hr/>
    <CENTER><jsp:include page = "/sample2_1.jsp"/> </CENTER>
  </body>
</html>
//chap02.asmple2_8.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
    <title>jsp:forward测试</title>
  </head>
  
  <body>
    <h3><font color = "red">jsp:forward动作标签测试</font></h3>
    <%
    int i = (int)(Math.random() * 10);
    if(i >= 5){
     %>
     <jsp:forward page = "sample2_1.jsp"></jsp:forward>
     <%
     }else
     {
      %>
      <jsp:forward page = "sample2_3.jsp"></jsp:forward>
      <%} %>
  </body>
</html>
//chap02.asmple2_9.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
   <title>注释测试</title>
  </head>
  
  <body>
   <h2>注释测试</h2>
   <!-- 在页面源代码中,这个HTML注释是看得见的 !-->
   <!-- 在页面源代码中,这个值为"hello"的表达式的HTML注释是看得见的!  <%= new String("hello")%>
   -->
   
   
   <%--在页面源代码中,这个隐藏注释是看不见的! --%>
   <%--在页面源代码中,隐藏注释即使包含了jsp表达式,在客户端也是看不到的!
   <% = new String("hello")%> --%>
  </body>
</html>
 
 


 

相关标签: jsp