跳转到目录
1、 Servlet动态网页 = Java代码(主) + html代码(辅): (在Servlet中编写html代码)html
Servlet擅长的事情:java
2、 JSP动态网页 = Java代码(辅) + html代码(主): (在html中编写java代码)web
Servlet不擅长作页面输出,JSP最擅长作页面输出!
责任分离思想:各自作各自最擅长的事情.apache
跳转到目录
JSP(Java Server Page): Java的服务页面,Java动态网页.
JSP的本质就是Servlet.
tomcat
<%-- Created by IntelliJ IDEA. User: Sunny Date: 2020/1/28 Time: 17:41 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>The first JSP application</title> </head> <body> 北京时间:<%=new java.util.Date().toLocaleString()%> </body> </html>
IDEA将hello.jsp文件编译好后存在C:\Users\Sunny\.IntelliJIdea2019.3\system\tomcat\_coderZYGui\work\Catalina\localhost\ROOT\org\apache\jsp\jspdemo\hello
目录下服务器
// hello_jsp.java 文件中 _jspService方法 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; // 在Servlet代码中输出静态网页内容 out.write("\r\n"); out.write("\r\n"); out.write("<html>\r\n"); out.write("<head>\r\n"); out.write(" <title>The first JSP application</title>\r\n"); out.write("</head>\r\n"); out.write("<body>\r\n"); out.write(" 北京时间:"); out.print(new java.util.Date().toLocaleString()); out.write("\r\n"); out.write("</body>\r\n"); out.write("</html>\r\n");
从上面能够看到:JSP的本质就是一个Servlet.session
跳转到目录
JSP语法:app
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>The first JSP application</title> </head> <body> 北京时间:<%=new java.util.Date().toLocaleString()%> <%-- JSP注释: 不会翻译到Servlet中 --%> <%--JSP的Java脚本表达式--%> <% // Java代码 String name = "ZYGui"; boolean flag = true; %> <%--JSP中的Java脚本片断--%> <%=name%> <%--JSP的声明--%> <%! private String user; public void doWork() { } %> <%--很麻烦--%> <% int num = 10; if (num > 5){ %> num大于5 <% }else if(num < 5){ %> num小于5 <% } else { %> num等于5 <% } %> </body> </html>
跳转到目录
标准指令: 设定JSP网页的总体配置信息
特色:jsp
指令的语法格式:
<%@ 指令名称 属性名=“属性值” 属性名=“属性值” …%>svg
跳转到目录
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
page指令: 表示JSP页面相关的配置信息
经常使用属性:
language
: 表示在JSP中编写的脚本的语言.(只能是java)contentType
: 表示JSP输出的MIME类型和编码.等价于 response.setContentType(“text/html;charset=utf-8”);pageEncoding
: 表示JSP输出的编码;等价于response.setCharacterEncoding(“utf-8”);<!-- 全局的错误页面 --> <error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page>
<% @include file="被包含的页面文件" %>
<jsp: include page="被包含的页面文件" />
注意:在实际开始中一般将被包含的jsp页面的后缀名设置为jspf.
<%@taglib uri="标签名称空间" prefix="前缀"%>
<%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>