jsp基本概念

服务器启动的时候执行初始化init方法,只执行一次

每次请求都会执行一次service方法

服务器停止的时候执行destroy方法,也是只执行一次

<%!
//全局变量
int initNum=0;
int serviceNum=0;
int destoryNum=0;


%>

<%!
//书写两个方法
public void jspInit(){
initNum++;
System.out.println("jspInit()============>执行了"+initNum);
}

public void jspDestory(){
destoryNum++;
System.out.println("jspDestory()============>执行了"+destoryNum);
}

%>


<%

serviceNum++;
System.out.println("jspService()============>执行了"+serviceNum);

%>

 

 

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
contentType="text/html; charset=utf-8"
%>
<%--
language:指定了jsp页面中使用的脚本语言
import:引入使用的脚本语言所需要的类文件
pageEncoding:当前页面的编码格式
contentType:制定页面中的 MIME 类型,以及编码格式(在浏览器中显示的编码)
--%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<%--
什么是内置对象?
   不需要声明和创建,就可以在jsp页面直接使用的成员变量!

九大内置对象                    对应java中的类
out              输出对象               JspWriter   
request          请求对象                HttpServletRequest
response         响应对象                 HttpServletResponse
page             页面对象                this
pageContext     页面的上下文对象    PageContext
session          会话对象                 HttpSession
application      应用程序对象         ServletContext
config           配置对象                 ServletConfig
exception        异常对象                 Throwable
--%>


  
    <% 
    //局部变量  如果没有使用 在service中是不显示的
    //都在service方法中
       int a=5;
       int b=15;
    %>
  
    <%=a %>
    <%=b %>
  
  
   <%!
   //全局变量  
   private  int  num1=10;
   private  int  num2=10;
   
   public   int  add(){
       return num1+num2;
   }
   //单行注释
   /* 多行注释*/
   %>
  
  <!-- html注释  可以看到 -->
<%-- jsp注释    用户在前台源码中看不到 --%>
 <% out.print(add()); %>
  <%=add() %>  <br/>
  
<%-- 转义符 --%>
  <%="\"哈哈\"" %>
  
  
  
</body>
</html>
posted @ 2017-08-06 09:57  迷茫王子  阅读(154)  评论(0)    收藏  举报