1.编译指令
include,page,taglib
2.脚本语法
注释:<!-- -->, <% --%>
申明:<%! %>
表达式:<%= %>
Java脚本:<% %>
3.动作语法
<jsp:forward>,<jsp:include>,<jsp:plugin>,
<jsp:getProperty>,<jsp:setProperty>,<jsp:useBean>
编译指令格式:
<%@Directive 属性=“属性值”%>
<%@page language=“script language”|
extends=“className”|
import=“importList”|
buffer=“none|kb size”| --none:不缓冲,默认8k
session=“true|false”| --是否可以使用session,默认true
autoFlush=“true|false” --缓冲器是否自动清除,默认true
isThreadSafe=“true|false”| --默认false(永远不要设成true)
info=“infoText”| --任何字符
errorPage=“errorPageUrl”|
isErrorPage=“true|false”|
contentType=“contentTyepInfo”|
pageEncoding=“gb2312”
%>
两中include比较:
1.静态导入
<%@include file="fileURL"%><%@include file="logo.jsp"%>
是在servlet引擎转译时,就把此文件内容包含了进去(两个文件的源代码整合到一起,全部放在_jspService方法中),
所以只生成一个servlet,即只生成一个java和class,所以两个页面不能有同名的变量。
运行效率高一点点。耦合性较高,不够灵活。
2.动态导入
<jsp:include page=“URLSpec” flush=“true”/>
<jsp:include page="logo.jsp"></jsp:include>
是在servlet引擎转译后,请求的此页面,所以生成了两个servlet,即两个java和class,所以可以有不同的同名变量。
生成两个servlet,相当于两个类之间的调用,耦合性较低,非常灵活。
Include不能带参数, 而<jsp:include>可以
<jsp:include page=“URLSpec” flush=“true”/>
<jsp:include page=“URLSpec” flush=“true”>
<jsp:param name=“ParamName” value=“paramValue”/>
</jsp:include>
jsp:param用来设定include文件时的参数和对应的值
<jsp:include page='<%=request.getParameter("includepage")%>'></jsp:include><jsp:include page="1.jsp">
<jsp:param value="10" name="kk"/>
</jsp:include>
<jsp:include page="1.jsp?kk=10"></jsp:include>
申明:<%! %>
<%!int hh = 3;//相当于java里的成员变量
%>
表达式:<%= %>
<%=dd%>
相当于<% out.print("申明"); %>
<jsp:forward>
<jsp:forward> 标签后的代码不会被执行
使用同一个request
forward后的语句不会继续发送给客户端
速度快
服务器内部转换,
可以传参数
response.sendRedirect
是不同的request
send后的语句会继续执行,除非return
速度慢
需要到客户端的往返,可以转到任何页面
可以传参数,直接写在url后面
jsp:useBean示例:
JSP:
<%@ page contentType="text/html; charset=gbk" import="com.exercise.jsp.User" %>
<html>
<head></head>
<body>
<%--
<jsp:useBean id="u1" class="com.exercise.jsp.User" scope="request"></jsp:useBean>
<jsp:setProperty property="uname" name="u1" value="lisi"/>
<jsp:setProperty property="pwd" name="u1" value="aisino"/>
<jsp:getProperty property="pwd" name="u1"/>
--%>
<% //下面代码与上面代码效果一样,一般不用jsp:useBean
request.setAttribute("u1",new User());
User user = (User)request.getAttribute("u1");//作用域的寻找范围(由小到大),pageContext/request/session/application
user.setUname("lisi");
user.setPwd("aisino");
out.println(user.getPwd());
%>
</body>
</html>
JavaBean:
package com.exercise.jsp;
public class User {
private String uname;
private String pwd;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public User(String uname, String pwd){
super();
this.uname = uname;
this.pwd = pwd;
}
public User(){
}
}
jsp中的basePath和path:
sample.jsp
1.jsp
路径:ExerciseJSP/WebRoot/login/sample.jsp
路径:ExerciseJSP/WebRoot/1.jsp
跳转示例:
sample.jsp如下
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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>
<a href="1.jsp">跳转</a>
</body>
</html>
[bashPath:是以当前项目的根目录为准。如:http://localhost:8888/ExerciseJSP/
所以当前的跳转<a href="1.jsp">就可以了]
第二种:
sample.jsp如下
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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>
<a href="../1.jsp">跳转</a>
</body>
</html>
[这里没有定义根路径,而sample.jsp在login目录下,1.jsp在webroot根目录下,
所以需要这么写<a href="../1.jsp">]
一般项目里用绝对路径,能避免很多麻烦
(在jsp里,可以用 request.getContextPath()方式来拿到webapp的路径
或者用myeclipse经常用的,指定basePath)
basePath和path可参考资料:http://blog.sina.com.cn/s/blog_73e110650100yw28.html
下面提供一共用jsp_servlet写的一个简单登录页面。下载地址(需要密码,请发邮件han_huayi@163.com):
http://pan.baidu.com/share/link?shareid=1908802552&uk=3409027827