|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
认真的记,感觉很紧张根本就没有时间和能力,来对技术知识点进行思考。这样课下就只能对知识进行简单的理解,其实简单的理解就是记忆课堂上讲的知识点,js|静态|静态|页面 1.媒介
为了能深切浅出的了解这个框架的由来,我们起首来懂得一下JSP剖析器将我们写的JSP代码转换成的JAVA文件的内容。
上面是一个JSP文件test.jsp
<%@pagelanguage="java"contentType="text/html;charset=GB2312"%>
<%
out.write("<!--文件入手下手-->");
%>
<html>
<head>
<body>
<%="输入"%>
</body>
</head>
</html>
经由TOMCAT转换出的JAVA文件test$jsp.java内容以下:
packageorg.apache.jsp;
importjavax.servlet.*;
importjavax.servlet.http.*;
importjavax.servlet.jsp.*;
importorg.apache.jasper.runtime.*;
publicclasstest$jspextendsHttpJspBase{
static{
}
publictestOutRedir$jsp(){
}
privatestaticboolean_jspx_inited=false;
publicfinalvoid_jspx_init()throwsorg.apache.jasper.runtime.JspException{
}
publicvoid_jspService(HttpServletRequestrequest,HttpServletResponseresponse)
throwsjava.io.IOException,ServletException{
JspFactory_jspxFactory=null;
PageContextpageContext=null;
HttpSessionsession=null;
ServletContextapplication=null;
ServletConfigconfig=null;
JspWriterout=null;
Objectpage=this;
String_value=null;
try{
if(_jspx_inited==false){
synchronized(this){
if(_jspx_inited==false){
_jspx_init();
_jspx_inited=true;
}
}
}
_jspxFactory=JspFactory.getDefaultFactory();
response.setContentType("text/html;charset=GB2312");
pageContext=_jspxFactory.getPageContext(this,request,response,
"",true,8192,true);
application=pageContext.getServletContext();
config=pageContext.getServletConfig();
session=pageContext.getSession();
out=pageContext.getOut();
//为了节俭篇幅,我删除注释器增加的正文
out.write("
");
//上一句是因为<%@pagelanguage="java"contentType="text/html;charset=GB2312"%>前面的换行发生的
out.write("<!--文件入手下手-->");
out.write("
<html>
<head>
<body>
");
out.print("输入");
out.write("
</body>
</head>
</html>
");
}catch(Throwablet){
if(out!=null&&out.getBufferSize()!=0)
out.clearBuffer();
if(pageContext!=null)pageContext.handlePageException(t);
}finally{
if(_jspxFactory!=null)_jspxFactory.releasePageContext(pageContext);
}
}
}
从下面的代码中能够明晰的看到JSP内建的几个对象(out、request、response、session、pageContext、application、config、page)是怎样发生的,懂servlet的伴侣一看就可以分明。
上面重点了解一下out对象,它被声明为JspWriter范例,JspWriter是一个笼统类,在包javax.servlet.jsp中能够找到它的界说。
abstractpublicclassjavax.servlet.jsp.JspWriterextendsjava.io.Writer{
finalpublicstaticintNO_BUFFER=0;
finalpublicstaticintDEFAULT_BUFFER=-1;
finalpublicstaticintUNBOUNDED_BUFFER=-2;
protectedintbufferSize;
protectedBooleanautoFlush;
protectedjavax.servlet.jsp.JspWriter(intarg1,booleanarg2);
abstractpublicvoidnewLine()throwsIOException;
abstractpublicvoidprint(booleanarg0)throwsIOException;
abstractpublicvoidprint(chararg0)throwsIOException;
abstractpublicvoidprint(intarg0)throwsIOException;
abstractpublicvoidprint(longarg0)throwsIOException;
abstractpublicvoidprint(floatarg0)throwsIOException;
abstractpublicvoidprint(doublearg0)throwsIOException;
abstractpublicvoidprint(chararg0)throwsIOException;
abstractpublicvoidprint(Stringarg0)throwsIOException;
abstractpublicvoidprint(Objectarg0)throwsIOException;
abstractpublicvoidprintln()throwsIOException;
abstractpublicvoidprintln(booleanarg0)throwsIOException;
abstractpublicvoidprintln(chararg0)throwsIOException;
abstractpublicvoidprintln(intarg0)throwsIOException;
abstractpublicvoidprintln(longarg0)throwsIOException;
abstractpublicvoidprintln(floatarg0)throwsIOException;
abstractpublicvoidprintln(doublearg0)throwsIOException;
abstractpublicvoidprintln(chararg0)throwsIOException;
abstractpublicvoidprintln(Stringarg0)throwsIOException;
abtractpublicvoidprintln(Objectarg0)throwsIOException;
abstractpublicvoidclear()throwsIOException;
abstractpublicvoidclearBuffer()throwsIOException;
abstractpublicvoidflush()throwsIOException;
abstractpublicvoidclose()throwsIOException;
publicintgetBufferSize();
abstractpublicintgetRemaining();
publicbooleanisAutoFlush();
}
我信任当我写到这里你大概已晓得我想怎样做了。是的,来个批红判白,承继JspWriter类,然后完成其界说的虚函数,然后把out变量交换成你本人完成的类的实例就ok了。
2.完成交换
假定:
<%@pagelanguage="java"contentType="text/html;charset=GB2312"
import="jwb.util.HtmlIntoFile,jwb.util.TempSinglet,java.io.File"%><%
JspWriterout_bak=out;
Stringarg1="argument1";
StringfilePath="/cache/依据参数天生文件名_"+arg1+".html";
//起首判别文件是不是已存在,假如不存在则实行本页面,不然跳转到静态页面就OK了
Filef=newFile(pageContext.getServletContext().getRealPath(filePath));
if(f.exists())
{
out_bak.clear();
pageContext.forward(filePath);
System.out.println("间接转到静态页面");
return;
}
out=newHtmlIntoFile(pageContext.getServletContext().getRealPath(filePath));
out.write("<!--文件入手下手-->");
%>
<html>
<head>
<body>
<%="看吧,这就是输入被重定向到文件的完成,很复杂吧^_^"%>
</body>
</head>
</html>
<%
out.close();//封闭天生的静态文件
out_bak.clear();
pageContext.forward(filePath);
System.out.println("实行本页面后再转到静态页面");
return;
%>
3.更新成绩
上面就会商一下怎样更重生成静态文件,实在从下面完成中你能够看到,很复杂的就是将天生的静态文件删除便可,至于甚么时分删除,要看你的需求了。我能想到的几种情形以下:
当用来天生页面的数据更新时
假如不必要很供应不时的数据能够准时更新
永久不更新
其实产生见解的过程就是训练自己发现问题,分析问题的能力。根据以上的认识我想谈下传统的学习与通过视频独立学习的优缺点: |
|