一、JSP簡介
JSP本身就是一個servlet;直接在HTML中內嵌JSP代碼;JSP程序由JSP Engine先轉換成servlet代碼,接着編譯成類文件載入執行;只有當客戶端第一次請求JSP時,才需要將其轉換、編譯
HelloWorld.jsp
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Hello World JSP</title>
</head>
<body>
<%
out.println("Hello World!");
out.println(new java.util.Date());
%>
</body>
</html>
二、JSP的基本語法
1.JSP有四種語法:
1)JSP傳統語法。(使用<%%>嵌套java語言)
2)JSTL。(和標籤語言類似,沒有%)
3)JSF。(JSTL的替代品)
4)其他tablib(如struts)。
2.JSP的傳統語言:
1)Declaration。 2)Scriptlet。 3)Expression。 4)Comment。 5)Directives。 6)Action動作指令。 7)內置對象
Declaration 基本語法:<%! %>
在此聲明的變量、方法都會被保留成唯一的一份,直到JSP程序停止執行;類似java中的成員變量和成員方法。
示例:
<body>
<h1>JSP Declaration</h1>
<%!
int accessCount = 0; //此處聲明的是成員變量,每次刷新頁面,都會遞增1
%>
<%
int accessCount2 = 0; //此處聲明的是局部變量,每次刷新頁面,程序執行一次,就新生成一個變量,故每次刷新值都不會變。
%>
<h2>Access to page since server reboot:
<%= ++accessCount %>
<br />
<%= accessCount2 %>
<br />
</h2>
</body>
Scriptlet基本語法:<% 程序代碼區 %>
可以放入任何的java程序代碼
註釋格式: <%--... ... --%>
<%//... ...%>
<%/*... ...*/%>
示例:(鏈接後面跟參數示例 url?bgColor=red)
<!--
客戶端可以看見的html註釋
-->
<%
// java單行註釋,客戶端看不見
/*
java多行註釋
客戶端看不見
*/
String bgColor = request.getParameter("bgColor");
boolean hasColor;
if(bgColor == null) {
hasColor = false;
bgColor = "white";
} else {
hasColor = true;
}
%>
<body style = "background-color:<%= bgColor %>;">
<h1>Test BGColor</h1>
<p>
<%
if(hasColor) {
out.println("You have choosed " + bgColor + " as backgroud color");
} else {
out.println("You have the default backgroud color \"white\"");
}
%>
</p>
</body>
Expression 基本語法:<%=... ...%>
=後面必須是字符串變量或者可以被轉換成字符串的表達式;不需要以“;”結束;只有一行;
示例:Expressions.jsp
<body>
<h1>JSP Expressions</h1>
<p>Current time: <%= new Date() %></p>
<p>You Host Name: <%= request.getRemoteHost() %></p>
<p>You Session ID: <%= session.getId() %></p>
<p>The testParam is: <%= request.getParameter("testParam") %></p>
</body>
Directive 基本語法格式:<%@Directive屬性="屬性值"%>
directive(編譯指令)相當於在編譯期間的命令
常見的Directive:1)page 2)include 3)taglib
1)Directive — page
指明與JSP Container的溝通方式
基本格式:
<%@page language="script language"| --目前值只能取java,默認爲java。
extends="className"| --說明該JSP頁面從誰繼承,不需要指定。
import="importList"| --*!引入進來哪些包,相當於java的import語句
buffer="none|kb size"| --緩衝區,none爲不緩衝,默認8k的緩衝。
session="true|false"| --頁面能不能使用session,默認能使用。指定較少。
autoFlush="true|false"| --頁面輸出完是否flush到客戶端,默認true。
isThreadSafe="true|false"| --相當於servlet實現了singleThreadMode(已廢棄)
info="infoText"| --JSP的描述信息,很少用
errorPage="errorPageUrl"| --*!這個頁面出錯了,顯示哪個頁面。
isErrorPage="true|false"| --*!這個頁面是否是顯示錯誤的頁面
contentType="contentTypeInfo" --*!通常爲"text/html"
%>
示例:
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="ISO-8859-1" import="java.util.Date,java.text.*"
errorPage="ErrPage.jsp"
%>
2)Directive — include
將指定的JSP程序或者HTML文件包含進來
格式:<%@ include file="fileURL"%>
JSP Engine會在JSP程序的轉換時期先把file屬性設定的文件包含進來,然後開始執行轉換編譯工作。
不能向fileURL中傳參數。
3)Directive — taglib
後續項目中再講。
Action 基本語法:<jsp: .. /> 嚴格上來講它屬於jsp的第二種語法。
動作指令是在運行期間的命令
常見的:
jsp:useBean
jsp:setProperty
jsp:getProperty
jsp:include
jsp:forward
jsp:param
jsp:plugin //往網頁裏面插入一個applet小程序,基本不用了。
jsp:include/jsp:param
用於動態包含JSP程序或者HTML文件等
除非這個指令被執行,否則它是不會被Tomcat等JSP Engine編譯。
格式:
<jsp:include page="URLSpec" flush="true" />或者
<jsp:include page="URLSpec" flush="true">
<jsp:param name="ParamName" value="paramValue" />
</jsp:include>
與Directive include的區別:
1)include編譯指令是在JSP程序的轉換時期就將file屬性所指定的程序內容嵌入,然後再編譯執行;而jsp:include在轉換期間不會被編譯,只有在客戶端請求時如果被執行到纔會被動態編譯載入,所以效率較低
2)include編譯指令不能帶參數;而jsp:include可以
jsp:forword/jsp:param
用於將一個jsp的內容傳送到page所指定的JSP程序或者Servlet中處理
格式:
<jsp:forword page="URLSpec" flush="true" />或者
<jsp:forword page="URLSpec" >
<jsp:param name="ParamName" value="paramValue" />
</jsp:forword>forword的頁面和forward到的頁面用的是同一個request。
與此對應的是:
response.sendRedirect(區別,此爲跳轉到另一個鏈接上,不是同一個request;所以跳轉後的鏈接地址不是原來的url)
jsp:useBean
通過jsp:useBean,可以在JSP中使用定義好的Bean
基本用法:
<jsp:useBean id="beanName" scope="page|request|session|application"
class="package.BeanClass" type="typeName" />
<jsp:useBean ...>
<jsp:setProperty ...>
<jsp:getProperty ...>
</jsp:useBean>
示例:
<jsp:useBean id="cd" scope="page" class="bean.CounterBean" type="Object" />
/*相當於
<% Object cb = new bean.CounterBean();%>
*/
<jsp:setProperty name="cd" property="count" value="23" />
<jsp:setProperty name="cd" property="count" />
/*相當於
<% cd.setcount("23"); %>
<% cd.getcount; %>
*/
bean的放置目錄同servlet,連包一起放;
3.jsp_characterEncoding
<% request.setCharacterEncoding("GBK"); %>
或者在單個的顯示上用:
<%
String name = request.getParameter("name");
new String(name.getBytes("ISO8859_1"),"GBK");
%>
<%= new String(hello.getName().getBytes("ISO8859_1"),"GBK")%>
4.JSP內置對象
out
request
response
pageContext //用得很少
session
application
config //用得較少;一般指web.xml對於這個JSP或者它編譯成功之後的servlet的配置
exception
page //page裏面的所有東西可以直接使用,一般不用它
5.servlet和JSP的相互跳轉
JSP跳轉到Servlet:使用<jsp:forword>或者response.sendRedirect
Servlet跳轉到JSP:getServletConfig().getServletContext().getRequestDispatcher("/servlet_jsp/SercletUseJsp.jsp").forword(req,resp);