今天开始接触JavaWeb早在暑假的时候都学完了,自己还要来复习一遍 ....
Jsp简介 :
Jsp全名为Java Server Pages ,被称为Java动态服务网页, 其根本是一个简化的Servlet设计,他实现了Java当中使用HTML标签,Jsp是一种动态网页技术标准
也是JavaEE的标准,Jsp和Servlet一样,实在服务器端执行的。
Logo :
那么今天直接进入主题 :
Jsp:指令 :
Page指令 :通常位于Jsp页面顶端,同一个页面可以有多个page指令
Include指令 : 将一个外部文件嵌入当前Jsp文件中,同时解析这个页面中的jsp语句,
taglib指令:使用标签库定义新的自定义标签,在jsp页面中启动定制行为
Jsp基础语法
Page指令语法 :
<span style="white-space:pre"> </span><%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
这是在MyEclipse的写法
解析下这句话,<%@%>是他的结构,Language:指定Jsp页面使用的脚本语言 默认值为Java
Import : 通过引用属性来引用脚本语言中使用到的类文件
contentType : 用来指定JSP页面所采用的编码方式 text/html,iso-8895-1
而这里他是用的是pageEncoding="ISO-8859-1", 我们可以对这里进行修改,使用中文的utf - 8 .
<span style="white-space:pre"> </span><%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
Jsp注释:
HTML注释 :
<!-- html注释 --> : 客户端可见
jsp注释 :
<%-- jsp注释 --%> : 客户端不可见
jsp脚本注释 ;
//单行注释
/**/多行注释
Jsp脚本:
在jsp页面中使用java代码
语法:
<% Java代码 %>
实例 :
<% out.println("This is a JavaScript"); %>
Jsp声明:
在jsp中使用java的变量及方法
语法:
<%! java代码 %>
实例 :
<%! int age = 13;//申明一个int类型的数值 int add(int x, int y){//声明了一个add()的方法,返回数值 return x + y; } %>
Jsp表达式:
在jsp页面中执行表达式
语法:
<% = jsp表达式 %>
这里需要注意的是表达式不以分号结束
实例 :
它的年龄是:<%=age %><br/> x + y = <%= add(10,20)%><br/>
Jsp的生命周期(life cycle)
jspService()方法被调用来处理客户端请求,对每一个请求,Jsp引擎都会创建一个新的线程来处理该请求,如果有多个客户端来同时访问Jsp文件,则jsp引擎会创建多个线程,每个客户端对应一个线程,这样大大的降低对系统的资源需求,提高系统并发量及响应时间,但也要注意多线程所带来的同步问题,由于servlet始终驻于内存,所以相应是非常快的
我们在index,jsp中写入
<% SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); String s = sdf.format(new Date()); %> 今天的日期是: <%= s %>
当我们查看tomcat work的文件里面,我们找到了被创建的java文件了,打开我们可以看到
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/7.0.22 * Generated at: 2016-01-10 03:42:31 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 java.util.logging.SimpleFormatter; import java.util.*; import java.text.*; public final class index_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 javax.el.ExpressionFactory _el_expressionfactory; private org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map<java.lang.String,java.lang.Long> getDependants() { return _jspx_dependants; } public void _jspInit() { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } 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("\r\n"); 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("\r\n"); out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n"); out.write("<html>\r\n"); out.write(" <head>\r\n"); out.write(" <base href=\""); out.print(basePath); out.write("\">\r\n"); out.write(" \r\n"); out.write(" <title>My JSP 'index.jsp' starting page</title>\r\n"); out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n"); out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n"); out.write("\t<meta http-equiv=\"expires\" content=\"0\"> \r\n"); out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n"); out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n"); out.write("\t<!--\r\n"); out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n"); out.write("\t-->\r\n"); out.write(" </head>\r\n"); out.write(" \r\n"); out.write(" <body>\r\n"); out.write("\t\t"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); String s = sdf.format(new Date()); out.write("\r\n"); out.write("\t\t 今天的日期是: "); out.print( s ); out.write("\r\n"); out.write(" </body>\r\n"); out.write("</html>\r\n"); } catch (java.lang.Throwable t) { if (!(t instanceof javax.servlet.jsp.SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { out.clearBuffer(); } catch (java.io.IOException e) {} if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }
第一次初始化jsp的时候被调用,
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
每次访问服务器就会创建一个新的线程
案例:九九乘法表
通过表达式创建 :
<%!String FirstTestTable() { String s = ""; for (int i = 1; i <= 9; ++i) { for (int j = 1; j <= i; ++j) { s += i + "*" + j + "=" + (i * j) + " "; } s += "<br>"; } return s; }%>
<%=FirstTestTable()%>
<%! void FirstTestTable2(JspWriter out) throws Exception { for (int i = 1; i <= 9; ++i) { for (int j = 1; j <= i; ++j) { out.println(i + "*" + j + "=" + (i * j) + " "); } out.println("<br>"); } }%>
<%FirstTestTable2(out);%>