<script>
function fun1()
{
var html = document.documentElement;
// alert(html.nodeName);
if (html.hasChildNodes())
{
// alert("Y");
}
else
{
/// alert("N");
}
var node1 = html.firstChild;
var node2 = node1.nextSibling;
var pElement = document.createElement("p");
var text = document.createTextNode("Hello World");
pElement.appendChild(text);
node2.appendChild(pElement);
}
</script>
2013年10月1日 星期二
2013年9月29日 星期日
Json 傳遞參數
<!DOCTYPE html>
<html>
<head>
<meta charset="BIG5">
<title>Insert title here</title>
<script type="text/javascript" src="json.js"></script>
<script>
var people =
{ "programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance.com" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "jason@servlets.com" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "elharo@macfaq.com" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
]
};
alert(people.programmers[0].firstName);
var stuents=new Object();
stuents.name="egg";
stuents.scope=100;
alert(stuents.toJSONString());
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<meta charset="BIG5">
<title>Insert title here</title>
<script type="text/javascript" src="json.js"></script>
<script>
var people =
{ "programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance.com" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "jason@servlets.com" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "elharo@macfaq.com" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
]
};
alert(people.programmers[0].firstName);
var stuents=new Object();
stuents.name="egg";
stuents.scope=100;
alert(stuents.toJSONString());
</script>
</head>
<body>
</body>
</html>
2013年9月25日 星期三
DOM的寫法-
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<jsp:useBean id="mylist" scope="page" class="com.shaojung.shopping.CategoryDAO" />
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
pageContext.setAttribute("catlist", mylist.getlistbyMainID(0));
%>
<!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=utf-8">
<title>Insert title here</title>
<script language="javascript" type="text/javascript">
var request = new XMLHttpRequest();
function funclick()
{
var id = document.getElementById("MainID").value;
request.open("GET", "GetSubCategory.jsp?ID=" + id, false);
request.onreadystatechange = updatePage;
request.send(null);
}
function updatePage() {
if (request.readyState == 4)
if (request.status == 200)
{
var ops = request.responseText.split('|');
var selectbox=document.getElementById("SubID");
for (var j=ops.length-1;j>=0;j--)
{
selectbox.remove(j);
}
for (var i=0;i<ops.length;i++)
{
var new_option = new Option(ops[i], i);
selectbox.options.add(new_option);
}
}
// document.getElementById("show").innerHTML = request.responseText;
}
</script>
</head>
<body>
<p>
<label for="MainID">分類</label>
<select name="MainID" id="MainID" onchange="funclick()">
<c:forEach items="${catlist}" var="item">
<option value="${item.ID}">${item.CName}</option>
</c:forEach>
</select>
</p>
<p><span id="show"></span></p>
<label for="SubID">子分類</label>
<select name="SubID" id="SubID">
</select>
</body>
</html>
pageEncoding="utf-8"%>
<jsp:useBean id="mylist" scope="page" class="com.shaojung.shopping.CategoryDAO" />
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
pageContext.setAttribute("catlist", mylist.getlistbyMainID(0));
%>
<!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=utf-8">
<title>Insert title here</title>
<script language="javascript" type="text/javascript">
var request = new XMLHttpRequest();
function funclick()
{
var id = document.getElementById("MainID").value;
request.open("GET", "GetSubCategory.jsp?ID=" + id, false);
request.onreadystatechange = updatePage;
request.send(null);
}
function updatePage() {
if (request.readyState == 4)
if (request.status == 200)
{
var ops = request.responseText.split('|');
var selectbox=document.getElementById("SubID");
for (var j=ops.length-1;j>=0;j--)
{
selectbox.remove(j);
}
for (var i=0;i<ops.length;i++)
{
var new_option = new Option(ops[i], i);
selectbox.options.add(new_option);
}
}
// document.getElementById("show").innerHTML = request.responseText;
}
</script>
</head>
<body>
<p>
<label for="MainID">分類</label>
<select name="MainID" id="MainID" onchange="funclick()">
<c:forEach items="${catlist}" var="item">
<option value="${item.ID}">${item.CName}</option>
</c:forEach>
</select>
</p>
<p><span id="show"></span></p>
<label for="SubID">子分類</label>
<select name="SubID" id="SubID">
</select>
</body>
</html>
2013年9月23日 星期一
sql-convert語法
SELECT 訂單.訂單序號,convert(char,訂單.日期,111),客戶.客戶名稱 from 訂單 inner join 客戶 on 訂單.客戶編號=客戶.客戶編號
convert有三個引數,
convert有三個引數,
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
style是用數字代表,可用google用msql convert關鍵字查詢:
11 = yy/mm/dd
111 = yyyy/mm/dd
2013年9月17日 星期二
JSP語法-連結資料庫
<%@ page language="java" contentType="text/html; charset=BIG5"
pageEncoding="BIG5"%>
<%@page import="java.sql.*"%>
<!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=BIG5">
<title>Insert title here</title>
</head>
<body>
<%
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;"
+ "databaseName=Students;user=sa;password=123456;";
Connection con = DriverManager.getConnection(connectionUrl);
if (con.isClosed()) {
out.println("jdbs is closed!關閉中");
} else {
out.println("jdbs is opened!開啟中");
}
con.close();
if (con.isClosed()) {
out.println("jdbs is closed!");
} else {
out.println("jdbs is opened!");
}
%>
</body>
</html>
pageEncoding="BIG5"%>
<%@page import="java.sql.*"%>
<!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=BIG5">
<title>Insert title here</title>
</head>
<body>
<%
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;"
+ "databaseName=Students;user=sa;password=123456;";
Connection con = DriverManager.getConnection(connectionUrl);
if (con.isClosed()) {
out.println("jdbs is closed!關閉中");
} else {
out.println("jdbs is opened!開啟中");
}
con.close();
if (con.isClosed()) {
out.println("jdbs is closed!");
} else {
out.println("jdbs is opened!");
}
%>
</body>
</html>
2013年9月16日 星期一
SQL語法--表格關聯、條件篩選 、排列順序、群組
SELECT 訂單.訂單序號, 訂單.日期, 客戶.客戶名稱 FROM 訂單 inner join 客戶 on 客戶.客戶編號=訂單.客戶編號 where 客戶名稱='一品書店' order by 訂單.訂單序號 asc;
表格關聯
from 表格1名稱 inner join 表格2名稱 on 表格1.客戶編號欄位=表格2.客戶編號欄位
條件篩選 (客戶名稱為一品書局):
where 客戶名稱='一品書店'
排列順序(訂單序號排序):
order by 訂單.訂單序號 ----由小到大
order by 訂單.訂單序號 desc ----由大到小
群組(Sum.....GROUP BY...)
SELECT 訂單.客戶編號, Sum(明細加小記.小記) AS 小記之總計
FROM 明細加小記 INNER JOIN 訂單 ON 明細加小記.訂單序號 = 訂單.訂單序號
GROUP BY 訂單.客戶編號;
表格關聯
from 表格1名稱 inner join 表格2名稱 on 表格1.客戶編號欄位=表格2.客戶編號欄位
條件篩選 (客戶名稱為一品書局):
where 客戶名稱='一品書店'
排列順序(訂單序號排序):
order by 訂單.訂單序號 ----由小到大
order by 訂單.訂單序號 desc ----由大到小
群組(Sum.....GROUP BY...)
SELECT 訂單.客戶編號, Sum(明細加小記.小記) AS 小記之總計
FROM 明細加小記 INNER JOIN 訂單 ON 明細加小記.訂單序號 = 訂單.訂單序號
GROUP BY 訂單.客戶編號;
2013年9月14日 星期六
JSP-javaBean、setProperty演練:改寫登入流程
登入流程:login.jsp--->check.jsp--->ok.jsp或fail.jsp
check.jsp使用ChkLogin.java這支程式。
login.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="check.jsp">
<p>
<label for="username">帳號:</label>
<input type="text" name="username" id="username" value="${cookie.username.value}"/>
</p>
<p>密碼:
<input type="password" name="password" id="password" />
</p>
<p>
<input type="submit" name="button" id="button" value="送出" />
</p>
</form>
</body>
</html>
check.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<jsp:useBean id="objCheck" scope="page" class="t0915.ChkLogin" />
<jsp:setProperty name="objCheck" property="userName" param="username" />
<jsp:setProperty name="objCheck" property="pwd" param="password" />
<%
if (objCheck.check())
{
session.setAttribute("login", "ok");
session.setAttribute("username", objCheck.getUserName());
Cookie myCookie = new Cookie("username", objCheck.getUserName());
myCookie.setMaxAge(60);
response.addCookie(myCookie);
response.sendRedirect("ok.jsp");
}
else
{
response.sendRedirect("fail.jsp");
}
%>
ChkLogin.java
package t0915;
public class ChkLogin {
private String name;
private String pwd;
public ChkLogin()
{
}
public void setUserName(String u)
{
name = u;
}
public String getUserName()
{
return name;
}
public void setPwd(String p)
{
pwd = p;
}
public String getPwd()
{
return pwd;
}
public boolean check()
{
if (name.equals("abc") && pwd.equals("123"))
return true;
else
return false;
}
}
check.jsp使用ChkLogin.java這支程式。
login.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="check.jsp">
<p>
<label for="username">帳號:</label>
<input type="text" name="username" id="username" value="${cookie.username.value}"/>
</p>
<p>密碼:
<input type="password" name="password" id="password" />
</p>
<p>
<input type="submit" name="button" id="button" value="送出" />
</p>
</form>
</body>
</html>
check.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<jsp:useBean id="objCheck" scope="page" class="t0915.ChkLogin" />
<jsp:setProperty name="objCheck" property="userName" param="username" />
<jsp:setProperty name="objCheck" property="pwd" param="password" />
<%
if (objCheck.check())
{
session.setAttribute("login", "ok");
session.setAttribute("username", objCheck.getUserName());
Cookie myCookie = new Cookie("username", objCheck.getUserName());
myCookie.setMaxAge(60);
response.addCookie(myCookie);
response.sendRedirect("ok.jsp");
}
else
{
response.sendRedirect("fail.jsp");
}
%>
ChkLogin.java
package t0915;
public class ChkLogin {
private String name;
private String pwd;
public ChkLogin()
{
}
public void setUserName(String u)
{
name = u;
}
public String getUserName()
{
return name;
}
public void setPwd(String p)
{
pwd = p;
}
public String getPwd()
{
return pwd;
}
public boolean check()
{
if (name.equals("abc") && pwd.equals("123"))
return true;
else
return false;
}
}
2013年9月13日 星期五
stil-fmt<標籤函式庫>
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
<jsp:useBean id="now" class="java.util.Date"/>
<p><fmt:formatDate value="${now}" type="both" dateStyle="full" timeStyle="full"/></p>
<p><fmt:formatDate value="${now}" type="date" dateStyle="medium"/></p>
<p><fmt:formatDate value="${now}" type="date" pattern="MM-dd-YYYY"/></p>
</body>
</html>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
<jsp:useBean id="now" class="java.util.Date"/>
<p><fmt:formatDate value="${now}" type="both" dateStyle="full" timeStyle="full"/></p>
<p><fmt:formatDate value="${now}" type="date" dateStyle="medium"/></p>
<p><fmt:formatDate value="${now}" type="date" pattern="MM-dd-YYYY"/></p>
</body>
</html>
jstl-functions標籤函式庫
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
${fn:contains("this is a test","ths")}
<P>${fn:substring("abcdefg",2,5)}</P>
<P>${fn:substringAfter("this is a test","a")}</P>
<P>${fn:substringBefore("this is a test","a")}</P>
</body>
</html>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
${fn:contains("this is a test","ths")}
<P>${fn:substring("abcdefg",2,5)}</P>
<P>${fn:substringAfter("this is a test","a")}</P>
<P>${fn:substringBefore("this is a test","a")}</P>
</body>
</html>
jstl-編碼問題設定
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<c:set value="OK" var="login" scope="session"/>
<fmt:requestEncoding value="utf-8"/>
<c:choose>
<c:when test="${param.username != '' && param.password !=''}">
${param.username}
<c:redirect url="ok.jsp"/>
</c:when>
<c:otherwise>
<c:redirect url="fail.jsp"/>
</c:otherwise>
</c:choose>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<c:set value="OK" var="login" scope="session"/>
<fmt:requestEncoding value="utf-8"/>
<c:choose>
<c:when test="${param.username != '' && param.password !=''}">
${param.username}
<c:redirect url="ok.jsp"/>
</c:when>
<c:otherwise>
<c:redirect url="fail.jsp"/>
</c:otherwise>
</c:choose>
stil-for&forEach& for Tokens迴圈演練
綠色字體是for迴圈,輸出為1~10
紅色字體是 forEach,透過script定義陣列變數,用request參數設定內容。輸出為a,b,c,d。
紫色字體是 forEach,輸出為b,d。
藍色字體是 for Tokens,輸出為台北 高雄 屏東 林口 台中 台東
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String[] myarray={"a","b","c","d"};
request.setAttribute("myarrays",myarray);
%>
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
<c:forEach var="myitems" begin="1" end="10" >
${myitems}
</c:forEach>
<c:forEach items="${myarrays}" var="myitems2">
<P> ${myitems2}</P>
</c:forEach>
forEach2:
<c:forEach items="${myarrays}" var="myitems3" begin="1" end="3" step="2">
<P> ${myitems3}</P>
</c:forEach>
for Tokens:
<c:forTokens var="myitems4" items="台北,高雄,屏東,林口,台中,台東" delims=",">
<P> ${myitems4}</P>
</c:forTokens>
</body>
</html>
2013年9月12日 星期四
stil-choose改寫if..else法
choose內的when.....otherwise類似於script的if...else 。
原有程式
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("abc") && password.equals("123"))
{
session.setAttribute("login", "ok");
response.sendRedirect("ok.jsp");
}
else
{
response.sendRedirect("fail.jsp");
}
%>
可改寫為
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set value="OK" var="login" scope="session"/>
<c:choose>
<c:when test="${param.username != '' && param.password !=''}">
<c:redirect url="ok.jsp"/>
</c:when>
<c:otherwise>
<c:redirect url="fail.jsp"/>
</c:otherwise>
</c:choose>
原有程式
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("abc") && password.equals("123"))
{
session.setAttribute("login", "ok");
response.sendRedirect("ok.jsp");
}
else
{
response.sendRedirect("fail.jsp");
}
%>
可改寫為
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set value="OK" var="login" scope="session"/>
<c:choose>
<c:when test="${param.username != '' && param.password !=''}">
<c:redirect url="ok.jsp"/>
</c:when>
<c:otherwise>
<c:redirect url="fail.jsp"/>
</c:otherwise>
</c:choose>
jstl-if演練
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
<c:if test="${4>2}">
<c:out value="ok"/>
</c:if>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
<c:if test="${4>2}">
<c:out value="ok"/>
</c:if>
</body>
</html>
jstl-param演練
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="get.jsp">
<p>
<label for="username"></label>
<input type="text" name="username" id="username" />
</p>
<p>
<input type="submit" name="button" id="button" value="送出" />
</p>
</form>
</body>
</html>
---------------------------------------------------------------------------------------------------
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
${param.username} //用param可以將username的變數值呼叫出來
</body>
</html>
jstl-import演練
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
<c:import url="intstr.jsp" /> //單獨匯入intstr.jsp
<c:import url="inc.jsp" var="str"/>
<p>mystr: ${str}</p> //透過參數呼叫網頁
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
<c:import url="intstr.jsp" /> //單獨匯入intstr.jsp
<c:import url="inc.jsp" var="str"/>
<p>mystr: ${str}</p> //透過參數呼叫網頁
</body>
</html>
jstl-url設定演練。
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
<a href="<c:url value="004.jsp">
<c:param value="123" name="id" />
</c:url>">連到004 </a>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
<a href="<c:url value="004.jsp">
<c:param value="123" name="id" />
</c:url>">連到004 </a>
</body>
</html>
jstl用Java script叫出
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set value="my test!" var="tt" scope="request" />
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
<%
String str=(String)request.getAttribute("tt");
out.println(str);
%>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set value="my test!" var="tt" scope="request" />
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
<%
String str=(String)request.getAttribute("tt");
out.println(str);
%>
</body>
</html>
標籤庫jstl-匯入與使用
JSTL是另一個標準規範,本身並非在JSP的規範當中,所以必須另外下載JSTL的API與實作。
以下兩個為Tomcat 的函式庫路徑
C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\WEB-INF\lib (針對單一網站使用的函式庫)
將下載的jstl.jar放入上述第一個路徑即可。
要使用JSTL核心標籤庫,必須在JSP網頁上,使用taglib指示元素定義前置文件與uri參考。
------------------------------------------------------------
<body>
<c:set value="這是預設的值" var="str"/>
${str} //會印出str變數值
<c:out value="test"/>
</body>
以下兩個為Tomcat 的函式庫路徑
C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\WEB-INF\lib (針對單一網站使用的函式庫)
將下載的jstl.jar放入上述第一個路徑即可。
要使用JSTL核心標籤庫,必須在JSP網頁上,使用taglib指示元素定義前置文件與uri參考。
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
------------------------------------------------------------
<body>
<c:set value="這是預設的值" var="str"/>
${str} //會印出str變數值
<c:out value="test"/>
</body>
session隱含物件範例
session是存放在伺服器端。
session1.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%
session.setAttribute("login","ok");
%>
-------------------------------------------------------------------------------------------------------
session2.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
<%
String s=(String)session.getAttribute("login");
out.println(s);
%>
</body>
</html>
session1.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%
session.setAttribute("login","ok");
%>
-------------------------------------------------------------------------------------------------------
session2.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!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=utf-8" />
<title>無標題文件</title>
</head>
<body>
<%
String s=(String)session.getAttribute("login");
out.println(s);
%>
</body>
</html>
cookies隱含物件
cookies是儲存在客戶端。
cookies存活時間若沒有設定,瀏覽器關閉就會消失。
若有設定,則會以文字檔
cookies.jsp
<%
Cookie mycookie=new Cookie("username","egg");
mycookie.setMaxAge(60);
response.addCookie(mycookie);
%>
cookies_read.jsp
<body>
<%
Cookie[] cookies=request.getCookies();
out.println(cookies[0].getName());
%>
</body>
訂閱:
文章 (Atom)