Jsp的基本语法
2017-04-20 20:40
330 查看
什么是Jsp
JSP(全称JavaServer Pages) 其根本是一个简化的Servlet设计,他实现了在java中使用Html标签。jsp是一种动态网络技术标准.也是JAVAEE的标准。Jsp和Servlet一样,是服务器端执行的。Jsp与其他动态网站开发技术的优缺点
Jsp元素构成
jsp指令
page指令
jsp注释
jsp脚本语法
jsp声明变量
jsp表达式表示方法
<%@page import="java.text.SimpleDateFormat"%> <%@ page language="java" import="java.util.*" contentType="text/html; charset=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 'index.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> Hello This is my JSP page. <br> <h1>大家好</h1> <!-- 我是html注释 --> <%--我是jsp注释 --%> <%!String s = "jack"; int add(int x, int y) { return x + y; }%> <% //单行注释 /* 多行注释 */ out.print("欢迎学习javaee"); %> <br> 你好<%=s%><br> x+y=<%=add(10, 5)%> <br> <%!SimpleDateFormat sdf = new SimpleDateFormat(); String date = sdf.format(new Date());%> 今天是<%=date%> </body> </html>
显示效果如下
下面我们用两种方法打印九九乘法表
<%@page import="com.sun.xml.internal.fastinfoset.tools.PrintTable"%> <%@ page language="java" import="java.util.*" contentType="text/html; charset=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 'execise.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> <h1>九九乘法表</h1> <%!String pirntTable1() { String s = ""; for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) { s += i + "*" + j + "=" + (i * j) + " "; } s += "<br>"; } return s; }%> <br> <%=pirntTable1()%> <h1>脚本方式打印九九乘法表</h1> <%!void pirntTable2(JspWriter out) throws Exception { for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) { out.println(i + "*" + j + "=" + (i * j) + " "); } out.println("<br>"); } }%> <br> <% pirntTable2(out); %> </body> </html>
效果如图
相关文章推荐
- JSP 基本语法
- JSP的基本语法与简单表单处理
- (三)JSP基本语法B
- JSP基本语法(上)
- (七)JSP基本语法--session对象
- JSP基本语法
- JSP基本语法(指令,属性,内置对象)[转]
- JSP基本语法
- jsp基本语法
- jsp的基本语法
- jsp程序设计(二)-jsp基本语法(9)- jsp:include
- JSP基本语法
- jsp程序设计(二)-jsp基本语法(5)-Page指令
- JSP 基本语法总结
- jsp程序设计(二)-jsp基本语法(10)- jsp:plugin
- jsp程序设计(二)-jsp基本语法(2)-声明
- jsp程序设计(二)-jsp基本语法(8)- jsp:getProperty
- jsp程序设计(二)-jsp基本语法(7)- jsp:forward
- JSP基本语法D--request对象
- jsp基本语法介绍