四大指令元素
1.页面指令
页面指令用来定义jsp文件中的全局属性。一个页面可以包含多个页面指令
1 <%@ page 2 定义要使用的脚本语言:[language="java"] 3 需要引入的包:[import="java.util.Date, java.util.ArrayList"] 4 JSP字符编码,页面响应类型:[contentType="text/html";charset=CHARSET] 5 指定一个Http回话,该页面是否参与:[session="true|false"] 6 指定到客户输出缓冲流模式:[buffer="none|8kb|otherBuffSize"] 7 缓冲区满时,是否刷新:[autoFlush="true|false"] 8 是否能够多线程:[isThreadSafe="true|false"] 9 关于jsp页面信息:[info='text'] 10 当前页是否可为其他页面错误讯息页:[isErrorPage="true|false"] 11 该页面错误时使用的错误信息页:[errorPage="relativeURL"] 12 [extends="package.class"] 13 制定EL是否被忽略:[isELIgnored="true|false"] 14 页面字符编码:[pageEncoding="peinfo"] 15 16 %>
2.include指令
1 <%@ include file="jspfilename" %>
该包含为静态包含,file1包含file2,则将file2的代码插入至file1的指定位置作为一个jspfile,然后再编译。两个文件中变量会复用。
3.taglib
我不懂
4.表达式语言
参考其他人的吧
三大脚本元素
1.声明 <%! %>
声明是一段java代码,可以用来定义类或方法
1 <%! 2 class Student{ 3 private int id; 4 private String name; 5 6 public Student(){} 7 8 public Student(int id, String name){ 9 this.id = id; 10 this.name = name; 11 } 12 13 public String getName(){ 14 return name; 15 } 16 17 public int getId(){ 18 return id; 19 } 20 } 21 %>
2.scriplets <% %>
jsp页面处理请求时执行的代码,可以产生输出,也可以时流程控制语句
<% Student s1 = new Student(1,"pig"); out.println(s1.getName()); %>
2.scriplets <%= some java expression %>
<%=s1.getId() %>
八大动作元素
1.param <jsp:param name="paramName" value="paramValue">
jsp:param操作被用来以“名-值”对的形式为其他标签提供附加信息,通常与<jsp:include> <jsp:forward> <jsp:plugin>组合使用。
2.include <jsp:include page="fiilename" flush="true">
jsp:include操作允许在请求时间内,让现成的jsp页面中包含静态或动态资源。被包含的对象只有对jsp writer对象的访问权,并且不能设置头或cookie。如果页面输出是缓冲的,那么缓冲区的刷新要优于包含的刷新。
实例1.
<!-- copyRight.jsp -->>
<hr/>
©2012 Slowalker Lee
<hr/>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.util.Date" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <%=new Date() %> <jsp:include page="copyRight.jsp"/> </body> </html>
实例2.
主页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:include page="tow.jsp">
<jsp:param name="name" value="slowalker"/>
</jsp:include>
</body>
</html>
<!-- 被包含页面 --> <%="the parameter is " + request.getParameter("name")%>
运行结果:
the parameter is slowalker
3. <jsp:forward page="filename">
将请求转发到另一个页面。
验证登录的例子。
confirmLogIn.jsp
<!--confirmLogIn.jsp 登录的主界面-->
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form method="post" action="checkLoginIn.jsp"> <table> <tr> <td>user name:</td><td><input type="text" name="name"></td> </tr> <tr> <td>password :</td><td><input type="password" name="passwd"></td> </tr> <tr><td><input type="submit" value="submit"></td></tr> </table> </form> </body> </html>
<!--登录检查 后台执行 --> <% int a = 0; try{ a = Integer.parseInt(request.getParameter("passwd")); }catch (NumberFormatException e){} if (request.getParameter("name").equals("slowalker") && (a == 123)){ %> <jsp:forward page="success.jsp"></jsp:forward> //账户密码正确,跳转至成功界面 <% }else{ %> <jsp:forward page="confirmLogIn..jsp"></jsp:forward> //账号密码不正确,跳转至登录界面重新填写账号密码 <% } %>
<!-- 登录成功界面-->
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>success</h1>
</body>
</html>
4.<jsp:useBean>
<jsp:useBean id="id" scope="page|request|session|application" typeSpec>
id:一个大小写相关的名字,在所定义的范围中确认Bean的变量
/ page:能够在包含<jsp:useBean>标签的JSP文件以及此文件中的所有静态包含文件使用Bean
| request:在请求范围内使用有效 request.getAttribute(name) name是bean实例化的名字
scope:此对象可使用的范围 { session.getValues(name) name是bean实例化的名字
| session:从创建Bean开始,就可以在session有效范围内使用这个Bean,这个Bean对JSP来说是共享的。
\ application:从创建Bean开始,就可以在aplication有效范围内使用这个Bean,这个Bean对JSP来说是共享的。
application对象在应用服务器启动时创建,直至服务器关闭。
application.getAttribute(name) name是bean实例化的名字
/ class="className" bean的类路径和类名,这个class不能是抽象的。必须有一个公用的无参构造器。
| class="className" type="typeName" 使用instantiate方法从一个class中实例化一个Bean,同时可以指定Bean类型
typeSpec {
| beanName="beanName" type="typeName"
\ type="typeName"
1 package com.servlet.test; 2 3 public class TestBean { 4 public String userName; 5 public String password; 6 public int age; 7 8 public TestBean() {} //Bean中必须有一个无参构造器 9 10 public String getUserName() { 11 return userName; 12 } 13 14 public void setUserName(String userName) { 15 this.userName = userName; 16 } 17 18 public String getPassword() { 19 return password; 20 } 21 22 public void setPassword(String password) { 23 this.password = password; 24 } 25 26 public int getAge() { 27 return age; 28 } 29 30 public void setAge(int age) { 31 this.age = age; 32 } 33 34 35 }
1 <!--regisiter.html--> 2 <!DOCTYPE html> 3 <html> 4 <head> 5 <meta charset="UTF-8"> 6 <title>Insert title here</title> 7 </head> 8 <body> 9 <form method="post" action=register.jsp> 10 <table> 11 <tr> 12 <td>姓名:<input name="userName" type="text"/></td> <!--此处的userName,password,age必须和Bean中定义的属性的大小写一致--> 13 </tr> 14 <tr> 15 <td>密码:<input name="password" type="password"/></td> 16 </tr> 17 <tr> 18 <td>年龄:<input name="age" type="text"/></td> 19 </tr> 20 <tr> 21 <td><input type="submit" value="submit"></td> 22 </tr> 23 </table> 24 </form>> 25 </body> 26 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <jsp:useBean id="user" scope="page" class="com.servlet.test.TestBean" />
<!-- 实例化一个名为user的TestBean类的对象,有效范围为本页 -->
5 //<jsp:setProperty property="*" name="user"/> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 注册成功<hr> 13 <hr> 14 使用Bean的属性方法:<br> 15 用户名:<%=user.getUserName() %> 16 密码:<%=user.getPassword() %> 17 年龄:<%=user.getAge() %> //此处的用户名 密码 年龄均为之前填写的值 18 19 </body> 20 </html>
关于Bean的猜想:调用一个包含Bean的页面时,<jsp:useBean id="user" scope="page" class="com.servlet.test.TestBean" /> 此句实例化出一个名为user的TestBean对象
没有搞懂useBean的执行过程
5.setProperty
6.getProperty
7.plugin
8.fallback