<html> <head> <title> Hello World </title> </head> <body> <% String s = "Eric"; %> <h1>Hello <%=s%></h1> </body> </html>
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=Big5"> <title> Hello Form </title> </head> <body> <form method="post" action="http://localhost:8080/test/Greeting.jsp"> <input type="text" value="老呂" name="data"> <input type="submit"> </form> </body> </html>
<%@ page language="java" contentType="text/html;charset=Big5" %>
<html>
<head>
<title>
Greetings
</title>
</head>
<body>
<% String s =
new String(request.getParameter("data").getBytes("ISO-8859-1"), "Big5");
%>
<h1>Hello <%=s%></h1>
<%
out.println("<h1>試試看 out 物件</h1>");
%>
</body>
</html>
<%@ page contentType="text/html;charset=Big5"
language="java"
import="java.sql.*" %>
<%
String dsn = request.getParameter("dsn");
String uid = request.getParameter("uid");
String pwd = request.getParameter("pwd");
// initialize query string
String aQuery = null;
Connection conn = null;
try
{
// load the JDBC-ODBC bridge driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// connect to Database
aQuery = "select * from Product";
conn = DriverManager.getConnection("jdbc:odbc:"+ dsn, uid, pwd);
%>
<html><head><title>
<%
out.println("Database " + dsn + " Query");
%>
</title></head><body>
<%
// Construct a SQL statement and submit it
Statement aStatement = conn.createStatement();
ResultSet rs = aStatement.executeQuery(aQuery);
// Get info about the query results
ResultSetMetaData rsmeta = rs.getMetaData();
int cols = rsmeta.getColumnCount();
// contruct table
out.println("<table border=\"1\">");
out.println("<tr>");
// Display column headers
for(int i=1; i<=cols; i++)
{
out.print("<th>");
out.print(rsmeta.getColumnLabel(i));
out.print("</th>");
}
out.println("\n</tr>");
// Display query results.
while(rs.next())
{
out.print("<tr>");
for(int i=1; i<=cols; i++)
{
out.print("<td>");
out.print(rs.getString(i));
out.print("</td>");
}
out.println("</tr>");
}
// Clean up
rs.close();
aStatement.close();
conn.close();
}
// a better exception handling can be used here.
catch (Exception e)
{
System.out.println("Exception Occurs: " + e);
}
%>
</body></html>
<%@ page contentType="text/html;charset=Big5"
language="java"
import="java.sql.*" %>
<%
String uid = request.getParameter("uid");
String pwd = request.getParameter("pwd");
// 初始化變數
String aQuery = null;
Connection conn = null;
try
{
// 載入 JDBC 的驅動程式
Class.forName("com.mysql.jdbc.Driver");
// 欲執行之 SQL 查詢語法
aQuery = "select * from Product";
// 產生程式和資料庫之間的連線
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/eric", uid, pwd);
%>
<html><head><title>
查詢產品資料表的結果
</title></head><body>
<%
// 查詢產品資料表的結果
Statement aStatement = conn.createStatement();
ResultSet rs = aStatement.executeQuery(aQuery);
// 取得產品資料表的 metadata
ResultSetMetaData rsmeta = rs.getMetaData();
int cols = rsmeta.getColumnCount();
// 建立 HTML table 來顯示資料表的內容
out.println("<table border=\"1\">");
out.println("<tr>");
// 顯示欄位名稱
for(int i=1; i<=cols; i++) {
out.print("<th>");
out.print(rsmeta.getColumnLabel(i));
out.print("</th>");
}
out.println("\n</tr>");
// 顯示資料表內的內容
while(rs.next()) {
out.print("<tr>");
for(int i=1; i<=cols; i++) {
out.print("<td>");
out.print(rs.getString(i));
out.print("</td>");
}
out.println("</tr>");
}
// 結束前,把該關閉的關閉
rs.close();
aStatement.close();
conn.close();
}
// 可以考慮設計成比較好的例外處理:例如載入驅動程式錯誤、連接資料庫失敗等
catch (Exception e) {
System.out.println("發生錯誤: " + e.getMessage());
}
%>
</body></html>
<%@page contentType="text/html;charset=Big5"
language="java"
import="java.sql.*" %>
<html>
<!-- 加上 ! 宣告成為類別的屬性,否則便成為方法內的變數 -->
<%! private Connection conn = null;
private int count = 0;
%>
<%!
// 用來產生一個資料庫的連線,提供所有使用這個 JSP 檔的要求使用
public void jspInit() {
count = 10;
try {
// 載入 JDBC-ODBC 驅動程式
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// 請記得設定適當的 UID 和 PWD。
conn = DriverManager.getConnection("jdbc:odbc:csie", UID, PWD);
}
catch (Exception e) {
System.out.println("Fail to connect to database.");
}
}
// 在這個 JSP 檔從 tomcat 卸載時,將之前的資料庫連線結束掉
public void jspDestroy() {
try {
conn.close();
} catch (Exception e) {
System.out.println("Fail to close connection.");
}
}
%>
<%
count++;
String salary = request.getParameter("price");
// initialize query string
String aQuery = null;
try {
// connect to Database
aQuery = "select * from Product where Price > " + salary;
%>
<head><title>Database Query</title></head>
<body>
<h3>Accessed <%=count%> times</h3>
<%
// Construct a SQL statement and submit it
Statement aStatement = conn.createStatement();
ResultSet rs = aStatement.executeQuery(aQuery);
// Get info about the query results
ResultSetMetaData rsmeta = rs.getMetaData();
int cols = rsmeta.getColumnCount();
// contruct table
out.println("<table border=\"1\">");
out.println("<tr>");
// Display column headers
for(int i=1; i<=cols; i++) {
out.print("<th>");
out.print(rsmeta.getColumnLabel(i));
out.print("</th>");
}
out.println("\n</tr>");
// Display query results.
while(rs.next()) {
out.print("<tr>");
for(int i=1; i<=cols; i++) {
out.print("<td>");
out.print(rs.getString(i));
out.print("</td>");
}
out.println("</tr>");
}
// Clean up
rs.close();
aStatement.close();
}
// a better exception handling can be used here.
catch (Exception e) {
System.out.println("Exception Occurs: " + e);
}
%>
</body></html>
<%@include file="initdestroy.jsp" %>
<?xml version="1.0" encoding="Big5"?>
<web-app>
<servlet>
<servlet-name>ReadParameters</servlet-name>
<jsp-file>/HelloParam.jsp</jsp-file>
<init-param>
<param-name>dsn</param-name>
<param-value>csie</param-value>
</init-param>
<init-param>
<param-name>param2</param-name>
<param-value>你好嗎</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ReadParameters</servlet-name>
<url-pattern>/HelloParam.jsp</url-pattern>
</servlet-mapping>
</web-app>
<%@page contentType="text/html;charset=Big5" %>
<html>
<body>
<%
String p1 = config.getInitParameter("dsn");
String p2 = config.getServletName();
String p3 = config.getInitParameter("param2");
%>
<h3 align="center"><%=p1%></h3>
<h3 align="center"><%=p2%></h3>
<h3 align="center"><%=p3%></h3>
</body>
</html>
<?xml version="1.0" encoding="Big5"?>
<web-app>
<context-param>
<param-name>jdbc_driver</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</context-param>
<context-param>
<param-name>db_server</param-name>
<param-value>mysql</param-value>
</context-param>
<context-param>
<param-name>db_port</param-name>
<param-value>3306</param-value>
</context-param>
<servlet>
<servlet-name>ReadParameters</servlet-name>
<jsp-file>/HelloParam.jsp</jsp-file>
<init-param>
<param-name>dsn</param-name>
<param-value>csie</param-value>
</init-param>
<init-param>
<param-name>param2</param-name>
<param-value>你好嗎</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ReadParameters</servlet-name>
<url-pattern>/HelloParam.jsp</url-pattern>
</servlet-mapping>
</web-app>
<!-- HelloContext.jsp -->
<%@page contentType="text/html;charset=Big5" %>
<html>
<body>
<%
String p1 = application.getInitParameter("jdbc_driver");
String p2 = application.getInitParameter("db_server");
String p3 = application.getInitParameter("db_port");
%>
<h3 align="center"><%=p1%></h3>
<h3 align="center"><%=p2%></h3>
<h3 align="center"><%=p3%></h3>
</body>
</html>
// 取得 config ServletConfig config = getServletConfig(); // 取得 application ServletContext application = getServletContext();
<!--
UseCookies.jsp
-->
<%@ page language="java" contentType="text/html;charset=Big5" %>
<html>
<head><title>Use Cookies</title></head>
<body>
<%
Cookie [] cookie = request.getCookies();
if(cookie.length == 0)
%>
<h1>沒有 Cookie</h1>
<% else
{
out.println("<h1>大家好</h1>");
out.println("<ul>");
for(int i=0; i<cookie.length; i++)
out.println("<li> " + cookie[i].getName() + "=" +
cookie[i].getValue());
out.println("</ul>");
}
%>
</body>
</html>
<!--
SetCookies.jsp
-->
<%@ page language="java" contentType="text/html;charset=Big5" %>
<html>
<head><title>Set Cookies</title></head>
<body>
<%
Cookie myck1 = new Cookie("UID", "123");
response.addCookie(myck1);
Cookie myck2 = new Cookie("PWD", "abc");
response.addCookie(myck2);
%>
<h1>寫入 Cookies</h1>
</body>
</html>
<?xml version="1.0" encoding="Big5" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>Hello 系列</shortname>
<info>Hello 系列:沒 body 的範例</info>
<tag>
<name>hello</name>
<tagclass>Hello.Hello1</tagclass>
<bodycontent>empty</bodycontent>
<attribute>
<name>name</name>
<required>false</required>
</attribute>
</tag>
</taglib>
package Hello;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
/**
* This is a simple tag example to show how content is added to the
* output stream when a tag is encountered in a JSP page.
*/
public class Hello1 extends TagSupport {
private String name=null;
// Getter/Setter for the attribute name
public void setName(String value){
name = value;
}
public String getName(){
return(name);
}
/**
* doStartTag is called by the JSP container when the tag is encountered
*/
public int doStartTag() {
try {
JspWriter out = pageContext.getOut();
out.println("<table border=\"1\">");
if (name != null)
out.println("<tr><td> Hello " + name + " </td></tr>");
else
out.println("<tr><td> Hello World </td></tr>");
} catch (Exception ex) {
throw new Error("All is not well in the world.");
}
// Must return SKIP_BODY because we are not supporting a body for this
// tag.
return SKIP_BODY;
}
/**
* doEndTag is called by the JSP container when the tag is closed
*/
public int doEndTag(){
try {
JspWriter out = pageContext.getOut();
out.println("</table>");
} catch (Exception ex){
throw new Error("All is not well in the world.");
}
return SKIP_BODY;
}
}
<%@ taglib uri="Hello.tld" prefix="sample" %> <html> <head> <title>Your Standard Hello World Demo</title> </head> <body bgcolor="#ffffff"> <hr /> <sample:hello name="Sue"/> <hr /> </body> </html>