(1)注释:
<!-- HTML 风格注释,它会发送到客户端 -->
<%-- JSP 风格注释,它不会发送到客户端 --%>
(2)表达式:
<%
String name = "Jack";
String msg = "欢迎来到本系统!";
%>
<br>
<%=name+", "+msg %>
(3)程序段:
<%
for(int i = 1; i <= 10; i++) {
out.print(i+"times<br>");
}
%>
<%
for(int i = 1; i <= 10; i++) {
%>
<%=i %>timessss<br>
<%
}
%>
(4)声明:
<%
out.print(str);
%>
<%!
String str = "declaration!";
%>
(5)URL传值:
urlP1.jsp定义数值变量num,显示num的平方,要求点击链接,在urlP2.jsp显示num的立方.
urlP1.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String str = "12";
int num = Integer.parseInt(str);
%>
这个数字的平方是:<%=num*num %><hr>
<a href="urlP2.jsp?num=<%=num %>">前往urlP2</a>
urlP2.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String str = request.getParameter("num");
int num = Integer.parseInt(str);
%>
这个数的立方:<%=num*num*num %><hr>
(6)导入包:
①<%@ page import="包名.类名" %>
②<%@ page import="包名.*" %>
③<%@ page import="包名.类1" %>
<%@ page import="包名.类2" %>
④<%@ page import="包名.类1,包名.类2" %>
例子:显示用户访问时间
<%@ page language="java" import="java.util.Date" contentType="text/html;charset=GB18030" %>
<html>
<body>
你的登录时间是<%= new Date() %>
</body>
</html>
(7)设定字符集:
<%@page pageEncoding="编码类型" %>
(8)设定错误页面:
errorPage="pageTest2_error.jsp"
pageTest2.jsp:
<%@ page contentType="text/html;charset=GB18030" errorPage="pageTest2_error.jsp"%>
<html>
<body>
<%
int num1 = 10;
int num2 = 0;
int num3 = num1/num2;
%>
</body>
</html>
isErrorPage="true"
<%@ page contentType="text/html;charset=GB18030" isErrorPage="true"%>
<html>
<body>
<%
out.print("网页出现数学运算异常!");
%>
</body>
</html>
(9)设定MIME类型和字符编码:
<%@ page contentType="MINE类型;charset=字符编码"%>
(10)include指令:
例子:includeTest1.jsp包含info.jsp
info.jsp:
<%@ page contentType="text/html;charset=GB18030"%>
<hr>
<center>
公司电话号码:010-89574895,欢迎来电!
</center>
includeTest1.jsp:
<%@ page language="java" contentType="text/html;charset=GB18030"%>
<html>
<body>
<%
out.print("欢迎来到本系统!");
%>
<br>
<%@ include file="info.jsp" %>
</body>
</html>
(11)JSP动作:
①jsp:include。当页面被请求时引入一个文件。
<jsp:include page="文件名" />
②jsp:forward。将请求转到另外一个页面。
<jsp:forward page="文件名" />
例子:
<%@ page language="java" contentType="text/html;charset=GB18030"%>
<html>
<body>
<jsp:forward page="pageTest1.jsp" />
</body>
</html>
(12)单一表单元素数据的获取:
例子1:
textForm.jsp:
<%@ page language="java" contentType="text/html;charset=GB18030" %>
<html>
<body>
<form action="textForm_result.jsp">
请您输入学生的模糊资料:<br>
<input name="stuname" type="text">
<input type="submit" value="查询">
</form>
</body>
</html>
textForm_result.jsp:
<%@ page language="java" contentType="text/html;charset=GB18030"%>
<html>
<body>
<%
//textForm_result.jsp
String stuname = request.getParameter("stuname");
out.print("输入的查询关键字为:"+stuname);
%>
</body>
</html>
例子2:
radioForm.jsp:
<%@ page language="java" contentType="text/html;charset=GB18030"%>
<html>
<body>
请您输入自己的信息进行注册
<form action="radioForm_result.jsp" method="post">
请您输入账号:<input name="account" type="text"><br>
请您输入密码:<input name="password" type="password"><br>
请您选择性别:
<input name="sex" type="radio" value="boy" checked>男
<input name="sex" type="radio" value="girl">女<br>
<input type="submit" value="注册">
</form>
</body>
</html>
radioForm_result.jsp:
<%@ page language="java" contentType="text/html;charset=GB18030"%>
<html>
<body>
<%
String sex = request.getParameter("sex");
out.print("性别为:"+sex);
%>
</body>
</html>
例子3:
selectForm.jsp:
<%@ page language="java" contentType="text/html;charset=GB18030"%>
<html>
<body>
请您输入自己的信息进行注册
<form action="selectForm_result.jsp" method="post">
请您输入账号:<input name="account" type="text"><br>
请您输入密码:<input name="password" type="password"><br>
请您选择家乡:
<select name="home">
<option value="beijing">北京</option>
<option value="shanghai">上海</option>
<option value="guangdong">广东</option>
</select>
<input type="submit" value="注册">
</form>
</body>
</html>
selectForm_result.jsp:
<%@ page language="java" contentType="text/html;charset=GB18030"%>
<html>
<body>
<%
String home = request.getParameter("home");
out.println("家乡为:"+home);
%>
</body>
</html>
(13)捆绑表单元素数据的获取:
例子:
checkForm.jsp:
<%@ page language="java" contentType="text/html;charset=GB18030"%>
<html>
<body>
请您输入自己的信息进行注册
<form action="checkForm_result.jsp" method="post">
请您选择您的爱好:
<input name="fav" type="checkbox" value="sing">唱歌
<input name="fav" type="checkbox" value="dance">跳舞
<input name="fav" type="checkbox" value="ball">打球
<input name="fav" type="checkbox" value="game">打游戏<br>
<input type="submit" value="注册">
</form>
</body>
</html>
checkForm_result.jsp:
<%@ page language="java" contentType="text/html;charset=GB18030"%>
<html>
<body>
<%
String[] fav = request.getParameterValues("fav");
out.println("爱好为:");
for(int i = 0; i < fav.length; i++) {
out.println(fav[i]);
}
%>
</body>
</html>
(14)隐藏表单:
formP1.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String str = "12";
int num = Integer.parseInt(str);
%>
该数字的平方为:<%=num*num %><hr>
<form action="formP2.jsp" method="post">
<input type="hidden" name="num" value="<%=num %>">
<input type="submit" value="到达P2">
</form>
formP2.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String str = request.getParameter("num");
int num = Integer.parseInt(str);
%>
该数字的立方为:<%=num*num*num %><hr>
(15)中文乱码问题:
①对post和get都有效:
String stuname = request.getParameter("stuname");
stuname = new String(stuname.getBytes("ISO-8859-1"),"GB18030");
②只对post有效,get仍然出现乱码:
request.setCharacterEncoding("gb18030");
String stuname = request.getParameter("stuname");