本系列博客汇总在这里:JSP 汇总
目录
- JSP 的 scriptlet
JSP 的 scriptlet
从单词上分析 script 是脚本:let 在 JAVA 中表示的小程序,scriptlet 表示脚本小程序。
JSP 中的 Java 代码块就是最常见的动态信息。
它分为三种:
① <%…%>:java 代码片段
代码片段会原模原样的出现在真身中,不会有变动。正常的 Java 代码都可以在代码片段中出现。
② <%=…%>:表达式在页面上打印数据
表达式会在“真身”中输出,例如:<%=a%>,对应真身中的 out.print(a)。只能出现常量、变量,以及有返回值的方法调用,而且不能出现分号!即在 out.println() 的合法参数都可以!- <%=a%> 对应 out.println(a);
- <%=”a”%> 对应 out.println(“a”);
- <%=fun()%> 对应 out.println(fun());
- <%=a;%> 对应 out.println(a;),所以出错;
- <%=System.out.println(“hello”)%> 对应out.println(System.out.println(“hello”)),所以出错。
③ <%!..%>:声明
声明对应 “真身” 中的属性和方法!
以上操作完整源码
index
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<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>
<form action="/jsp_demo1/index.jsp" method="post">
行:<input type="text" name="row"><br> 列:<input type="text"
name="col"><br>
<br> <input type="submit" value="打印">
</form>
<hr>
<%
String row = request.getParameter("row");
String col = request.getParameter("col");
Integer rowInt = 1;
Integer colInt = 1;
if(row != null){
rowInt = new Integer(row);
}
if(col != null){
colInt = new Integer(col);
}
%>
<table border="1px;">
<%
for(int i = 1; i <= rowInt; i++){
%>
<tr>
<%
for(int j = 1; j <= colInt; j++){
%>
<td>
<%
out.print(i*j);
%>
</td>
<%
}
%>
</tr>
<%
}
%>
</table>
</body>
</html>
index1
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<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>
<form action="/jsp_demo1/index1.jsp" method="post">
row:<input type="text" name="row"><br>
col:<input type="text" name="col"><br>
<input type="submit" value="print">
</form>
<hr>
<%
String row = request.getParameter("row");
String col = request.getParameter("col");
Integer rowInt = 1;
Integer colInt = 1;
if(row != null){
rowInt = new Integer(row);
}
if(col != null){
colInt = new Integer(col);
}
%>
<table border="1px;">
<%
for(int i = 1; i <= rowInt; i++){
%>
<tr>
<%
for(int j = 1; j <= colInt; j++){
%>
<td>
<%=i*j %>
</td>
<%
}
%>
</tr>
<%
}
%>
</table>
</body>
</html>
index2
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<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>
<%!
public static final String driver = "com.mysql.jdbc.Driver";
public int add(int i, int j){
return i*j;
}
%>
<%
String name = "weiyuxuan";
%>
<%=driver %>
<%=add(10, 10) %>
<%=name %>
</body>
</html>
工程文件下载
如有错误,欢迎指正!