首先在沒有jsp文件的情況下,是以下方式寫頁面:
package com.oracle.index; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class indexServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("<!DOCTYPE html>"); response.getWriter().write("<html>"); response.getWriter().write("<head>"); response.getWriter().write("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>"); response.getWriter().write("<title>Insert title here</title>"); response.getWriter().write("</head>"); response.getWriter().write("<body>"); response.getWriter().write("1234567890"); response.getWriter().write("</body>"); response.getWriter().write("</html>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
效果如下:
1、jsp
(實質上就是一個servlet)
(1)jsp腳本:
1)<% java代碼 %>--相當於寫在方法里;所以不能定義方法;
2)<%= java變量或表達式%>---相當於寫在out.print()里面
3)<%!java代碼%>---相當於成員變量;所以能定義方法;
(2)jsp注釋:
1)//java單行注釋 jsp源碼、翻譯后的servlet、
2)/*java多行注釋*/ jsp源碼、翻譯后的servlet、
3)<!--html注釋--> jsp源碼、翻譯后的servlet、html源代碼
4)<%--jsp注釋--%> jsp源碼、
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="false" errorPage="error.jsp"%> <!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> </head> <body> <!-- html注釋 --> <%--jsp注釋 --%> <%int i = 0; System.out.println(i); //java單行注釋 %> <%= i %> <%! String str = "ni hao java"; /*java多行注釋*/ %> <%=str %> </body> </html>
具體表現如下:
網頁顯示為:
控制台顯示為:
具體顯示的java文件可以發現具體的注釋位置和變量
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/7.0.52 * Generated at: 2018-11-16 03:11:37 UTC * Note: The last modified time of this file was set to * the last modified time of the source file after * generation to assist with modification tracking. */ package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { String str = "ni hao java"; /*java多行注釋*/ private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory.getDefaultFactory(); private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; private javax.el.ExpressionFactory _el_expressionfactory; private org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map<java.lang.String,java.lang.Long> getDependants() { return _jspx_dependants; } public void _jspInit() { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } public void _jspDestroy() { } public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { final javax.servlet.jsp.PageContext pageContext; final javax.servlet.ServletContext application; final javax.servlet.ServletConfig config; javax.servlet.jsp.JspWriter out = null; final java.lang.Object page = this; javax.servlet.jsp.JspWriter _jspx_out = null; javax.servlet.jsp.PageContext _jspx_page_context = null; try { response.setContentType("text/html; charset=UTF-8"); pageContext = _jspxFactory.getPageContext(this, request, response, "error.jsp", false, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); out = pageContext.getOut(); _jspx_out = out; out.write("\r\n"); out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n"); out.write("<html>\r\n"); out.write("<head>\r\n"); out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n"); out.write("<title>Insert title here</title>\r\n"); out.write("</head>\r\n"); out.write("\r\n"); out.write("<body>\r\n"); out.write("\t<!-- html注釋 -->\r\n"); out.write("\t"); out.write("\r\n"); out.write(" "); int i = 0; System.out.println(i); //java單行注釋 out.write("\r\n"); out.write(" "); out.print( i ); out.write("\r\n"); out.write(" "); out.write("\r\n"); out.write(" "); out.print(str ); out.write("\r\n"); out.write("</body>\r\n"); out.write("</html>"); } catch (java.lang.Throwable t) { if (!(t instanceof javax.servlet.jsp.SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { out.clearBuffer(); } catch (java.io.IOException e) {} if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); else throw new ServletException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }
以及頁面源代碼:
(3)jsp運行原理
第一次訪問index.jsp --> index_jsp.java [Tomcat下的work目錄下] --> 編譯運行;
當訪問完index.jsp后如圖:
(4)jsp三大指令
1)page指令:
language:jsp腳本可以嵌套的代碼,但是只能是java;
pageEncoding:當前jsp腳本的編碼
contentType:"text/html; charset=UTF-8"相當於響應的中文亂碼問題解決;
session:"true","false";
是否使用session對象
import:導包;
一個一個的導入:
<%@ page import="java.sql.*" %>
<%@ page import="java.util.regex.*"%>
一塊導入:
<%@ page import="java.sql.*,java.util.regex.*" %>中間用逗號隔開,后面不加逗號!
errorPage:當前頁面出錯跳轉到其他頁面;
isErrorPage:當前頁面是一個處理錯誤的頁面;
2)include指令:
靜態:<%@ include file="被包含的文件地址"%>;
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
</head>
<body>
<h1>這是頭部!</h1>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> </head> <body> <%@ include file="header.jsp"%> <h3>這是內容!</h3> <%@ include file="footer.jsp"%> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
</head>
<body>
<h1>這是尾部!</h1>
</body>
</html>
效果如下:
動態:<jsp:include page="被包含的頁面"/>;
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
</head>
<body>
<h1>這是include1</h1>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
</head>
<body>
<jsp:include page="/include.jsp"></jsp:include>
<h2>這是include的h2標簽</h2>
</body>
</html>
效果如下:
靜態包含:被包含文件不編譯,直接以Html進入文件;動態包含:被包含文件編譯,指向被包含文件地址,進行編譯;
3)taglib指令;
(5)九大內置對象:out、request、response、config、session、application、page、pagecontext、exception、
(6)out對象:out緩沖區的內容添加到response緩沖區、默認為buffer="8kb",設置"0kb"時直接進入response緩沖區;
最后注意打印順序:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" buffer="8kb"%>
<!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>
</head>
<body>
<% out.write("bbbbbbbbbbbbbbb"); response.getWriter().write("ccccccccccccccccc"); %> <%="gggggggggggggggg" %> </body> </html>
具體表現如下:
原因如下:
1、首先介紹write()和print()方法的區別:
(1)、write():僅支持輸出字符類型數據,字符、字符數組、字符串等
(2)、print():可以將各種類型(包括Object)的數據通過默認編碼轉換成bytes字節形式,這些字節都通過write(int c)方法被輸出
2、介紹response.getWriter()和out的區別:
(1)、out和response.getWriter的類不一樣,一個是JspWriter,另一個是java.io.PrintWriter。
(2)、執行原理不同:JspWriter相當於一個帶緩存功能的printWriter,它不是直接將數據輸出到頁面,而是將數據刷新到response的緩沖區后再輸出,
response.getWriter直接輸出數據(response.print()),所以(out.print)只能在其后輸出。
(3)、out為jsp的內置對象,刷新jsp頁面,自動初始化獲得out對象,所以使用out對象是需要刷新頁面的,
而response.getWriter()響應信息通過out對象輸出到網頁上,當響應結束時它自動被關閉,與jsp頁面無關,無需刷新頁面
形象的比喻:當我們調用response.getWriter()這個對象同時獲得了網頁的畫筆,這時你就可以通過這個畫筆在網頁上畫任何你想要顯示的東西。
(4)、out的print()方法和println()方法在緩沖區溢出並且沒有自動刷新時候會產生ioexception,
而response.getWrite()方法的print和println中都是抑制ioexception異常的,不會有ioexception
(7)pageContext對象:pageContext可以向指定的其他域中存取數據;
findAttribute(String name)依次從pageContext域,request域,session域,application域中獲取屬性,
在某個域中獲取后將不在向后尋找
pageContext.getRequest();可以獲得其他8大隱式對象
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> </head> <body> <% pageContext.setAttribute("name", "zhangsan"); %> <!-- 向request域中設置值 --> <% pageContext.setAttribute("name", "lisi",pageContext.REQUEST_SCOPE); %> <% pageContext.setAttribute("name", "wangwu",pageContext.APPLICATION_SCOPE); %> <% pageContext.setAttribute("name", "xiaohongmao",pageContext.SESSION_SCOPE); %> <%= pageContext.getAttribute("name") %> <%= pageContext.getAttribute("name",pageContext.REQUEST_SCOPE) %> <%= pageContext.getAttribute("name",pageContext.SESSION_SCOPE) %> </body> </html>
(8)jsp標簽(動作):
1)頁面包含(動態包含):<jsp:include page="被包含的頁面"/>
2)請求轉發:<jsp:forward page="要轉發的資源" />