您的位置:首页 > Web前端 > JavaScript

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 servlet java ee