JSP基礎語法 JSP註釋 comment.jsp 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C// ...
JSP註釋
comment.jsp
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 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <!-- 這個註釋客戶端可以看見 --> 11 <%-- jsp中的註釋,客戶端無法看見 --%> 12 <% 13 //java中的單行註釋,客戶端無法看見 14 /* 15 java中的多行註釋,客戶端無法看見 16 */ 17 %> 18 </body> 19 </html>View Code
Scriptlet
scriptlet_demo01.jsp
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 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 //定義局部變數,編寫語句 12 int x = 10; 13 String info = "www.mldnjava.cn"; 14 out.println("<h2>x="+x+"</h2>"); 15 out.println("<h2>info="+info+"</h2>"); 16 %> 17 </body> 18 </html>View Code
scriptlet_demo02.jsp
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 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <!-- 定義全局變數,方法,類 --> 11 <%! public static final String INFO = "www.mldnjava.cn"; %> 12 <!-- 用於輸出一個變數或一個具體的常量 --> 13 <%=1 %><br/> 14 <%=INFO %> 15 </body> 16 </html>View Code
儘量不要使用system.out.print();進行輸出
input_table_value.jsp
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 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="print_table.jsp" method="post"> 11 <table border="1" width="100%"> 12 <tr> 13 <td>輸入表格的行數:</td> 14 <td><input type="text" name="row"></td> 15 </tr> 16 <tr> 17 <td>輸入表格的列數:</td> 18 <td><input type="text" name="col"></td> 19 </tr> 20 <tr> 21 <td> 22 <input type="submit" value="顯示"> 23 <input type="reset" value="重置"> 24 </td> 25 </tr> 26 </table> 27 </form> 28 </body> 29 </html>View Code
print_table.jsp
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 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 int rows = 0; 12 int cols = 0; 13 //讀取input_table_value.jsp post的row和col,將之強制轉換為int類型 14 try{ 15 rows = Integer.parseInt(request.getParameter("row")); 16 cols = Integer.parseInt(request.getParameter("col")); 17 }catch(Exception e){} 18 if(rows>0&&cols>0){ 19 %> 20 <table border="1" width="100%"> 21 <% 22 for(int x = 1;x <= rows; x++){ 23 %> 24 <tr> 25 <% 26 for(int y = 1; y <= cols; y++){ 27 %> 28 <td> <%=x%> * <%=y%> = <%=(x * y)%></td> 29 <% 30 } 31 %> 32 </tr> 33 <% 34 } 35 %> 36 </table> 37 <a href="input_table_value.jsp"><input type="button" value="返回"></a> 38 <% 39 }else{ 40 %> 41 <%--輸入不符合時彈出對話框指示,並自動返回到輸入數值處 --%> 42 <script type="text/javascript" language="javascript"> 43 alert("輸入不合法!"); 44 /* alert(document.location === window.location);//true */ 45 //window.location.href="input_table_value.jsp"; 46 //document.location.href="input_table_value.jsp"; 47 //以上兩種好像等價,待探索 48 window.document.location.href="input_table_value.jsp"; 49 </script> 50 <% 51 } 52 %> 53 </body> 54 </html>View Code
scriptlet標簽
此標簽具有和<% %>
一樣的效果,更加美觀一些,無強制要求
scriptlet_tag.jsp
1 <jsp:scriptlet> 2 String url = "www.MLDNJAVA.cn"; 3 </jsp:scriptlet> 4 <h2><%=url %></h2>View Code
page指令
設置頁面的MIME、文件編碼
page_demo01.jsp
1 <%@ page language="java" contentType="application/msword; 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 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <!-- pageEncoding是JSP文件本身的編碼,contentType是伺服器發送給客戶端的內容編碼 --> 11 12 <!-- txt text/plain --> 13 <!-- doc application/msword --> 14 <!-- png image/png --> 15 <!-- jpg/jpeg image/jpeg --> 16 <!-- htm/html text/html--> 17 <table border="1"> 18 <% 19 //指定文件下載後的保存名稱是mldn.doc 20 response.setHeader("Content-Disposition", "attachment;filename=mldn.doc"); 21 %> 22 <tr><td>歡迎大家</td></tr> 23 <tr><td>歡迎大家!!</td></tr> 24 <tr><td>歡迎大家!!!</td></tr> 25 </table> 26 </body> 27 </html>View Code
錯誤頁的設置
伺服器端跳轉
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page errorPage="error.jsp" %> 4 <%-- 出現錯誤將會跳轉到error.jsp --%> 5 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 10 <title>Insert title here</title> 11 </head> 12 <body> 13 <% 14 int result = 10 / 0; 15 %> 16 <%=result %> 17 </body> 18 </html>View Code
error.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page isErrorPage="true" %> 4 <%-- 表示出現錯誤該頁面可以處理錯誤 --%> 5 <% response.setStatus(200); %> 6 <%-- 設置了200的HTTP狀態碼,表示本頁沒有錯誤,防止tomcat也認為本頁出現了錯誤,從而無法顯示 --%> 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">