當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JDBC+Servlet+JSP整合开发之22.JSP简介
生活随笔
收集整理的這篇文章主要介紹了
JDBC+Servlet+JSP整合开发之22.JSP简介
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
–對JSP的需求
–JSP的結(jié)構(gòu)
–JSP的好處
–JSP實例
?創(chuàng)建一個簡單的JSP頁面 ########################################### ? JSP
–JSP 全稱Java Server Page
?對JSP的需求
–使用servlet可以容易地完成下述任務(wù):
? 讀取表單數(shù)據(jù)
? 讀取HTTP請求報頭
? 設(shè)置HTTP狀態(tài)代碼和響應(yīng)報頭
? 使用cookie以及進行會話跟蹤
?跨servlet共享數(shù)據(jù)
? 跨請求記錄數(shù)據(jù)
? 獲得有趣且報酬豐厚的工作
? 但使用servlet也有不盡如人意的地方:
–使用println語句生成HTML
–維護上述的HTML ? JSP的結(jié)構(gòu)
–思想:
?大部分的頁面使用常規(guī)的HTML
?用特殊的標簽將servlet代碼標記出來
?整個JSP頁面最終轉(zhuǎn)換成servlet(僅執(zhí)行一次),實際
被調(diào)用的是servlet(每個請求)
–示例 使用Servlet輸出0~9的頁面 LoopServlet 測試 使用JSP輸出0~9的頁面 MyFirstJsp.jsp 測試 看一下JSP頁面轉(zhuǎn)換的Servlet文件存放在哪里哈~ MyFirstJsp_jsp.java package org.apache.jsp;????
import javax.servlet.*;????
import javax.servlet.http.*;????
import javax.servlet.jsp.*;????
import java.util.*;????
public final class MyFirstJsp_jsp extends org.apache.jasper.runtime.HttpJspBase????
????????implements org.apache.jasper.runtime.JspSourceDependent {????
????private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();????
????private static java.util.List _jspx_dependants;????
????private javax.el.ExpressionFactory _el_expressionfactory;????
????private org.apache.AnnotationProcessor _jsp_annotationprocessor;????
????public Object getDependants() {????
????????return _jspx_dependants;????
????}????
????public void _jspInit() {????
????????_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();????
????????_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());????
????}????
????public void _jspDestroy() {????
????}????
????public void _jspService(HttpServletRequest request, HttpServletResponse response)????
????????????????throws java.io.IOException, ServletException {????
????????PageContext pageContext = null;????
????????HttpSession session = null;????
????????ServletContext application = null;????
????????ServletConfig config = null;????
????????JspWriter out = null;????
????????Object page = this;????
????????JspWriter _jspx_out = null;????
????????PageContext _jspx_page_context = null;????
????????try {????
????????????response.setContentType("text/html;charset=ISO-8859-1");????
????????????pageContext = _jspxFactory.getPageContext(this, request, response,????
????????????????????????????????????null, true, 8192, true);????
????????????_jspx_page_context = pageContext;????
????????????application = pageContext.getServletContext();????
????????????config = pageContext.getServletConfig();????
????????????session = pageContext.getSession();????
????????????out = pageContext.getOut();????
????????????_jspx_out = out;????
????????????out.write('\r');????
????????????out.write('\n');????
String path = request.getContextPath();????
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";????
????????????out.write("\r\n");????
????????????out.write("\r\n");????
????????????out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");????
????????????out.write("<html>\r\n");????
????????????out.write("????<head>\r\n");????
????????????out.write("????????<base href=\"");????
????????????out.print(basePath);????
????????????out.write("\">\r\n");????
????????????out.write("????????\r\n");????
????????????out.write("????????<title>My JSP 'MyFirstJsp.jsp' starting page</title>\r\n");????
????????????out.write("????????\r\n");????
????????????out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");????
????????????out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");????
????????????out.write("\t<meta http-equiv=\"expires\" content=\"0\">????????\r\n");????
????????????out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");????
????????????out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");????
????????????out.write("\t<!--\r\n");????
????????????out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");????
????????????out.write("\t-->\r\n");????
????????????out.write("\r\n");????
????????????out.write("????</head>\r\n");????
????????????out.write("????\r\n");????
????????????out.write("????<body>\r\n");????
????????????out.write("????\t<h1>My First JSP!</h1>\r\n");????
????????????out.write("????????");????
????????????????for(int i=0;i<10;i++){????
????????????????????????out.println(i);????
????????????????????????out.print("<br/>");????
????????????????}????
????????????out.write("\r\n");????
????????????out.write("????</body>\r\n");????
????????????out.write("</html>\r\n");????
????????} catch (Throwable t) {????
????????????if (!(t instanceof 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);????
????????????}????
????????} finally {????
????????????_jspxFactory.releasePageContext(_jspx_page_context);????
????????}????
????}????
}
? JSP的好處
–盡管JSP技術(shù)能夠做的事情并不比servlet多,但JSP可以使下述任務(wù)更為容易:
? 輸出HTML
? 閱讀和維護這些HTML
–JSP使得下面的做法成為可能:
? 使用標準的HTML工具,比如Macromedia DreamWeaver或FrontPage
? 由小組的另外一些成員完成HTML布局,不關(guān)注Java編程
–JSP鼓勵我們:
? 將創(chuàng)建內(nèi)容(Java)代碼同表示內(nèi)容的(HTML)代碼分離開來 ? JSP實例
–創(chuàng)建一個簡單的JSP頁面 ########################################### 與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖
–JSP的結(jié)構(gòu)
–JSP的好處
–JSP實例
?創(chuàng)建一個簡單的JSP頁面 ########################################### ? JSP
–JSP 全稱Java Server Page
?對JSP的需求
–使用servlet可以容易地完成下述任務(wù):
? 讀取表單數(shù)據(jù)
? 讀取HTTP請求報頭
? 設(shè)置HTTP狀態(tài)代碼和響應(yīng)報頭
? 使用cookie以及進行會話跟蹤
?跨servlet共享數(shù)據(jù)
? 跨請求記錄數(shù)據(jù)
? 獲得有趣且報酬豐厚的工作
? 但使用servlet也有不盡如人意的地方:
–使用println語句生成HTML
–維護上述的HTML ? JSP的結(jié)構(gòu)
–思想:
?大部分的頁面使用常規(guī)的HTML
?用特殊的標簽將servlet代碼標記出來
?整個JSP頁面最終轉(zhuǎn)換成servlet(僅執(zhí)行一次),實際
被調(diào)用的是servlet(每個請求)
–示例 使用Servlet輸出0~9的頁面 LoopServlet 測試 使用JSP輸出0~9的頁面 MyFirstJsp.jsp 測試 看一下JSP頁面轉(zhuǎn)換的Servlet文件存放在哪里哈~ MyFirstJsp_jsp.java package org.apache.jsp;????
import javax.servlet.*;????
import javax.servlet.http.*;????
import javax.servlet.jsp.*;????
import java.util.*;????
public final class MyFirstJsp_jsp extends org.apache.jasper.runtime.HttpJspBase????
????????implements org.apache.jasper.runtime.JspSourceDependent {????
????private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();????
????private static java.util.List _jspx_dependants;????
????private javax.el.ExpressionFactory _el_expressionfactory;????
????private org.apache.AnnotationProcessor _jsp_annotationprocessor;????
????public Object getDependants() {????
????????return _jspx_dependants;????
????}????
????public void _jspInit() {????
????????_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();????
????????_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());????
????}????
????public void _jspDestroy() {????
????}????
????public void _jspService(HttpServletRequest request, HttpServletResponse response)????
????????????????throws java.io.IOException, ServletException {????
????????PageContext pageContext = null;????
????????HttpSession session = null;????
????????ServletContext application = null;????
????????ServletConfig config = null;????
????????JspWriter out = null;????
????????Object page = this;????
????????JspWriter _jspx_out = null;????
????????PageContext _jspx_page_context = null;????
????????try {????
????????????response.setContentType("text/html;charset=ISO-8859-1");????
????????????pageContext = _jspxFactory.getPageContext(this, request, response,????
????????????????????????????????????null, true, 8192, true);????
????????????_jspx_page_context = pageContext;????
????????????application = pageContext.getServletContext();????
????????????config = pageContext.getServletConfig();????
????????????session = pageContext.getSession();????
????????????out = pageContext.getOut();????
????????????_jspx_out = out;????
????????????out.write('\r');????
????????????out.write('\n');????
String path = request.getContextPath();????
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";????
????????????out.write("\r\n");????
????????????out.write("\r\n");????
????????????out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");????
????????????out.write("<html>\r\n");????
????????????out.write("????<head>\r\n");????
????????????out.write("????????<base href=\"");????
????????????out.print(basePath);????
????????????out.write("\">\r\n");????
????????????out.write("????????\r\n");????
????????????out.write("????????<title>My JSP 'MyFirstJsp.jsp' starting page</title>\r\n");????
????????????out.write("????????\r\n");????
????????????out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");????
????????????out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");????
????????????out.write("\t<meta http-equiv=\"expires\" content=\"0\">????????\r\n");????
????????????out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");????
????????????out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");????
????????????out.write("\t<!--\r\n");????
????????????out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");????
????????????out.write("\t-->\r\n");????
????????????out.write("\r\n");????
????????????out.write("????</head>\r\n");????
????????????out.write("????\r\n");????
????????????out.write("????<body>\r\n");????
????????????out.write("????\t<h1>My First JSP!</h1>\r\n");????
????????????out.write("????????");????
????????????????for(int i=0;i<10;i++){????
????????????????????????out.println(i);????
????????????????????????out.print("<br/>");????
????????????????}????
????????????out.write("\r\n");????
????????????out.write("????</body>\r\n");????
????????????out.write("</html>\r\n");????
????????} catch (Throwable t) {????
????????????if (!(t instanceof 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);????
????????????}????
????????} finally {????
????????????_jspxFactory.releasePageContext(_jspx_page_context);????
????????}????
????}????
}
? JSP的好處
–盡管JSP技術(shù)能夠做的事情并不比servlet多,但JSP可以使下述任務(wù)更為容易:
? 輸出HTML
? 閱讀和維護這些HTML
–JSP使得下面的做法成為可能:
? 使用標準的HTML工具,比如Macromedia DreamWeaver或FrontPage
? 由小組的另外一些成員完成HTML布局,不關(guān)注Java編程
–JSP鼓勵我們:
? 將創(chuàng)建內(nèi)容(Java)代碼同表示內(nèi)容的(HTML)代碼分離開來 ? JSP實例
–創(chuàng)建一個簡單的JSP頁面 ########################################### 與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖
總結(jié)
以上是生活随笔為你收集整理的JDBC+Servlet+JSP整合开发之22.JSP简介的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenSSL状态机中可选消息的处理
- 下一篇: 在JSP中如何或得当前绝对路径