JSP

it2026-02-14  6

1.JSP是什么? Java Server Pages[jsp]—java服务器页面 【包含有java程序的运行在服务器上的页面{HTML}】 页面—【HTML】----包含有java程序代码----在服务器上运行。

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.text.SimpleDateFormat" import="java.util.Date"%> <!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>Insert title here</title> </head> <body> <% SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM--dd HH:mm:ss E"); String datetime=sdf.format(new Date()); %> <h1>当前系统时间:<% out.write(datetime); %></h1> <h1>当前系统时间:<%=datetime %></h1> <%! String name="zhangsan"; public String getInfo(){ return "这是一个jsp声明"; } public class Student{ public String getStudentInfo(){ return "这是一个java类中的实例方法"; } } %> <h1>name:<%=name %></h1> <h1>getInfo():<%=getInfo() %></h1> <h1>JSP动作包含test.txt文件:<jsp:include page="test.txt"></jsp:include></h1> </body> </html>

运行结果 JSP实际上就是html中包含java代码。用html标记表示静态效果,包含的java代码则处理动态数据。 2.JSP页面中的元素? 2.1 指令 格式:<%@指令名称 属性1=”属性值1”…%> 作用:在jsp页面中声明的一些由浏览器执行的附加信息. 常用指令:page指令 / include指令 / taglib指令 1.page指令—常见属性 1.language=“java”—指定语言 2.contentType=“text/html; charset=utf-8”:设置当前jsp页面的内容类型 3.pageEncoding=“utf-8”:设置当前jsp页面的字符编码 4.import=“java.util.Date”:导入java类库的依赖包【可以出现多个】 5.isELIgnored=“false”—是否启用EL表达式语言 “${表达式}”,false可用,true不可以 6.isErrorPage=“false”—是否是一个错误页面,如果是那么就可以使用exception的JSP内置对象 例如:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isELIgnored="false" isErrorPage="true"%> <!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>Insert title here</title> </head> <body> <h1>page指令--常见属性</h1> <h2>1.language="java"--指定语言</h2> <h2>2.contentType="text/html; charset=utf-8":设置当前jsp页面的内容类型</h2> <h2>3.pageEncoding="utf-8":设置当前jsp页面的字符编码</h2> <h2>4.import="java.util.Date":导入java类库的依赖包【可以出现多个】</h2> <h3>&lt;%@page import="java.text.SimpleDateFormat"<br> import="java.util.Date"%&gt;</h3> <hr> <h3>&lt;%@ page import="java.text.SimpleDateFormat"%&gt;</h3> <h3>&lt;%@ page import="java.util.Date"%&gt;</h3> <h3>123+234=${123+234}</h3> <h2>6.isErrorPage="false"---是否是一个错误页面,如果是那么就可以使用exception的JSP内置对象</h2> <%-- <% exception.printStackTrance(); %> --%> </body> </html>

2.include指令[包含其他的资源进入本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>Insert title here</title> </head> <body> <h1>include指令--常见属性</h1> <h2>include指令--包含其他的资源进入本jsp页面</h2> <h2>file="test.txt":指定被包含的其他的资源的路径</h2> 包含文本信息:<%@include file="test.txt" %><br> 包含html信息:<%@include file="mytest.html" %> 包含jsp信息:<%@include file="test1.jsp" %> </body> </html>

运行结果 3.taglib指令–在jsp页面中使用JSTL【jsp的标准标签库】标签的时候,用来导入标签库 【这个指令暂时不处理,我们在后面学习JSTL时候,会详细介绍】 2.2Java代码片段

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.text.SimpleDateFormat" import="java.util.Date"%> <!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>Insert title here</title> </head> <body> <% SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM--dd HH:mm:ss E"); String datetime=sdf.format(new Date()); %> <h1>当前系统时间:<% out.write(datetime); %></h1> </body> </html>

2.3jsp表达式 格式:<%= 表达式/变量/方法 %> 作用:计算某种表达式的结果,或者变量/方法的结果。 2.4jsp声明 格式:<%! 类/变量/方法 %> 作用:在jsp页面中定义一些自己需要的java变量/方法/类 例如:

<%@ 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>Insert title here</title> </head> <body> <h1>jsp表达式</h1> <h2>计算某种表达式的结果,或者变量/方法的结果。</h2> <h2>算数表达式 123+234=<%=123+234 %></h2> <h2>关系表达式 123>234=<%=123>234 %></h2> <h2>逻辑表达式(123>234)||(123<234)===<%= (123 > 234) || (123 < 234) %></h2> <h1>jsp声明</h1> <h2>在jsp页面中定义一些自己需要的java变量/方法/</h2> <%! String name="zhangsan"; public String getInfo(){ return "hello,wangxing"; } %> <h2>声明中的变量name==<%=name %></h2> <h2>声明中的变量getInfo()==<%= getInfo() %></h2> </body> </html>

运行结果 2.5jsp动作 格式:<jsp:动作名称 动作属性…></jsp:动作名称> 作用:将一些常用的java程序封装,方便调用 include的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>Insert title here</title> </head> <body> <h1>jsp动作</h1> <h2>将一些常用的java程序封装,方便调用</h2> <h2>include的jsp动作--包含其他的资源进入本jsp页面</h2> 包含文本信息:<jsp:include page="test.txt"></jsp:include> 包含html信息:<jsp:include page="mytest.html"></jsp:include> 包含jsp信息:<jsp:include page="test1.jsp"></jsp:include> </body> </html>

forward的jsp动作

<h2>forward的jsp动作--跳转到其他的资源</h2> <jsp:forward page="mytest.html"></jsp:forward>

include指令[<%@include file=”” %>]与include动作[<jsp:include page=""></jsp:include>]有什么区别? 3.JSP页面的执行过程? 1.jsp本质上就是一个Servlet程序。 Jsp在执行的时候会被转换成Servlet程序。 保存位置 F:\20200728\javawebworkspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\TestJSP1\org\apache\jsp

public final class test5_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent, org.apache.jasper.runtime.JspSourceImports { org.apache.jasper.runtime.HttpJspBase继承过javax.servlet.http.HttpServlet 当我们请求一个jsp页面的时候,服务器收到jsp页面的请求以后,会先将被访问的jsp文件转换成Servlet文件,继续编译转换以后的Servlet文件得到Servlet文件对应的字节码文件,接着执行这个编译以后的字节码文件,将运行结果封装到响应对象中,返回给客户端浏览器。 缺点:第一次访问jsp页面会比较慢一些。 4.Servlet与JSP的区别?

最新回复(0)