JSP

1.各个指令的含义

<%@page指令

contentType = "text/html"; charset = UTF-8

相当于response.setContentType("text/html; charset = UTF-8");

上面通知浏览器以UTF-8进行中文编码

pageEncoding = “UTF-8”

如果jsp文件中出现了中文,这些中文使用UTF-8进行编码

import = “java.util.*”

导入其他类,如果导入多个类,彼此用,逗号隔开,像这样import  = "java.util.*,java.sql.*"

二、执行过程

1.把hello.jsp转译为hello_jsp.java

2.hello_jsp.java是一个servlet

3.把hello_jsp.java编译为hello_jsp.class

4.执行hello_jsp,生成html

5.通过http协议把html响应返回给浏览器

三、页面元素总结

1.静态内容

  就是html,css,javascript等内容

2.指令

  以<%@开始 %>结尾,比如<%@page  import = "java.util.*" %>

3.表达式<%= %>

  用于输出一段html

4.scriptlet

  在<% %>之间,可以写任何java代码

5.声明

在<%!%>之间可以声明字段或者方法。但是不建议这么做

6.动作

  <jsp:include page = "Filename">在jsp页面中包含另一个页面

7.注释<%--   --%>

不同于html的注释<!-- -->通过jsp的注释,浏览器也看不到相应的代码,相当于在servlet中注释掉了

四、循环

<%

  List<String> words = new ArrayList<String>();

  words.add("today");

  words.add("is");

  words.add("a");

  words.add("great");

  words.add("day");

%>

<table width ="200px" align = "center" border = "1" cellspace = "0">

<%for(String word : words)  {%>

<tr>

  <td><%=word%></td>

</tr>

<%}%>

</table>

五、include

1.首先准备一个footer.jsp

<hr>

<p style = "text-align:center">copyright@2016

</p>

2.通过指令

<%@include file = "footer.jsp" %>在其他jsp文件中包含footer.jsp页面

3.通过动作

<jsp: include page = "footer.jsp"/>

在hello.jsp中包含该页面

区别:动作的方式会产生一个footer_jsp.java独立存在

hello_jsp.java会在服务器访问footer_jsp.java,然后把返回的结果,嵌入到响应中

例子:

hello.jsp中:

<%@page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>

你好 JSP
<%=new Date().toLocaleString()%>

<jsp:include page="footer.jsp">

<jsp:param name = "year" value = "2017"/>
</jsp:include>

在footer.jsp中

<hr>

  <p style = "text-align:center">copyright@<%= request.getParameter("year")%>

</p>

原文地址:https://www.cnblogs.com/zxj-262410/p/8545547.html