【Head First Servlets and JSP】笔记15:建立一个JSP页面来显示被访问了多少次
2017-06-14 15:33
756 查看
1、这是一个非常简单的程序,它看起来是这个样子的:
实际功能就是,每访问该页面一次count数加1,在服务器重启前(JVM重启前),这个次数将持续累加。
2、因为这个程序过于简单,所以我希望可以通过HTML内置Java来完成。
这样做的好处就是:可以把写servlet的事情交给web容器来做。
3、代码如下:
BasicCounter.jsp
web.xml使用默认的即可
4、启动服务器后,输入BasicCounter.jsp的绝对路径即可访问。
5、println是servlet才用的语句!在JSP中还可以改进代码,如下
这是一种等价写法,事实上web容器在把JSP转换成.java时,还是会把它改写为out.println...
还可以这么玩↓
每次访问这个JSP都会获得一个新的随机数。
6、目前为止,我们已经看到了三种不同形态的JSP元素:
7、实际上,我们甚至不需要创建额外的cOUNTER类,我们尝试一下这段代码
这段代码是错的。结果很容易猜到:count的结果永远是1,因为每调用一次页面count都会被重新初始化一次。
要想知道容器到底对你的JSP做了什么,
唯一的办法就是去产看源码。
8、在实际应用中,统计自身被调用多少次并不是servlet的职责,可以通过过滤器或者AOP切面实现。
实际功能就是,每访问该页面一次count数加1,在服务器重启前(JVM重启前),这个次数将持续累加。
2、因为这个程序过于简单,所以我希望可以通过HTML内置Java来完成。
这样做的好处就是:可以把写servlet的事情交给web容器来做。
3、代码如下:
packageSample; publicclassCounter{ privatestaticintcount; publicstaticintgetCount(){ ++count; returncount; } }
BasicCounter.jsp
<%@pageimport="Sample.Counter"%> <%@pagecontentType="text/html;charset=UTF-8"language="java"%> <html> <head> <title>BasicCounter</title> </head> <body> <p>Thepagecountis:</p> <% out.println(Counter.getCount()); %> </body> </html>
web.xml使用默认的即可
<?xmlversion="1.0"encoding="UTF-8"?> <web-appxmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"> </web-app>
4、启动服务器后,输入BasicCounter.jsp的绝对路径即可访问。
5、println是servlet才用的语句!在JSP中还可以改进代码,如下
<%@pageimport="Sample.Counter"%> <%@pagecontentType="text/html;charset=UTF-8"language="java"%> <html> <head> <title>BasicCounter</title> </head> <body> <p>Thepagecountis:</p> <%=Counter.getCount()%> </body> </html>
这是一种等价写法,事实上web容器在把JSP转换成.java时,还是会把它改写为out.println...
还可以这么玩↓
<%@pageimport="Sample.HelloWorld"%> <%@pagecontentType="text/html;charset=UTF-8"language="java"%> <html> <head> <title>Simplejsppage</title> </head> <body> <h3class="message"><%=HelloWorld.getMessage()%></h3> <%=Math.random()%> </body> </html>
每次访问这个JSP都会获得一个新的随机数。
6、目前为止,我们已经看到了三种不同形态的JSP元素:
<%=Counter.getCount()%>
<%@pageimport="Sample.Counter"%>
<% out.println(Counter.getCount()); %>
7、实际上,我们甚至不需要创建额外的cOUNTER类,我们尝试一下这段代码
<%@pageimport="Sample.Counter"%> <%@pagecontentType="text/html;charset=UTF-8"language="java"%> <html> <head> <title>BasicCounter</title> </head> <body> <% intcount=0; %> <p>Thepagecountis:</p> <%=++count%> </body> </html>
这段代码是错的。结果很容易猜到:count的结果永远是1,因为每调用一次页面count都会被重新初始化一次。
要想知道容器到底对你的JSP做了什么,
唯一的办法就是去产看源码。
/* *GeneratedbytheJaspercomponentofApacheTomcat *Version:ApacheTomcat/7.0.77 *Generatedat:2017-06-1408:18:57UTC *Note:Thelastmodifiedtimeofthisfilewassetto *thelastmodifiedtimeofthesourcefileafter *generationtoassistwithmodificationtracking. */ packageorg.apache.jsp; importjavax.servlet.*; importjavax.servlet.http.*; importjavax.servlet.jsp.*; importSample.Counter; publicfinalclassBasicCounter_jspextendsorg.apache.jasper.runtime.HttpJspBase implementsorg.apache.jasper.runtime.JspSourceDependent{ privatestaticfinaljavax.servlet.jsp.JspFactory_jspxFactory= javax.servlet.jsp.JspFactory.getDefaultFactory(); privatestaticjava.util.Map<java.lang.String,java.lang.Long>_jspx_dependants; privatevolatilejavax.el.ExpressionFactory_el_expressionfactory; privatevolatileorg.apache.tomcat.InstanceManager_jsp_instancemanager; publicjava.util.Map<java.lang.String,java.lang.Long>getDependants(){ return_jspx_dependants; } publicjavax.el.ExpressionFactory_jsp_getExpressionFactory(){ if(_el_expressionfactory==null){ synchronized(this){ if(_el_expressionfactory==null){ _el_expressionfactory=_jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); } } } return_el_expressionfactory; } publicorg.apache.tomcat.InstanceManager_jsp_getInstanceManager(){ if(_jsp_instancemanager==null){ synchronized(this){ if(_jsp_instancemanager==null){ _jsp_instancemanager=org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } } } return_jsp_instancemanager; } publicvoid_jspInit(){ } publicvoid_jspDestroy(){ } publicvoid_jspService(finaljavax.servlet.http.HttpServletRequestrequest,finaljavax.servlet.http.HttpServletResponseresponse) throwsjava.io.IOException,javax.servlet.ServletException{ finaljavax.servlet.jsp.PageContextpageContext; javax.servlet.http.HttpSessionsession=null; finaljavax.servlet.ServletContextapplication; finaljavax.servlet.ServletConfigconfig; javax.servlet.jsp.JspWriterout=null; finaljava.lang.Objectpage=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"); out.write("<html>\r\n"); out.write("<head>\r\n"); out.write("<title>BasicCounter</title>\r\n"); out.write("</head>\r\n"); out.write("<body>\r\n"); intcount=0; out.write("\r\n"); out.write("<p>Thepagecountis:</p>\r\n"); out.print(++count); out.write("\r\n"); out.write("</body>\r\n"); out.write("</html>\r\n"); }catch(java.lang.Throwablet){ if(!(tinstanceofjavax.servlet.jsp.SkipPageException)){ out=_jspx_out; if(out!=null&&out.getBufferSize()!=0) try{ if(response.isCommitted()){ out.flush(); }else{ out.clearBuffer(); } }catch(java.io.IOExceptione){} if(_jspx_page_context!=null)_jspx_page_context.handlePageException(t); elsethrownewServletException(t); } }finally{ _jspxFactory.releasePageContext(_jspx_page_context); } } }
8、在实际应用中,统计自身被调用多少次并不是servlet的职责,可以通过过滤器或者AOP切面实现。
相关文章推荐
- 【Head First Servlets and JSP】笔记7:如何创建一个全局的dog?
- 【Head First Servlets and JSP】笔记6:什么是响应首部 & 快速搭建一个简单的测试环境
- 【Head First Servlets and JSP】笔记14:session再探 & Listener示例
- 【Head First Servlets and JSP】笔记19:JavaBeans与JSP动作元素(<jsp:setProperty.....>、<jsp:getProperty.....>)
- 【Head First Servlets and JSP】笔记 27: web 应用安全
- 【Head First Servlets and JSP】笔记20:EL以及<jsp:useBean ....>的补充
- 【Head First Servlets and JSP】笔记5:HttpServletResponse resp
- 【Head First Servlets and JSP】笔记13:session & cookie
- 【Head First Servlets and JSP】笔记22:直接从请求到JSP & 获取Person的嵌套属性
- 【Head First Servlets and JSP】笔记21:从有脚本到无脚本
- 【Head First Servlets and JSP】笔记2:MVC迷你教程
- 【Head First Servlets and JSP】笔记8:监听者
- 【Head First Servlets and JSP】笔记 25:JSTL 参考
- 【Head First Servlets and JSP】笔记23:Expression Language(EL) 完全攻略
- 【Head First Servlets and JSP】笔记1
- 【Head First Servlets and JSP】笔记16:JSP隐式对象(内置对象)的映射关系
- 【Head First Servlets and JSP】笔记12:URL重写
- 【Head First Servlets and JSP】笔记10:请求分派(RequestDispatcher)
- 【Head First Servlets and JSP】笔记3:Servlet的生命周期
- 【Head First Servlets and JSP】笔记 26: web 应用部署