简单的jsp页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'MyJsp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 欢迎学习Java Web知识, 现在时间是: <%out.println(new java.util.Date()); %> </body> </html>java脚本语言放在<% 和%>之间,通过这种方式将java代码嵌入在HTML页面中,由此便生成了动态的jsp页面。jsp的本质依然是一个servlet,而servl是一个特殊的类。
<%@page import="com.sun.org.apache.regexp.internal.REDebugCompiler"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'test.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <!-- 下面是jsp页面 --> <%for (int i = 0; i<7; i++) { out.println("<font size="+i+">"); %> 疯狂Java学习过程中</font> <br/> <%} %> </body> </html>font标签注解:规定文本的字体,字体尺寸以及字体大小
<font size="3" color="red">This is some text!</font> <font size="2" color="blue">This is some text!</font> <font face="verdana" color="green">This is some text!</font>
<%@page import="com.sun.org.apache.regexp.internal.REDebugCompiler"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'test.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <table bgcolor = "#9999dd" border = "1" width = "300px"> <% for (int i = 0; i<10; i++) { %> <tr> <td>循环值:</td> <td><%= i %></td> </tr> <% } %> </table> </body> </html>
JSP的三个编译指令:
1、page:针对当前页面的指令
2、include:用于指定包含另一个页面
3、taglib:用于定义和访问自定义标签
JSP的7个动作指令:
1、jsp:forward:执行页面转向,将请求的处理转发到下一个页面
2、jsp:param:用于传递参数
3、jsp:include:用于动态引入一个jsp页面
4、jsp:plugin:用于下载JavaBean或Applet到客户端执行
5、jsp:useBean:创建一个JavaBean的实例
6、jsp:setProperty:设置javaBean实例的属性值
7、jsp:getProperty:输出JavaBean实例的属性值