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>
jsp学习一
最新推荐文章于 2022-07-22 18:20:00 发布