JSP学习总结
Java Server Pages : Java服务器端页面,也和Servlet一样,用于动态Web技术!tomcat中有一个work目录; IDEA中使用Tomcat的会在IDEA的tomcat中生产一个work目录,发现页面转变成了Java程序!1、JSP源码分析JSP 本质上就是一个Servlet//jsp变成的java程序//初始化public void _jspInit() { }//销
·
Java Server Pages : Java 服务器端页面,也和 Servlet 一样,用于动态 Web 技术!
tomcat 中有一个 work 目录; IDEA中使用 Tomcat 的会在 IDEA 的 tomcat 中生产一个 work目录,发现页面转变成了 Java 程序!
1、JSP源码分析
JSP 本质上就是一个Servlet
//jsp变成的java程序
//初始化
public void _jspInit() { }
//销毁
public void _jspDestroy() { }
//JSPService
public void _jspService(.HttpServletRequest request,HttpServletResponse response)
JSP内置的对象
final javax.servlet.jsp.PageContext pageContext; //页面上下文
javax.servlet.http.HttpSession session = null; //session
final javax.servlet.ServletContext application; //applicationContext
javax.servlet.ServletConfig config; //config
javax.servlet.jsp.JspWriter out = null; //out
final java.lang.Object page = this; //page:当前
HttpServletRequest request //请求
HttpServletResponse response //响应
输出页面前增加的代码,一下内容jsp中可以直接使用
response.setContentType("text/html"); //设置响应的页面类型
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;
只要是 JAVA 代码就会原封不动的输出;
如果是 HTML代码,就会被转换为: out . write ( "<html>\r\n" );
这样的格式,输出到前端!
2、JSP基础语法
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
JSP表达式——输出到客户端
<%--JSP表达式 作用:用来将程序的输出,输出到客户端 <%= 变量或者表达式%>--%>
<%= new java.util.Date()%>
jsp脚本片段
<%--jsp脚本片段--%>
<%
int sum = 0;
for (int i = 1; i <=100 ; i++) {
sum+=i;
}
out.println("<h1>Sum="+sum+"</h1>");
%>
<%
for (int i = 0; i < 5; i++) {
%>
<h1> Hello,World <%=i%> </h1>
<%
}
%>
JSP声明
<%!
static {
System.out.println("Loading Servlet!");
}
private int globalVar = 0;
public void kuang(){
System.out.println("进入了方法!");
}
%>
JSP 声明:会被编译到 JSP 生成 Java 的类中!其他的,就会被生成到 _jspService 方法中!
<%%>
<% = %>
<% ! %>
<% -- 注释 -- %>
JSP 的注释,不会在客户端显示, HTML(<!-- -->) 就会!
3、JSP指令
<% @page args .... %>
定制错误页面
也可在web.xml中配置
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
<% @include file = "" %>
<% -- @include 会 将两个页面合二为一 -- %>
<% @include file = "common/header.jsp" %>
<h1> 网页主体 </h1>
<% @include file = "common/footer.jsp" %>
jSP 标签
jsp : include : 拼接页面,本质还是三个
<jsp:include page = "/common/header.jsp" />
<h1> 网页主体 </h1>
<jsp:include page = "/common/footer.jsp" />
4、JSP9大内置对象
- PageContext 存东西
- Request 存东西
- Response
- Session 存东西
- Application 【SerlvetContext】 存东西
- config 【SerlvetConfig】
- out
- page
- exception
//保存的数据只在一个页面中有效
pageContext.setAttribute("name1","1号");
//保存的数据只在一次请求中有效,请求转发会携 带这数据
request.setAttribute("name2","2号");
//保存的数据只在一次会话中有效,从打开浏览器 到关闭浏览器
session.setAttribute("name3","3号");
//保存的数据只在服务器中有效,从打开服 务器到关闭服务器
application.setAttribute("name4","4号");
request :客户端向服务器发送请求,产生的数据,用户看完就没用了,比如:新闻,用户看完没用的!
session :客户端向服务器发送请求,产生的数据,用户用完一会还有用,比如:购物车;
application :客户端向服务器发送请求,产生的数据,一个用户用完了,其他用户还可能使用,比如: 聊天数据;
5、JSP标签、JSTL标签、EL表达式
EL表达式:${}
- 获取数据
- 执行运算
- 获取web开发的常用对象
需要的依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
JSP标签
<jsp:forward page="/demo01.jsp">
<jsp:param name="value1" value="value1"/>
<jsp:param name="value2" value="value2"/>
</jsp:forward>
参数可以在转发页面显示
<%=request.getParameter("value1")%>
JSTL 表达式
JSTL 标签库的使用就是为了弥补 HTML 标签的不足;它自定义许多标签,可以供我们使用,标签的功能和 Java代码一样!
格式化标签
SQL 标签
XML 标签
核心标签 (掌握部分)
在 Tomcat 也需要引入 jstl 的包,否则会报错: JSTL 解析错误
<c:if test="${param.username=='admin'}" var="isAdmin">
<c:out value="欢迎你"/>
</c:if>
<c:set var="scoer" value="90"/>
<c:choose>
<c:when test="${scoer>=90}">优秀</c:when>
<c:when test="${scoer>=80}">良好</c:when>
<c:when test="${scoer>=70}">一般</c:when>
<c:when test="${scoer>=60}">合格</c:when>
<c:when test="${scoer<60}">不及格</c:when>
</c:choose>
<%--var:每一次遍历的变量--%>
<%--items:遍历的对象--%>
<%--begin:开始下标--%>
<%--end:结束--%>
<%--step:步长--%>
<c:forEach var="people" items="${list}" begin="1" end="3" step="2">
<c:out value="${people}"/> <br>
</c:forEach>
jstl掌握核心即可,以后用到直接找直接用
更多推荐
所有评论(0)