JSP入门学习

来源:互联网 发布:淘宝apple store 编辑:程序博客网 时间:2024/06/06 16:29
今天我们学的是JSP: 

JSP语法:

JSP模板元素、JSP表达式、JSP脚本片断、JSP注释、JSP指令、JSP标签、JSP内置对象。

JSP模板元素:

JSP页面中的HTML内容称之为JSP模板元素。

JSP模板元素定义了网页的基本骨架,即定义了页面的结构和外观。

JSP脚本表达式:

JSP脚本表达式(expression)用于将程序数据输出到客户端语法:<%=变量或表达式%>

举例:当前日期:<%=new java.util.Date()%>

JSP引擎在翻译脚本表达式时,会将程序数据转成字符串,然后在相应位置用out.print(....),,将数据输给客户端;

JSP脚本表达式中的变量或表达式后面不能有分号(;)。

下面是在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 'chengfakoujue.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>

<div>

<div>

<h1>

打印出乘法口诀

</h1>

<hr color="red">

</div>

<div>

<%

for (int row = 1; row <= 9; row++) {

for (int col = 1; col <= row; col++) {

%>

<%=col%>*<%=row%>=<%=row * col%>  

<%

}

%>

<br>

<%

}

%>

</div>

<div>

<h1>

三角形

</h1>

<hr color="red">

</div>

<div>

<%

for (int i = 1; i <= 9; i++) {

for (int j = 10; j >= i; j--) {

%>

<%=" "%>

<%

}

%>

<%

for (int x = 1; x <= i; x++) {

%>

<%="* "%>

<%

}

%>

<br>

<%

}

%>

<div>

<hr color="red">

<h3>

水仙花数

</h3>

<%

for (int i = 100; i < 999; i++) {

int x, s, y;

x = i / 100;

s = (i - x * 100) / 10;

y = i - x * 100 - s * 10;

if (i == y * y * y + s * s * s + x * x *x) {

%>

<%=i%> 

<%

}

%>

<%

}

%>

</div>

</div>

</div>

</body>

</html>