jsp本质是一个servlet!!!
所以可以在jsp中写java代码,在访问jsp的时候,其实就是响应了一个servlet的请求
附上部分源码:
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; 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, false, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); out = pageContext.getOut(); _jspx_out = out; out.write(" "); out.write(" "); java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy"); request.setAttribute("year", sdf.format(new java.util.Date())); request.setAttribute("tomcatUrl", "http://tomcat.apache.org/"); request.setAttribute("tomcatDocUrl", "/docs/"); request.setAttribute("tomcatExamplesUrl", "/examples/"); out.write(" "); out.write("<!DOCTYPE html> "); out.write("<html lang="en"> "); out.write(" <head> "); out.write(" <meta charset="UTF-8" /> "); out.write(" <title>"); out.print(request.getServletContext().getServerInfo() ); out.write("</title> "); out.write(" <link href="favicon.ico" rel="icon" type="image/x-icon" /> "); out.write(" <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" /> "); out.write(" <link href="tomcat.css" rel="stylesheet" type="text/css" /> "); out.write(" </head> "); out.write(" "); out.write(" <body> "); out.write(" <div id="wrapper"> "); out.write(" <div id="navigation" class="curved container"> "); out.write(" <span id="nav-home"><a href=""); out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false)); out.write("">Home</a></span> "); 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的语法:
jsp模版元素--------》html标签,模版元素
jsp脚本表达式--------》用于向浏览器输出数据
<body> <form action="/day06/DoFormServlet" method="post "> <%@ page import="java.util.Date"%> <% Date date=new Date(); String time=date.toLocaleString(); out.write("out方式输出:"+"<br/>"); out.write(time); %> <br/> <% out.write("<%=方式输出"+"<br/>"); %> <%= time%> </body>
jsp脚本片段--------》如上代码 <% java代码 %>
jsp声明----------》就是<%! java代码%> 这段代码会加到service方法外!
jsp注释-----------》<%- dfsdfsdf -%>
html/xml注释--------》<!-- -->(会打给浏览器,但是不显示,会产生代码垃圾)
jsp指令----》jsp指令是为jsp引擎而设计的
1.page指令
2.Include指令
3.taglib指令
<%@ 指令 属性名=“值” %>
举例:<%@ page import="java.util.Date"%>
不产生可见输出
1.page指令
2.include指令
静态包含的性能会好很多,编译时包含是把所有jsp的编译成一个servlet
动态是把所需要的运行包含,每个jsp都有一个servlet
静态包含:
动态包含;
九大隐士对象:
request
response
session
application
config
page
out
exception
pagecontext
jsp最特殊的是pagecontext:
pagecontext可以调到其他八大隐式对象,所以可以通过这一个对象得到其他八个的值
jsp常用标签:
<jsp:forward page=""></jsp:forward> <jsp:include page=""></jsp:include>
<jsp:param name="" value="">
jsp映射:
把14.jsp映射到15.html,也就是访问15.html看到的是14.jsp