发布时间:2024-01-24阅读(3)
JSP全名为Java Server Pages,Java服务器页面。JSP是一种基于文本的程序,其特点就是html和Java代码共同存在!
为什么需要JSPJSP是为了简化Servlet的工作出现的替代品,Servlet输出HTML非常困难,JSP就是替代Servlet输出HTML的。
简单使用一下JSPString s = "HelloWorda";out.println(s);
JSP也是Servlet,运行时只有一个实例,JSP初始化和销毁时也会调用Servlet的init()和destroy()方法。另外,JSP还有自己初始化和销毁的方法
JSP代码可以分为两部分:
JSP脚本
<jsp:scriptlet> String s = "HelloWorld"; out.println(s);</jsp:scriptlet>
JSP注释
<%--这是JSP注释--%><%--%>//这是java的当行注释///*这是java的多行注释*//**/
JSP指令
JSP指令用来声明JSP页面的相关属性,例如编码方式、文档类型等等
JSP指令的语法:
<%@指令 属性名="值" %>
page指令
<%@ 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>
include指令
<%@ 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>
taglib指令
JSP行为
JSP行为(JSP Actions)是一组JSP内置的标签,只书写少量的标记代码就能够使用JSP提供丰富的功能,JSP行为是对常用的JSP功能的抽象和封装。
为什么我不把它直接称为JSP标签呢?我把这些JSP内置的标签称之为JSP行为,能够和JSTL标签区分开来。当然了,你也可以把它称之为JSP标签,你不要搞混就行了。我个人喜欢把这些JSP内置标签称之为JSP行为。
include行为
<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>
param行为
forward行为
<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>
javaBean行为
<jsp:useBean id=""/><jsp:setProperty name="" property=""/><jsp:getProperty name="" property=""/>
,文章来源:https://dwz.cn/OtXvyvh3
作者:Java3y
欢迎分享转载→http://www.umpkq.cn/read-238054.html
下一篇:花呗额度怎么分享
Copyright ? 2024 有趣生活 All Rights Reserve吉ICP备19000289号-5 TXT地图