1、这是一个非常简单的程序,它看起来是这个样子的:
实际功能就是,每访问该页面一次count数加1,在服务器重启前(JVM重启前),这个次数将持续累加。
2、因为这个程序过于简单,所以我希望可以通过HTML内置Java来完成。
这样做的好处就是:可以把写servlet的事情交给web容器来做。
3、代码如下:
package Sample; public class Counter { private static int count; public static int getCount() { ++count; return count; } }
BasicCounter.jsp
<%@ page import="Sample.Counter" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Basic Counter</title> </head> <body> <p>The page count is: </p> <% out.println(Counter.getCount()); %> </body> </html>
web.xml使用默认的即可
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> </web-app>
4、启动服务器后,输入BasicCounter.jsp的绝对路径即可访问。
5、println是servlet才用的语句!在JSP中还可以改进代码,如下
<%@ page import="Sample.Counter" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Basic Counter</title> </head> <body> <p>The page count is: </p> <%=Counter.getCount()%> </body> </html>
这是一种等价写法,事实上web容器在把JSP转换成.java时,还是会把它改写为out.println...
还可以这么玩 ↓
<%@ page import="Sample.HelloWorld" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Simple jsp page</title> </head> <body> <h3 class="message"><%=HelloWorld.getMessage()%></h3> <%=Math.random()%> </body> </html>
每次访问这个JSP都会获得一个新的随机数。
6、目前为止,我们已经看到了三种不同形态的JSP元素:
<%=Counter.getCount()%>
<%@ page import="Sample.Counter" %>
<%
out.println(Counter.getCount());
%>
7、实际上,我们甚至不需要创建额外的cOUNTER类,我们尝试一下这段代码
<%@ page import="Sample.Counter" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Basic Counter</title> </head> <body> <% int count = 0; %> <p>The page count is: </p> <%= ++count %> </body> </html>
这段代码是错的。结果很容易猜到:count的结果永远是1,因为每调用一次页面count都会被重新初始化一次。
要想知道容器到底对你的JSP做了什么,
唯一的办法就是去产看源码。
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/7.0.77 * Generated at: 2017-06-14 08:18:57 UTC * Note: The last modified time of this file was set to * the last modified time of the source file after * generation to assist with modification tracking. */ package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import Sample.Counter; public final class BasicCounter_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory.getDefaultFactory(); private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; private volatile javax.el.ExpressionFactory _el_expressionfactory; private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map<java.lang.String,java.lang.Long> getDependants() { return _jspx_dependants; } public javax.el.ExpressionFactory _jsp_getExpressionFactory() { if (_el_expressionfactory == null) { synchronized (this) { if (_el_expressionfactory == null) { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); } } } return _el_expressionfactory; } public org.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; } public void _jspInit() { } public void _jspDestroy() { } 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(" "); out.write(" "); out.write("<html> "); out.write("<head> "); out.write(" <title>Basic Counter</title> "); out.write("</head> "); out.write("<body> "); int count = 0; out.write(" "); out.write("<p>The page count is: </p> "); out.print( ++count ); out.write(" "); out.write("</body> "); out.write("</html> "); } 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); } } }
8、在实际应用中,统计自身被调用多少次并不是 servlet 的职责,可以通过过滤器或者 AOP 切面实现。