jsp与java的关系_jsp教程中:jsp与servlet的区别和关系
jsp教程上:我們一定學(xué)了servlet,也學(xué)習(xí)了jsp,那么jsp與servlet的區(qū)別是什么呢,jsp與servlet又有什么關(guān)系呢?下面我們先創(chuàng)建一個(gè)demo.jsp的頁面,然后tomcat運(yùn)行,看看jsp編譯之后是怎么樣的呢?
String?path?=?request.getContextPath();
String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
">
jsp演示代碼SimpleDateFormat?sf?=?new?SimpleDateFormat("yyyy-MM-dd?HH:mm:ss");
out.write("輸出當(dāng)前時(shí)間:"+sf.format(new?Date()));
%>
demo.jsp代碼運(yùn)行的結(jié)果是:輸出當(dāng)前時(shí)間:2017-02-08?22:09:10
上面的代碼已經(jīng)運(yùn)行之后,我們?cè)俚絫omcat服務(wù)器的work\Catalina文件夾里面去查看編譯后生成的demo_jsp.java這個(gè)文件,看看demo.jsp文件被編譯之后變成java文件之后是什么樣子的。package?org.apache.jsp;
import?javax.servlet.*;
import?javax.servlet.http.*;
import?javax.servlet.jsp.*;
import?java.text.SimpleDateFormat;
import?java.util.*;
public?final?class?demo_jsp?extends?org.apache.jasper.runtime.HttpJspBase
implements?org.apache.jasper.runtime.JspSourceDependent?{
//刪除了部分代碼
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;
javax.servlet.http.HttpSession?session?=?null;
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,
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\n");
out.write("\r\n");
String?path?=?request.getContextPath();
String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
out.write("\r\n");
out.write("HTML?PUBLIC?\"-//W3C//DTD?HTML?4.01?Transitional//EN\">\r\n");
out.write("\r\n");
out.write("??
\r\n");out.write("????
out.print(basePath);
out.write("\">\r\n");
out.write("????\r\n");
out.write("????
jsp演示代碼\r\n");out.write("??\r\n");
out.write("??
\r\n");out.write("????");
SimpleDateFormat?sf?=?new?SimpleDateFormat("yyyy-MM-dd?HH:mm:ss");
out.write("輸出當(dāng)前時(shí)間:"+sf.format(new?Date()));
out.write("\r\n");
out.write("??\r\n");
out.write("\r\n");
}?catch?(java.lang.Throwable?t)?{
if?(!(t?instanceof?javax.servlet.jsp.SkipPageException)){
out?=?_jspx_out;
if?(out?!=?null?&&?out.getBufferSize()?!=?0)
try?{
if?(response.isCommitted())?{
out.flush();
}?else?{
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);
}
}
}
你會(huì)發(fā)現(xiàn)上面的demo.jsp文件被編譯成了servlet的代碼,其實(shí)jsp和servlet的區(qū)別就是表現(xiàn)形式不一樣,本質(zhì)卻是一樣的,servlet是完全可以替代這個(gè)demo.jsp來輸出這個(gè)頁面,但是為什么又需要jsp呢?
這是因?yàn)樵趯?shí)際項(xiàng)目中,jsp有網(wǎng)頁標(biāo)簽,適合做網(wǎng)頁界面的設(shè)計(jì),如果所有的js和css都通過servlet來write輸出的話,代碼會(huì)變得非常繁瑣,所以實(shí)際項(xiàng)目中servlet主要負(fù)責(zé)網(wǎng)站后臺(tái)邏輯的實(shí)現(xiàn),jsp主要負(fù)責(zé)的是頁面的布局和后臺(tái)數(shù)據(jù)的獲取。
jsp和servlet的關(guān)系實(shí)際上就是一種互補(bǔ)的關(guān)系,只是職責(zé)不一樣,完整學(xué)習(xí)jsp教程,請(qǐng)看下一篇:10分鐘學(xué)會(huì)使用jsp內(nèi)置對(duì)象域?qū)ο蠹癳l表達(dá)式
來源網(wǎng)站:太平洋學(xué)習(xí)網(wǎng),轉(zhuǎn)載請(qǐng)注明出處:http://www.tpyyes.com/a/javaweb/63.html
總結(jié)
以上是生活随笔為你收集整理的jsp与java的关系_jsp教程中:jsp与servlet的区别和关系的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一部外国老电影《德黑兰30年》剧情
- 下一篇: 上海欢乐谷两次入园票是什么意思