jsp学习一

1,显示不同字体:
    <%
         for(int i=0;i<6;i++){
              out.write("<h"+i+">"+"Hello World!</h"+i+">");
             
         }
    %>

2,输出
     <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%! int i=0; %>
    <c:out value="hello" />
     <%=i %>
3,显示当前时间
   <%
         Date today=new Date();
         int hours=today.getHours();
         if(hours>=0 && hours<12){
              out.println("good morning");
         }else{
              out.println("good afternoon");
         }
         String[] weekdays={"日","一","二","三","四","五","六"};
         out.println("today is"+(today.getYear()+1900)+"year-"+(today.getMonth()+1)+"month-"+(today.getDate())+"day-"+weekdays[today.getDay()]+"week");
    %>
4,include包含语句
     <%@ include file="hello.jsp"%>

5,error页面范例
error.jsp:
     <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1" isErrorPage="true" %>
     <%@ page import="java.io.PrintWriter" %>
     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     <html>
       <head>
         <title>My JSP 'error.jsp' starting page</title>

       </head>
      
       <body>
         error in <%=exception %><br/>
         the reson is below:<br/>
         <%exception.printStackTrace(new PrintWriter(out)); %>
       </body>
     </html>

main.jsp:
     <%@ page language="java" import="java.util.*" pageEncoding="utf-8" errorPage="error.jsp" %>

     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     <html>
       <body>
         <%
                  int x=5/0;
         %>
       </body>
     </html>
6,jsp:forward
     <jsp:forward page="hello.jsp"></jsp:forward>
7,简单计数器
  <body>
   <%
        Integer count=null;
    synchronized(application){
        count=(Integer)application.getAttribute("basic.counter");
            if(count==null) count=new Integer(0);
            count=new Integer(count.intValue()+1);
            application.setAttribute("basic.counter",count);
   }
   %>
   you are the <%=count %> visitors
   
  </body>

8,设置页面属性
     <%@ page info="xiaoxi,where are you ,so are you happy and happiness" %>
...
     <%=getServletInfo() %>

9,c:catch的使用
   <head>
    <title>My JSP 'differentfont.jsp' starting page</title>
     <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  </head>
 
  <body>
     <c:catch var="errorMessage">
          <%
               String name="xiaoxi";
               int i=Integer.parseInt(name);
          %>    
     </c:catch>
     ${errorMessage}
  </body>

  10,c:forEach使用
    <head>
    <title>My JSP 'differentfont.jsp' starting page</title>
     <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  </head>
 
  <body>
     <%
     String strs[]=new String[5];
     strs[0]="hello";
     strs[1]="this";
     strs[2]="is";
     strs[3]="my";
     strs[4]="love";
     request.setAttribute("strs",strs);
     %>
     <c:forEach items="${strs}" var="item" begin="1" end="5" step="2">
          ${item}<br/>
     </c:forEach>
  </body>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值