JSP全名为Java Server Pages,java服务器页面。JSP是一种基于文本的程序,其特色就是HTML和Java代码共同存在!html
JSP是为了简化Servlet的工做出现的替代品,Servlet输出HTML很是困难,JSP就是替代Servlet输出HTML的。java
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>简单使用JSP</title> </head> <body> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>简单使用JSP</title> </head> <body> <% String s = "HelloWorld"; out.println(s); %> </body> </html>
package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import java.util.Date; public final class _1_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory(); private static java.util.List<String> _jspx_dependants; private javax.el.ExpressionFactory _el_expressionfactory; private org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.List<String> 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 HttpServletRequest request, final HttpServletResponse response) throws java.io.IOException, ServletException { final PageContext pageContext; HttpSession session = null; final ServletContext application; final ServletConfig config; JspWriter out = null; final Object page = this; JspWriter _jspx_out = null; 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("<html>\r\n"); out.write("<head>\r\n"); out.write(" <title>简单使用JSP</title>\r\n"); out.write("</head>\r\n"); out.write("<body>\r\n"); String s = "HelloWorda"; out.println(s); out.write("\r\n"); out.write("</body>\r\n"); out.write("</html>\r\n"); } catch (Throwable t) { if (!(t instanceof 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); } } }
out.write("\r\n"); out.write("\r\n"); out.write("<html>\r\n"); out.write("<head>\r\n"); out.write(" <title>简单使用JSP</title>\r\n"); out.write("</head>\r\n"); out.write("<body>\r\n");
String s = "HelloWorda"; out.println(s);
JSP也是Servlet,运行时只有一个实例,JSP初始化和销毁时也会调用Servlet的init()和destroy()方法。另外,JSP还有本身初始化和销毁的方法web
public void _jspInit() { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } public void _jspDestroy() { }
JSP代码能够分为两部分:express
JSP脚本有三种方式:apache
<jsp:scriptlet> String s = "HelloWorld"; out.println(s); </jsp:scriptlet>
<%--这是JSP注释--%> <%--%> //这是java的当行注释 // /*这是java的多行注释*/ /**/
JSP指令用来声明JSP页面的相关属性,例如编码方式、文档类型等等浏览器
JSP指令的语法:tomcat
<%@指令 属性名="值" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page contentType="application/msword;charset=UTF-8" language="java" %> <html> <head> <title>简单使用JSP</title> </head> <body> 1111 </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="error.jsp" %> <html> <head> <title>该页面出错了!</title> </head> <body> <%--模拟页面出错了!!!--%> <% int result = 2 / 0; %> 你好呀 </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %> <html> <head> <title>友好提示页面</title> </head> <body> 服务器正忙着呢! </body> </html>
<error-page> <error-code>404</error-code> <location>/error.jsp</location> </error-page> <error-page> <exception-type>java.lang.NullPointerException</exception-type> <location>/error.jsp</location> </error-page>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>页头</title> </head> <body> 我是页头 <br> <br> <br> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>页尾</title> </head> <body> 我是页尾 </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>包含页头和页尾进来</title> </head> <body> <%@include file="head.jsp" %> <%@include file="foot.jsp" %> </body> </html>
JSP行为(JSP Actions)是一组JSP内置的标签,只书写少许的标记代码就可以使用JSP提供丰富的功能, JSP行为是对经常使用的JSP功能的抽象和封装。
为何我不把它直接称为JSP标签呢?我把这些JSP内置的标签称之为JSP行为,可以和JSTL标签区分开来。固然了,你也能够把它称之为JSP标签,你不要搞混就好了。我我的喜欢把这些JSP内置标签称之为JSP行为。服务器
<jsp:include page=""/>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>包含页头和页尾进来</title> </head> <body> <jsp:include page="head.jsp"/> <jsp:include page="foot.jsp"/> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>页头</title> </head> <body> <% String s = "zhongfucheng"; %> 我是页头呀 <br> <br> <br> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>页尾</title> </head> <body> <% String s = "zhongfucheng"; %> 我是页尾呀 </body> </html>
<jsp:forward page=""/>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>访问1.jsp就跳转到head.jsp</title> </head> <body> <jsp:forward page="head.jsp"/> </body> </html>
<jsp:forward page="head.jsp"> <jsp:param name="username" value="zhongfucheng"/> </jsp:forward>
<% String ss = request.getParameter("username"); %> 获取到的参数是: <%=ss%>
directive的中文意思就是指令。该行为就是替代指令<%@%>的语法的微信
<jsp:directive.include file="head.jsp"></jsp:directive.include> <jsp:directive.include file="foot.jsp"></jsp:directive.include>
<jsp:scriptlet>
替代<%%>是一样一个道理JSP还提供了操做javaBean对象的行为,在这里就不详细说明了,后面会讲到的!如今记住JSP提供了javaBean行为来操做简单类便可!session
<jsp:useBean id=""/>
<jsp:setProperty name="" property=""/>
<jsp:getProperty name="" property=""/>
若是文章有错的地方欢迎指正,你们互相交流。习惯在微信看技术文章的同窗,能够关注微信公众号:Java3y