jsp简介
Posted 1024军团
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsp简介相关的知识,希望对你有一定的参考价值。
一、概念部分
JSP,即Java Server Pages首字母的缩写,Java服务器页面,即运行在服务器端的页面。它是由Sun公司倡导,许多国际性大公司一起参与,建立的一种动态网页技术。JSP技术是在传统的静态网页html页面中插入Java代码片段和JSP标签后形成的一种文件,其后缀名为.jsp。使用JSP开发的Web应用和Java一样是跨平台的,也就是既能在linux上运行,也能在其它操作系统上运行。
JSP文件的本质是Servlet。只不过JSP与Servlet不同的是:JSP是专门用于数据展示的Servlet,其有特殊的的写法。而普通的Servlet是用于完成业务逻辑处理的。由于Servlet是运行在单例多线程环境下的,所以JSP同样也是运行在单例多线程环境下的。
二、JSP规范
将JSP页面翻译为Servlet的过程是由Tomcat完成的。在Tomcat中内置了一个JSP翻译引擎,当第一次访问该JSP页面时,该翻译引擎会将JSP页面翻译为Servlet的.java源文件,再将该.java源文件编译为.class字节码文件进行运行。
SUN公司制定的Java EE规范中包含两个很重要的子规范:Servlet规范和JSP规范。其中JSP规范中就包含了如何将JSP页面翻译为Servlet。例如,JSP页面中的HTML、CSS、javascript及普通文本部分军徽被翻译到out.write()中。Tomcat中的JSP翻译引擎,就遵循了这个JSP规范。
三、jsp页面组成部分图解
四、示例
4.1我的第一个jsp页面
发布到tomcat后该页面存放在:..\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\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=UTF-8"> <title>first_jsp</title> </head> <body> My First jsp! </body> </html>
4.2经Tomcat采用JSP规范将上面jsp页面翻译成的.java源码
存放地址:..\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\work\\Catalina\\localhost\\jsp\\org\\apache\\jsp
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/7.0.85 * Generated at: 2019-01-22 16:01: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 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 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("\\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=UTF-8\\">\\r\\n"); out.write("<title>first_jsp</title>\\r\\n"); out.write("</head>\\r\\n"); out.write("<body>\\r\\n"); out.write("My First jsp!\\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 { 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); } } }
以上是关于jsp简介的主要内容,如果未能解决你的问题,请参考以下文章