当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JSP动态网页开发技术概述

JSP动态网页开发技术概述

2020年07月23日  | 移动技术网IT编程  | 我要评论

  在动态web项目的开发中,经常需要动态生成html内容(如系统中的当前在线人数需要动态生成)。如果使用servlet实现html页面数据的统计,则需要使用大量的输出语句。同时,如果静态内容和动态内容混合在一起,那么也将导致程序非常臃肿。为了客服servlet的这些缺点,oracle(sun)公司推出了jsp技术。

1.jsp概述

  jsp(java server pages)是建立在servlet规范之上的动态网页开发技术,其实质是一个简化的servlet。在jsp文件中,html和java代码共同存在,其中,html代码用于实现网页中静态内容的显示,java代码用于实现网页中动态内容的实现。为了和传统的html有所区别,jsp文件扩展名为jap。

  jsp技术所开发的web应用程序是基于java的,其具有以下特征:

  (1)预编译

  预编译指在用户第一次通过浏览器访问jsp页面时,服务器将对jsp页面代码进行编译,并且仅指向一次编译。编译好的代码将被保存,在用户下一次访问时会直接执行编译好的代码。这样不仅节约了服务器的cpu资源,还大幅度提升了客户端的访问速度。

  (2)业务代码相分离

  在使用jsp技术开发web应用时,可以将界面的开发和应用程序的开发分离。

  (3)组件重用

  jsp可以使用javabean编写业务组件,也就是使用一个javabean类封装业务处理代码或者将其作为一个数据存储模型,在jsp页面甚至整个项目中,都可以重复使用这个javabean,同时,javabean也可以应用带其他java应用程序中。

  (4)跨平台

  由于jsp是基于java语言的,它可以使用java api,所有它也是跨平台的,可以应用与不同的系统,如windows和linux。

jsp 运行原理

  jsp的工作模式是请求/响应模式,客户端首先发出http请求,jsp程序收到请求后将进行处理并返回处理结果。在一个jsp文件第一次请求时,jsp引擎(容器)把该jsp文件转化成一个servlet,而这个引擎本身也是一个servlet。

  jsp运行过程:
  (1)客户端发出请求,请求访问jsp文件。

  (2)jsp容器先将jsp文件转化成一个java源文件(java servlet源程序),在转换过程中,如果发现jsp文件存在任何语法错误,则中断转换过程,并向服务器和客户端返回出错信息。

  (3)如果转换成功,则jsp容器会将生成的java源文件编译成相应的字节码文件*.class。该class文件就是一个servlet,servlet容器会像处理其他servlet一样处理它。

  (4)有servlet容器加载转换后的servlet类(class文件)创建该servlet(jsp页面的转换结果)的实例,并执行servlet的jspinit()方法。jspinit()方法在servlet的整个生命周期只会执行一次。

  (5)执行jspservice()方法处理客户端的请求。对于每一个请求,jsp容器都会创建一个新的线程处理它。如果多个客户端同时请求该jsp文件,则jsp容器会创建多个线程,使每一个客户端请求都对应一个线程。

  (6)如果jsp文件被修改了,则服务器将根据设置决定是否对该文件重新进行编译,如果需要重新编译,则使用重新编译后的结果取代内存中的servlet,并继续上述处理过程。需要注意的是,虽然jsp效率很高,但在第一次调用时往往需要转换和编译,所以会产生一些轻微的延迟。

  (7)如果系统出现资源不足等问题,jsp容器可能会以某种不确定的方式将servlet从内存中移除,发生这种情况时,首先会调用jspdestroy()方法,然后servlet实例会被作为垃圾进行处理。

  (8)当请求处理完成后,响应对象由jsp容器接收,并将html格式的响应信息送回客户端。

  因此:浏览器向服务器发送请求,不管访问的是什么资源啊,其实都是在访问servlet,所有当访问一个jsp页面时,其实也是在访问一个servlet,服务器在执行jsp的时候,首先把jsp翻译成一个servlet,所有访问jsp时,其实不是在访问jsp,而是在访问jsp翻译过后的那个servlet。例如:

c1.jsp

<%@ page language="java" contenttype="text/html; charset=utf-8"
 pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>insert title here</title>
</head>
<body>
 this is my first jsp
 <%
  out.print("你好啊c1");
  %>
</body>
</html>

当我们通过浏览器(http://localhost:8080/day11_01_jsp(项目名称)/c1.jsp)访问c1.jsp时,服务器首先将c1.jsp翻译成一个c1_jsp.class,在tomcat服务器的work\catalina\localhost\项目名\org\apache\jsp目录下可以看到c1_jsp.class的源代码。(1.jsp翻译成_1_jsp.class)

c1_jap.java的代码:

/*
 * generated by the jasper component of apache tomcat
 * version: apache tomcat/7.0.52
 * generated at: 2018-10-05 08:32:50 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.*;

public final class c1_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("<!doctype html public \"-//w3c//dtd html 4.01 transitional//en\" \"http://www.w3.org/tr/html4/loose.dtd\">\r\n");
  out.write("<html>\r\n");
  out.write("<head>\r\n");
  out.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\r\n");
  out.write("<title>insert title here</title>\r\n");
  out.write("</head>\r\n");
  out.write("<body>\r\n");
  out.write("\tthis is my first jsp \r\n");
  out.write("\t");

  out.print("你好啊c1");

  out.write("\r\n");
  out.write("</body>\r\n");
  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 { 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);
 }
 }
}

  我们可以看到,c1_jsp这个类是继承 org.apache.jasper.runtime.httpjspbase这个类的,通过查看tomcat服务器的源代码,可以知道在apache-tomcat-6.0.20-src\java\org\apache\jasper\runtime目录下存httpjspbase这个类的源代码文件,如下图所示:

httpjsbase这个类的源代码:

/*
 * licensed to the apache software foundation (asf) under one or more
 * contributor license agreements. see the notice file distributed with
 * this work for additional information regarding copyright ownership.
 * the asf licenses this file to you under the apache license, version 2.0
 * (the "license"); you may not use this file except in compliance with
 * the license. you may obtain a copy of the license at
 *
 *  http://www.apache.org/licenses/license-2.0
 *
 * unless required by applicable law or agreed to in writing, software
 * distributed under the license is distributed on an "as is" basis,
 * without warranties or conditions of any kind, either express or implied.
 * see the license for the specific language governing permissions and
 * limitations under the license.
 */

package org.apache.jasper.runtime;

import java.io.ioexception;

import javax.servlet.servletconfig;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.jsp.httpjsppage;

import org.apache.jasper.compiler.localizer;

/**
 * this is the super class of all jsp-generated servlets.
 *
 * @author anil k. vijendran
 */
public abstract class httpjspbase extends httpservlet implements httpjsppage {

 private static final long serialversionuid = 1l;

 protected httpjspbase() {
 }

 @override
 public final void init(servletconfig config)
  throws servletexception
 {
  super.init(config);
  jspinit();
  _jspinit();
 }

 @override
 public string getservletinfo() {
  return localizer.getmessage("jsp.engine.info");
 }

 @override
 public final void destroy() {
  jspdestroy();
  _jspdestroy();
 }

 /**
  * entry point into service.
  */
 @override
 public final void service(httpservletrequest request, httpservletresponse response)
  throws servletexception, ioexception
 {
  _jspservice(request, response);
 }

 @override
 public void jspinit() {
 }

 public void _jspinit() {
 }

 @override
 public void jspdestroy() {
 }

 protected void _jspdestroy() {
 }

 @override
 public abstract void _jspservice(httpservletrequest request,
          httpservletresponse response)
  throws servletexception, ioexception;
}

  httpjspbase类是继承httpservlet的,所以httpjspbase类是一个servlet,而c1_jsp又是继承httpjspbase类的,所以c1_jsp类也是一个servlet,所以当浏览器访问服务器上的c1.jsp页面时,其实就是在访问c1_jsp这个servlet,c1_jsp这个servlet使用_jspservice这个方法处理请求。

2.jsp的基本语法

2.1 jsp模板元素

  网页的静态内容。如:html标签和文本。

2.2 jsp脚本元素

(1)jsp scriptlets(脚本片断)用于在jsp页面中编写多行java代码。语法:

<%
 java代码(变量、方法、表达式等 )
%>
<%
 int sum=0;//声明变量
 /*编写语句*/
 for (int i=1;i<=100;i++){
  sum+=i;
 }
 out.println("<h1>sum="+sum+"</h1>");
%>

jsp脚本片断中只能出现java代码,不能出现其它模板元素, jsp引擎在翻译jsp页面中,会将jsp脚本片断中的java代码将被原封不动地放到servlet的_jspservice方法中。jsp脚本片断中的java代码必须严格遵循java语法,例如,每执行语句后面必须用分号(;)结束。在一个jsp页面中可以有多个脚本片断,在两个或多个脚本片断之间可以嵌入文本、html标记和其他jsp元素。多个脚本片断中的代码可以相互访问,犹如将所有的代码放在一对<%%>之中的情况。如:out.println(x);单个脚本片断中的java语句可以是不完整的,但是,多个脚本片断组合后的结果必须是完整的java语句。

<%
 for (int i=1; i<5; i++) 
 {
%>
 <h1>http://localhost:8080/javaweb_jsp_study_20140603/</h1>
<%
 }
%>

(2)jsp声明

  jsp页面中编写的所有代码,默认会翻译到servlet的service方法中,而jsp声明中的java代码会被翻译到_jspservice方法外面。

<%!
 java代码:定义变量或者方法
%>

  多个静态代码块、变量和方法可以定义在一个jsp文件中,也可以分别单独定义在多个jsp声明中。

  jsp隐式对象的作用范围仅限于servlet的_japservice方法。所以在jsp声明中不能使用这些隐式对象。

jsp声明案例:

<%!
static {
 system.out.println("loading servlet!");
}

private int globalvar = 0;

public void jspinit(){
 system.out.println("initializing jsp!");
}
%>

<%!
public void jspdestroy(){
 system.out.println("destroying jsp!");
}
%>

(3)jsp 表达式

  jsp脚本表达式(expression)用于将程序数据输出到客户端,他将要输出的变量或者表达式直接封装在以<%= %>标记中,语法为:

<%=expression%>

举例:输出当前系统时间:

<%= new java.util.date() %> 

  jsp引擎在翻译脚本表达式时,会将程序数据转成字符串,然后在相应位置用out.print(...)将数据输给客户端。

  jsp脚本表达式的变量或者表达式后不能有分号(;)。

3.jsp注释

  (1)显式注释:直接使用html风格的注释:<!- - 注释内容- -> 特点:不安全,费流量;html的注释在浏览器中查看源文件的时候是可以看得到的

  (2)隐式注释:直接使用java的注释://、/*……*/

 jsp自己的注释:<%- - 注释内容- -%> 特点:安全,省流量

java注释和jsp注释在浏览器中查看源文件时是看不到注释的内容的

<!--这个注释可以看见-->

<%
 //java中的单行注释

 /*
  java中的多行注释
 */
%>

<%
--jsp自己的注释--%>

参考:

到此这篇关于jsp动态网页开发技术概述的文章就介绍到这了,更多相关jsp动态网页内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网