當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
JDBC+Servlet+JSP整合开发之22.JSP简介
生活随笔
收集整理的這篇文章主要介紹了
JDBC+Servlet+JSP整合开发之22.JSP简介
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
–對(duì)JSP的需求
–JSP的結(jié)構(gòu)
–JSP的好處
–JSP實(shí)例
?創(chuàng)建一個(gè)簡(jiǎn)單的JSP頁(yè)面 ########################################### ? JSP
–JSP 全稱Java Server Page
?對(duì)JSP的需求
–使用servlet可以容易地完成下述任務(wù):
? 讀取表單數(shù)據(jù)
? 讀取HTTP請(qǐng)求報(bào)頭
? 設(shè)置HTTP狀態(tài)代碼和響應(yīng)報(bào)頭
? 使用cookie以及進(jìn)行會(huì)話跟蹤
?跨servlet共享數(shù)據(jù)
? 跨請(qǐng)求記錄數(shù)據(jù)
? 獲得有趣且報(bào)酬豐厚的工作
? 但使用servlet也有不盡如人意的地方:
–使用println語(yǔ)句生成HTML
–維護(hù)上述的HTML ? JSP的結(jié)構(gòu)
–思想:
?大部分的頁(yè)面使用常規(guī)的HTML
?用特殊的標(biāo)簽將servlet代碼標(biāo)記出來(lái)
?整個(gè)JSP頁(yè)面最終轉(zhuǎn)換成servlet(僅執(zhí)行一次),實(shí)際
被調(diào)用的是servlet(每個(gè)請(qǐng)求)
–示例 使用Servlet輸出0~9的頁(yè)面 LoopServlet 測(cè)試 使用JSP輸出0~9的頁(yè)面 MyFirstJsp.jsp 測(cè)試 看一下JSP頁(yè)面轉(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
? 閱讀和維護(hù)這些HTML
–JSP使得下面的做法成為可能:
? 使用標(biāo)準(zhǔn)的HTML工具,比如Macromedia DreamWeaver或FrontPage
? 由小組的另外一些成員完成HTML布局,不關(guān)注Java編程
–JSP鼓勵(lì)我們:
? 將創(chuàng)建內(nèi)容(Java)代碼同表示內(nèi)容的(HTML)代碼分離開(kāi)來(lái) ? JSP實(shí)例
–創(chuàng)建一個(gè)簡(jiǎn)單的JSP頁(yè)面 ########################################### 與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖
–JSP的結(jié)構(gòu)
–JSP的好處
–JSP實(shí)例
?創(chuàng)建一個(gè)簡(jiǎn)單的JSP頁(yè)面 ########################################### ? JSP
–JSP 全稱Java Server Page
?對(duì)JSP的需求
–使用servlet可以容易地完成下述任務(wù):
? 讀取表單數(shù)據(jù)
? 讀取HTTP請(qǐng)求報(bào)頭
? 設(shè)置HTTP狀態(tài)代碼和響應(yīng)報(bào)頭
? 使用cookie以及進(jìn)行會(huì)話跟蹤
?跨servlet共享數(shù)據(jù)
? 跨請(qǐng)求記錄數(shù)據(jù)
? 獲得有趣且報(bào)酬豐厚的工作
? 但使用servlet也有不盡如人意的地方:
–使用println語(yǔ)句生成HTML
–維護(hù)上述的HTML ? JSP的結(jié)構(gòu)
–思想:
?大部分的頁(yè)面使用常規(guī)的HTML
?用特殊的標(biāo)簽將servlet代碼標(biāo)記出來(lái)
?整個(gè)JSP頁(yè)面最終轉(zhuǎn)換成servlet(僅執(zhí)行一次),實(shí)際
被調(diào)用的是servlet(每個(gè)請(qǐng)求)
–示例 使用Servlet輸出0~9的頁(yè)面 LoopServlet 測(cè)試 使用JSP輸出0~9的頁(yè)面 MyFirstJsp.jsp 測(cè)試 看一下JSP頁(yè)面轉(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
? 閱讀和維護(hù)這些HTML
–JSP使得下面的做法成為可能:
? 使用標(biāo)準(zhǔn)的HTML工具,比如Macromedia DreamWeaver或FrontPage
? 由小組的另外一些成員完成HTML布局,不關(guān)注Java編程
–JSP鼓勵(lì)我們:
? 將創(chuàng)建內(nèi)容(Java)代碼同表示內(nèi)容的(HTML)代碼分離開(kāi)來(lái) ? JSP實(shí)例
–創(chuàng)建一個(gè)簡(jiǎn)單的JSP頁(yè)面 ########################################### 與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖
總結(jié)
以上是生活随笔為你收集整理的JDBC+Servlet+JSP整合开发之22.JSP简介的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: OpenSSL状态机中可选消息的处理
- 下一篇: ActionScript 3.0 Ste