JSP(Java Server Page)页面是指扩展名为 .jsp 的文件。
JSP是服务器端运行的页面,不像html文件直接就可以在浏览器中运行。JSP页面必须部署到web容器中编译成Servlet才能执行,可以说,JSP就是Servlet的一种形式。
事实上,JSP是Servlet发展的产物,关于JSP和Servlet的关系以后再在博客中介绍。
JSP基本页面由指令标签,HTML标记语言,注释,Java代码,JSP动作标签5个部分组成。
1.JSP指令标签不会产生任何内容输出到网页中,主要用于定义整个JSP页面的相关信息,如使用的语言,导入的类包,指定错误处理页面等。
JSP指令有3种分别为:
page----用于定义整个JSP页面的相关属性,这些属性在JSP被服务器解析成Servlet时会转换成相应的java代码
主要属性有language extends import pageEncoding contentType session 等
include--文件包含指令,它可以在JSP页面中包含另一个文件内容(可以是一段java代码,html代码或JSP页面)
taglib---引用标签库指令
2.HTML标记语言
3 Java代码
语法格式 <% 编写java代码%>
JSP表达式<%=表达式%>可以直接把Java表达式结果输出到JSP页面中。表达式最终结果将被转换为字符串类型。
4 注释 包括html注释JSP注释java代码注释等
5 JSP动作标签,主要用来实现特殊的功能如转发用户请求,包含其他文件 操作JavaBean等。
<jsp:include>包含外部文件的动作标签,可以将另外一个文件的内容包含到当前JSP页面中,可以是静态文本,也可以是动态代码
<jsp:include>动作标签与include指令区别:include指令包含的资源为静态,如果将jsp动态内容用include指令包含,也会被当做静态资源包含到当前页面中
即是静态包含,先包含再编译成class文件(静态包含不能传递参数)。而<jsp:include>动作标签是动态包含,对于一个动态页面先编译,后包含,即当前页面与被包含的资源是两个独立的实体。被包含JSP页面会对包含它的JSP页面中的请求对象进行处理,将处理结果作为当前JSP页面中的处理内容,与当前页面内容一起发送到客户端。
<jsp:forward>请求转发标签,将当前页面请求转发给其他Web资源。
<jsp:param>设置参数标签,可作为其他标签子标签,为其他标签传递参数。如
<jsp:forward page="login.jsp"> <jsp:param name="userName" value="admin"> </jsp:forward>
在JSP页面第一次被请求时,JSP引擎会将JSP原始文件转换为Servlet源代码,然后调用Java编译器,编译成Servlet,并在Servlet引擎中执行,容器将生成的页面返回给客户端显示。当再次有请求时,JSP引擎会根据JSP文件是否存在更新,如果没有更新,运行Servlet;如果存在更新,就会重新执行转换和编译的过程。JSP的执行性能和Servlet相比,差别只在第一次的执行,当重复调用时,就直接执行第一次产生的Servlet。
如下为index.jsp的一个最简单代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> This is my JSP page. <br> </body> </html>
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/8.0.17 * Generated at: 2015-05-18 12:09:56 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.*; public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent, org.apache.jasper.runtime.JspSourceImports { 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 static final java.util.Set<java.lang.String> _jspx_imports_packages; private static final java.util.Set<java.lang.String> _jspx_imports_classes; static { _jspx_imports_packages = new java.util.HashSet<>(); _jspx_imports_packages.add("javax.servlet"); _jspx_imports_packages.add("java.util"); _jspx_imports_packages.add("javax.servlet.http"); _jspx_imports_packages.add("javax.servlet.jsp"); _jspx_imports_classes = new java.util.HashSet<>(); } 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 java.util.Set<java.lang.String> getPackageImports() { return _jspx_imports_packages; } public java.util.Set<java.lang.String> getClassImports() { return _jspx_imports_classes; } 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 java.lang.String _jspx_method = request.getMethod(); if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) { response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD"); return; } 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'); out.write('\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(" This is my JSP page. <br>\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 { 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); } } }
1客户端发出Request请求
2JSP容器将JSP转译成Servlet代码
3将转译的Servlet代码经过编译后,加载到内存执行
4把结果Request至客户端
原文地址:http://blog.csdn.net/u010498696/article/details/45824513