JavaWeb-11 JSP&EL表达式

JSP

四、JSP语法(学好的关键:相应的Servlet)

JavaWeb-10 总结:session技术也是cookie的一种。server给浏览器创建一个篮子,并加上编号,这编号会存储到client上,当client再次訪问server时。server会读取client的ID号。假设server找得到,就在篮子中拿出该client的session,若没有就新建一个

重点:URL重写。

1、JSP模版元素

  1. JSP模板元素:HTML页面
  2. JSP页面中的HTML内容称之为JSP模版元素。
  3. JSP模版元素定义了网页的基本骨架。即定义了页面的结构和外观。

2、JSP表达式

  1. JSP脚本表达式(expression)用于将程序数据输出到client
  2. 语法:<%= 变量或表达式 %>
  3. 举例:当前时间:<%= new java.util.Date() %>
  4. JSP引擎在翻译脚本表达式时,会将程序数据转成字符串,然后在对应位置用out.print(…) 将数据输给client
  5. JSP脚本表达式中的变量或表达式后面不能有分号(;)。
  6. 注意:现实中不同意这么写,那是html,是美工开发框架用的。
  7. 写这么多java代码没意义。正规开发中不同意出现Jsp脚本

项目架构:

下面实验要使用到的User类:

  1. public class User {
  2. private String id ;
  3. private String username ;
  4. private int age ;
  5. public User() {
  6. }
  7. public User(String id, String username, int age) {
  8. super();
  9. this.id = id;
  10. this.username = username;
  11. this.age = age;
  12. }
  13. public String getId() {
  14. return id;
  15. }
  16. public void setId(String id) {
  17. this.id = id;
  18. }
  19. public String getUsername() {
  20. return username;
  21. }
  22. public void setUsername(String username) {
  23. this.username = username;
  24. }
  25. public int getAge() {
  26. return age;
  27. }
  28. public void setAge(int age) {
  29. this.age = age;
  30. }
  31. }

实验:1.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>输出表达式</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <%
  22. //String name = "张三丰" ;
  23. request.setAttribute("name", "张三丰") ;
  24. String name = (String) request.getAttribute("name") ;
  25. out.write(name) ;
  26. %>
  27. <br>
  28. :
  29. <%=name %>
  30. </body>
  31. </html>

在浏览器输入http://localhost:8080/day1100jsp/1.jsp,IE结果:

3、JSP脚本片断

a、JSP脚本片断(scriptlet)用于在JSP页面中编写多行Java代码。语法:

  1. <%
  2. 多行java代码
  3. %>
  4. 注意:JSP脚本片断中仅仅能出现java代码。不能出现其他模板元素, JSP引擎在翻译JSP页面中,会将JSP脚本片断中的Java代码将被原封不动地放到Servlet的_jspService方法中。
  5. JSP脚本片断中的Java代码必须严格遵循Java语法,比如。每运行语句后面必须用分号(;)结束。

b、在一个JSP页面中能够有多个脚本片断,在两个或多个脚本片断之间能够嵌入文本、HTML标记和其它JSP元素。

  1. 举例:
  2. <%
  3. int x = 10;
  4. out.println(x);
  5. %>
  6. <p>这是JSP页面文本</p>
  7. <%
  8. int y = 20;
  9. out.println(y);
  10. %>
  11. 多个脚本片断中的代码能够相互訪问。宛如将全部的代码放在一对<%%>之中的情况。如:out.println(x);
  12. 正规开发中的JSP中不应出现java脚本:标签封装

c、单个脚本片断中的Java语句能够是不完整的,可是,多个脚本片断组合后的结果必须是完整的Java语句。比如:

  1. <%
  2. for (int i=1; i<5; i++)
  3. {
  4. %>
  5. <H1>www.it315.org</H1>
  6. <%
  7. }
  8. %>

实验:2.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>jsp脚本片段</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <table border = 1>
  22. <tr>
  23. <td>编号</td>
  24. <td>姓名</td>
  25. <td>年龄</td>
  26. </tr>
  27. <%
  28. List<User> list = new ArrayList<User>() ;
  29. list.add(new User("1","张三丰",20)) ;
  30. list.add(new User("2","张无忌",23)) ;
  31. list.add(new User("3","张翠山",25)) ;
  32. list.add(new User("4","张国老",28)) ;
  33. /*out.write("<table border = 1>") ;
  34. out.write("<tr><td>编号</td><td>姓名</td><td>年龄</td></tr>") ;
  35. for(int i = 0 ;i <list.size() ;i++){
  36. User u = list.get(i) ;
  37. out.write("<tr><td>") ;
  38. out.write(u.getId()) ;
  39. out.write("</td><td>") ;
  40. out.write(u.getUsername()) ;
  41. out.write("</td><td>") ;
  42. out.write(u.getAge() + "") ;
  43. out.write("</td></tr>") ;
  44. }*/
  45. // out.write("</table>") ;
  46. for(int i = 0 ;i<list.size() ;i++){
  47. User u = list.get(i) ;
  48. %>
  49. <tr>
  50. <td><%=u.getId() %></td>
  51. <td><%=u.getUsername() %></td>
  52. <td><%=u.getAge() + "" %></td>
  53. </tr>
  54. <%
  55. }
  56. %>
  57. </table>
  58. </body>
  59. </html>

以上的底层Servlet源代码例如以下:

2jsp.java

  1. package org.apache.jsp;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. import javax.servlet.jsp.*;
  5. import java.util.*;
  6. import com.heima.bean.*;
  7. public final class _2_jsp extends org.apache.jasper.runtime.HttpJspBase
  8. implements org.apache.jasper.runtime.JspSourceDependent {
  9. private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
  10. private static java.util.List _jspx_dependants;
  11. private javax.el.ExpressionFactory _el_expressionfactory;
  12. private org.apache.AnnotationProcessor _jsp_annotationprocessor;
  13. public Object getDependants() {
  14. return _jspx_dependants;
  15. }
  16. public void _jspInit() {
  17. _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
  18. _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
  19. }
  20. public void _jspDestroy() {
  21. }
  22. public void _jspService(HttpServletRequest request, HttpServletResponse response)
  23. throws java.io.IOException, ServletException {
  24. PageContext pageContext = null;
  25. HttpSession session = null;
  26. ServletContext application = null;
  27. ServletConfig config = null;
  28. JspWriter out = null;
  29. Object page = this;
  30. JspWriter _jspx_out = null;
  31. PageContext _jspx_page_context = null;
  32. try {
  33. response.setContentType("text/html;charset=UTF-8");
  34. pageContext = _jspxFactory.getPageContext(this, request, response,
  35. null, true, 8192, true);
  36. _jspx_page_context = pageContext;
  37. application = pageContext.getServletContext();
  38. config = pageContext.getServletConfig();
  39. session = pageContext.getSession();
  40. out = pageContext.getOut();
  41. _jspx_out = out;
  42. out.write('\r');
  43. out.write('\n');
  44. String path = request.getContextPath();
  45. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  46. out.write("\r\n");
  47. out.write("\r\n");
  48. out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
  49. out.write("<html>\r\n");
  50. out.write(" <head>\r\n");
  51. out.write(" <base href=\"");
  52. out.print(basePath);
  53. out.write("\">\r\n");
  54. out.write(" \r\n");
  55. out.write(" <title>jsp脚本片段</title>\r\n");
  56. out.write(" \r\n");
  57. out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");
  58. out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");
  59. out.write("\t<meta http-equiv=\"expires\" content=\"0\"> \r\n");
  60. out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");
  61. out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");
  62. out.write("\t<!--\r\n");
  63. out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");
  64. out.write("\t-->\r\n");
  65. out.write("\r\n");
  66. out.write(" </head>\r\n");
  67. out.write(" \r\n");
  68. out.write(" <body>\r\n");
  69. out.write(" \t\t<table border = 1>\r\n");
  70. out.write(" \t\t <tr>\r\n");
  71. out.write("\t\t\t\t<td>编号</td>\r\n");
  72. out.write("\t\t\t\t<td>姓名</td>\r\n");
  73. out.write("\t\t\t\t<td>年龄</td>\r\n");
  74. out.write("\t\t\t</tr>\r\n");
  75. out.write("\t\t");
  76. List<User> list = new ArrayList<User>() ;
  77. list.add(new User("1","张三丰",20)) ;
  78. list.add(new User("2","张无忌",23)) ;
  79. list.add(new User("3","张翠山",25)) ;
  80. list.add(new User("4","张国老",28)) ;
  81. /*out.write("<table border = 1>") ;
  82. out.write("<tr><td>编号</td><td>姓名</td><td>年龄</td></tr>") ;
  83. for(int i = 0 ;i <list.size() ;i++){
  84. User u = list.get(i) ;
  85. out.write("<tr><td>") ;
  86. out.write(u.getId()) ;
  87. out.write("</td><td>") ;
  88. out.write(u.getUsername()) ;
  89. out.write("</td><td>") ;
  90. out.write(u.getAge() + "") ;
  91. out.write("</td></tr>") ;
  92. }*/
  93. // out.write("</table>") ;
  94. for(int i = 0 ;i<list.size() ;i++){
  95. User u = list.get(i) ;
  96. out.write("\r\n");
  97. out.write("\t\t\t<tr>\r\n");
  98. out.write("\t\t\t\t<td>");
  99. out.print(u.getId() );
  100. out.write("</td>\r\n");
  101. out.write("\t\t\t\t<td>");
  102. out.print(u.getUsername() );
  103. out.write("</td>\r\n");
  104. out.write("\t\t\t\t<td>");
  105. out.print(u.getAge() + "" );
  106. out.write("</td>\r\n");
  107. out.write("\t\t\t</tr>\r\n");
  108. out.write("\t\t");
  109. }
  110. out.write("\r\n");
  111. out.write("\t\t</table>\r\n");
  112. out.write(" </body>\r\n");
  113. out.write("</html>\r\n");
  114. } catch (Throwable t) {
  115. if (!(t instanceof SkipPageException)){
  116. out = _jspx_out;
  117. if (out != null && out.getBufferSize() != 0)
  118. try { out.clearBuffer(); } catch (java.io.IOException e) {}
  119. if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
  120. else log(t.getMessage(), t);
  121. }
  122. } finally {
  123. _jspxFactory.releasePageContext(_jspx_page_context);
  124. }
  125. }
  126. }

在浏览器输入:http://localhost:8080/day1100jsp/2.jsp,输出结果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmFpdGhfeWVl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

3.1、JSP声明

  1. <%! name = "啊啊啊"%>//全局变量
  2. <% name = "啊啊啊"%>//局部变量

JSP页面中编写的全部代码,默认会翻译到servlet的service方法中, 而Jsp声明中的java代码被翻译到_jspService方法的外面。语法:

  1. <%!
  2. java代码
  3. %>

所以。JSP声明可用于定义JSP页面转换成的Servlet程序的静态代码块、成员变量和方法 。

多个静态代码块、变量和函数能够定义在一个JSP声明中,也能够分别单独定义在多个JSP声明中。

JSP隐式对象的作用范围仅限于Servlet的_jspService方法,所以在JSP声明中不能使用这些隐式对象。

实验: 3.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>jsp声明</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <%!
  22. String name = "小龙女" ;
  23. public static void demo(){
  24. System.out.print("你好") ;
  25. }
  26. public void demo1(){
  27. System.out.print("你好") ;
  28. }
  29. public class A{}
  30. %>
  31. <%
  32. demo1() ;
  33. demo() ;
  34. %>
  35. </body>
  36. </html>

在浏览器输入:http://localhost:8080/day1100jsp/3.jsp。在server输出的结果例如以下:

总结出来的在jsp编译环境下:事实上就是在HTML页面里,用标签编写的代码。会转成servlet类里的out.write()里的内容输出。而用<%%>括了的代码就会转成servlet类里的正常java代码

4、JSP凝视

  1. <!--HTML凝视-->
  2. //这是java凝视
  3. <%--<%这是jsp凝视%>--%>:这种凝视仅仅能在jsp文档里能够看见。把文档编译成了的servlet类文件就观察不到了,相当于隐身了。

实验: 4.jsp和servlet源代码

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>jsp凝视</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <%
  22. //这是java凝视
  23. %>
  24. <%--<%
  25. out.write("你好") ;
  26. %>--%>
  27. <!-- HTML凝视 -->
  28. </body>
  29. </html>

在浏览器输入:http://localhost:8080/day1100jsp/4.jsp,
可发现Servlet源代码例如以下:

  1. package org.apache.jsp;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. import javax.servlet.jsp.*;
  5. import java.util.*;
  6. import com.heima.bean.*;
  7. public final class _4_jsp extends org.apache.jasper.runtime.HttpJspBase
  8. implements org.apache.jasper.runtime.JspSourceDependent {
  9. private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
  10. private static java.util.List _jspx_dependants;
  11. private javax.el.ExpressionFactory _el_expressionfactory;
  12. private org.apache.AnnotationProcessor _jsp_annotationprocessor;
  13. public Object getDependants() {
  14. return _jspx_dependants;
  15. }
  16. public void _jspInit() {
  17. _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
  18. _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
  19. }
  20. public void _jspDestroy() {
  21. }
  22. public void _jspService(HttpServletRequest request, HttpServletResponse response)
  23. throws java.io.IOException, ServletException {
  24. PageContext pageContext = null;
  25. HttpSession session = null;
  26. ServletContext application = null;
  27. ServletConfig config = null;
  28. JspWriter out = null;
  29. Object page = this;
  30. JspWriter _jspx_out = null;
  31. PageContext _jspx_page_context = null;
  32. try {
  33. response.setContentType("text/html;charset=UTF-8");
  34. pageContext = _jspxFactory.getPageContext(this, request, response,
  35. null, true, 8192, true);
  36. _jspx_page_context = pageContext;
  37. application = pageContext.getServletContext();
  38. config = pageContext.getServletConfig();
  39. session = pageContext.getSession();
  40. out = pageContext.getOut();
  41. _jspx_out = out;
  42. out.write('\r');
  43. out.write('\n');
  44. String path = request.getContextPath();
  45. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  46. out.write("\r\n");
  47. out.write("\r\n");
  48. out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
  49. out.write("<html>\r\n");
  50. out.write(" <head>\r\n");
  51. out.write(" <base href=\"");
  52. out.print(basePath);
  53. out.write("\">\r\n");
  54. out.write(" \r\n");
  55. out.write(" <title>jsp凝视</title>\r\n");
  56. out.write(" \r\n");
  57. out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");
  58. out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");
  59. out.write("\t<meta http-equiv=\"expires\" content=\"0\"> \r\n");
  60. out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");
  61. out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");
  62. out.write("\t<!--\r\n");
  63. out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");
  64. out.write("\t-->\r\n");
  65. out.write("\r\n");
  66. out.write(" </head>\r\n");
  67. out.write(" \r\n");
  68. out.write(" <body>\r\n");
  69. out.write(" \t\t");
  70. //这是java凝视
  71. out.write("\r\n");
  72. out.write(" \t\t");
  73. out.write("\r\n");
  74. out.write(" \t\t<!-- HTML凝视 -->\r\n");
  75. out.write(" </body>\r\n");
  76. out.write("</html>\r\n");
  77. } catch (Throwable t) {
  78. if (!(t instanceof SkipPageException)){
  79. out = _jspx_out;
  80. if (out != null && out.getBufferSize() != 0)
  81. try { out.clearBuffer(); } catch (java.io.IOException e) {}
  82. if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
  83. else log(t.getMessage(), t);
  84. }
  85. } finally {
  86. _jspxFactory.releasePageContext(_jspx_page_context);
  87. }
  88. }
  89. }

5、JSP指令

JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生不论什么可见输出,而仅仅是告诉引擎怎样处理JSP页面中的其余部分。

在JSP 2.0规范中共定义了三个指令:

  1. a. page指令

  1. b. Include指令

  1. c. taglib指令
  2. page指令:演示errorPage:当你的页面出现异常后你须要去哪个页面(错误处理页面)。
  3. 演示include指令:<%@ include file = "6.jsp"%> //静态包括,在6.jsp里不能有和5.jsp同样的一部分指令标签。也能够包括txt文档(a.txt)。
  4. 以上这样的叫静态包括,也是代码级别的包括,所包括的要使用的代码的其它代码能够删除掉!
  5. <%@ taglib %>:引入标签库
  6. 除了import指令标签能够反复。
  7. 、其它指令标签仅仅能写一遍
  8. errorPage也能够在web.xml里配置(优先级高)
  9. <error-page>
  10. <error-code>404</error-code>
  11. <location></location>
  12. </error-page>

实验:

5.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8" errorPage="6.jsp" %>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>jsp指令标签</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <%response.setCharacterEncoding("UTF-8"); %>
  22. <%--<%
  23. // out.write(10/0) ;
  24. %>--%>
  25. aaaaaaaaaaaaaaaa
  26. <%@ include file="6.jsp" %>
  27. <br>
  28. <%@ include file="a.txt" %>
  29. </body>
  30. </html>

6.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8" isErrorPage="true" %>
  2. <body>
  3. server正忙。请一会再来訪问
  4. <%--<%
  5. out.write(exception.getMessage()) ;
  6. %> --%>
  7. </body>

在浏览器输入:http://localhost:8080/day1100jsp/5.jsp。

浏览器输出下面结果:

6、JSP标签

  1. <jsp:include>标签 :动态包括
  2. page属性、它包括了其它路径的jsp时,里面反复的内容不用删除。
  3. 不像静态包括那样。
  4. 静态包括与动态包括之间的优缺点:静态包括(代码级别的包括。效率高)、动态包括(页面级别的包括,),他们结果没差别。
  5. <jsp:forward>标签 :
  6. page属性:请求转发(地址栏是不变的),动态标签中的forward标签是须要配合param标签来使用吗?不一定。
  7. <jsp:param>标签 :
  8. 怎么拿出10.jsp的param属性值,还有中文值们拿出是乱码怎么办?request.setCharacterEncodeing("UTF-8");重要)。主要用来传递參数。
  9. value属性:
  10. name属性

实验:10.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8" %>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>动作指令</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <%--<jsp:include page="11.jsp"></jsp:include>--%>
  22. <% request.setCharacterEncoding("UTF-8") ; %>
  23. <jsp:forward page="11.jsp?a=abc&addr=武当山">
  24. <jsp:param value="bbbbb" name="b"/>
  25. <jsp:param value="张无忌" name="name"/>
  26. </jsp:forward>
  27. </body>
  28. </html>

11.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8" %>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>动作指令</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body><%--
  21. 111111111111111
  22. <%
  23. out.write("你好") ;
  24. %>
  25. --%>
  26. <%
  27. String a = request.getParameter("a") ;
  28. String b = request.getParameter("b") ;
  29. String addr = request.getParameter("addr") ;
  30. String name = request.getParameter("name") ;
  31. // name = new String(name.getBytes("iso-8859-1"),"UTF-8") ;
  32. out.write(a + ":" + b + "<br>") ;
  33. out.write(name + ":" + addr + "<br>") ;
  34. %>
  35. </body>
  36. </html>

在浏览器输入:http://localhost:8080/day1100jsp/10.jsp,结果例如以下:

7、JSP内置对象

a、request

b、response

c、config

d、application

e、exception

f、Session

g、page

h、out

i、pageContext

JSP九大隐式对象表示图

7.1、out隐式对象:

a. out隐式对象用于向client发送文本数据。

b. out对象是通过调用pageContext对象的getOut方法返回的。其作用和使用方法与ServletResponse.getWriter方法返回的PrintWriter对象很相似。

c. JSP页面中的out隐式对象的类型为JspWriter,JspWriter相当于一种带缓存功能的PrintWriter,设置JSP页面的page指令的buffer属性能够调整它的缓存大小,甚至关闭它的缓存。

d. 仅仅有向out对象中写入了内容,且满足例如以下不论什么一个条件时。out对象才去调用ServletResponse.getWriter方法,并通过该方法返回的PrintWriter对象将out对象的缓冲区中的内容真正写入到Servlet引擎提供的缓冲区中:

e. 设置page指令的buffer属性关闭了out对象的缓存功能

f. out对象的缓冲区已满

g. 整个JSP页面结束

实验:7.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8" %>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>out对象的细节</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <%
  22. out.write("12345") ;
  23. //out.flush() ;
  24. response.getWriter().write("67890") ;
  25. %>
  26. </body>
  27. </html>

在浏览器输入:http://localhost:8080/day1100jsp/7.jsp,输出结果例如以下:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmFpdGhfeWVl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmFpdGhfeWVl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

out对象总结:

  1. out.write("123");
  2. reponse.getWriter().write("456");

该两对象不一样。他们要输出的内容都先经过缓存,再由server整理在输出,而输出是先输出456 再123。根本原因是server会先清空out对象的缓存,那么out对象要输出的内容会把内容先存在reponse.getWriter()对象缓存里,当server要清空reponse.getWriter()对象缓存时,就会一起输出。所以是456 123

假设在指令标签的缓存属性设置xxx="0KB" 那么以上总结就不成立了,server会直接顺序输出以上内容。

7.2、pageContext对象

pageContext对象是JSP技术中最重要的一个对象,它代表JSP页面的执行环境,这个对象不仅封装了对其他8大隐式对象的引用。它自身还是一个域对象。能够用来保存数据。而且,这个对象还封装了web开发中经常涉及到的一些经常使用操作,比如引入和跳转其他资源、检索其他域对象中的属性等。jsp中最重要对象。底层代码的基础!

7.3、通过pageContext获得其它对象

  1. getException方法返回exception隐式对象
  2. getPage方法返回page隐式对象
  3. getRequest方法返回request隐式对象
  4. getResponse方法返回response隐式对象
  5. getServletConfig方法返回config隐式对象
  6. getServletContext方法返回application隐式对象
  7. getSession方法返回session隐式对象
  8. getOut方法返回out隐式对象
  9. pageContext封装其他8大内置对象的意义,思考:假设在编程过程中。把pageContext对象传递给一个普通java对象。那么这个java对象将具有什么功能?

7.4、pageContext对象的方法

  1. public void setAttribute(java.lang.String?
  2. name,java.lang.Object?value)
  3. public java.lang.Object?getAttribute(java.lang.String?
  4. name)
  5. public void?removeAttribute(java.lang.String?name)

7.5、pageContext对象中还封装了訪问其他域的方法

  1. public java.lang.Object?
  2. getAttribute(java.lang.String?name,int?
  3. scope)
  4. public void setAttribute(java.lang.String?
  5. name, java.lang.Object?value,int?scope)
  6. public void?removeAttribute(java.lang.String?
  7. name,int?
  8. scope)

7.5、代表各个域的常量

  1. PageContext.APPLICATION_SCOPE
  2. PageContext.SESSION_SCOPE
  3. PageContext.REQUEST_SCOPE
  4. PageContext.PAGE_SCOPE
  5. findAttribute方法 (*重点,查找各个域中的属性) EL表达式
  • PageContext代表jsp页面的执行环境,页面对象。也是一个域对象,封装了经常使用操作。

    1、域对象(第四个域对象了。):范围在本页面

    1. a、存储数据:setAttribute()、和它的重载(4个范围)
    2. b、能够将数据存放在其它范围中
    3. c、查找方法findAttribute():要从page_ScopeRequest_ScopeSESSION_ScopeAPPLICATION_SCOPE范围依次去寻找,找不到返回null

    2、提供了请求转发和包括

    1. forward()
    2. include()

实验:

8.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8" %>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>pageContext对象</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <!--
  22. 1. 域对象: 范围在本页面
  23. a. 存储数据
  24. b. 能够将数据存放到其它范围中
  25. c. 查找方法 findAttribute() : 要从Page_Scope,Request_SCOPE,sESSION_SCOPE,APPLICATION_SCOPE范围依次去
  26. 寻找,找不到返回空字符串
  27. 2. 提供了拿取其它8个对象的方法
  28. 3. 提供了请求转发和包括
  29. -->
  30. <%
  31. pageContext.setAttribute("name", "东方不败") ;
  32. pageContext.setAttribute("name1", "张三丰",pageContext.REQUEST_SCOPE) ;
  33. String name = (String) pageContext.getAttribute("name") ;
  34. String name1 = (String) pageContext.getAttribute("name1") ;
  35. out.write(name) ;
  36. out.write(name1);
  37. pageContext.setAttribute("name2", "张无忌") ;
  38. request.setAttribute("name2", "张三丰") ;
  39. session.setAttribute("name2", "张翠山") ;
  40. application.setAttribute("name2", "张果老") ;
  41. // request.setAttribute("age", "123") ;
  42. // pageContext.getRequest().setAttribute("age", "123") ;
  43. %>
  44. <!-- <a href="http://blog.163.com/faith_yee/blog/9.jsp">9.jsp</a> -->
  45. <%
  46. //pageContext.forward("9.jsp") ;
  47. pageContext.include("9.jsp") ;
  48. %>
  49. <br>
  50. <%--<%
  51. String n = (String) pageContext.findAttribute("name2") ;
  52. out.write(n) ;
  53. %>
  54. --%></body>
  55. </html>

9.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8" %>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>pageContext对象</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body>
  21. 9999999999999999999<br>
  22. <%
  23. //String name = (String)request.getAttribute("name1") ;
  24. //out.write(name) ;
  25. String name = (String)pageContext.findAttribute("name2") ;
  26. out.write(name) ;
  27. %>
  28. </body>
  29. </html>

在浏览器输入:http://localhost:8080/day1100jsp/8.jsp,在浏览器输出结果例如以下:

五、JSP中操作JavaBean

5.1、JavaBean的概念:

(VO:value Object, DO:Data Object 。POJO:最简单的java对象 ,DTO:Data Transfer Object)不同的场景不同的解释

遵循一定的命名规则:

  1. 1、必须有默认的构造方法
  2. 2、类的声明为public类型
  3. 3、字段都是私有的private boolean married
  4. 4、提供共同拥有的gettersetter方法(属性)。

通常是先java.io.Serializable接口

实际开发中有什么用?封装数据。便于传递数据

5.2、JavaWeb开发模型:

MVC模型(model(JavaBean数据)+veiw(JSP显示)+controller(Servlet控制器))

  1. 现实的样例:桌子+吧台+厨房(流程!)
  2. 三层架构:MVC仅仅是三层架构的表现层

  1. 三层架构:(表现层+业务逻辑层+数据訪问层)
  2. (耦合性低:兼容性扩展性强!

评价程序猿好坏:编出来的成品的扩展性。QQ。

更新换代这么多年非常大原因是项目扩展性好

JSP开发模式 :

  1. *SUN公司推出JSP技术后。同一时候也推荐了两种web应用程序的开发模式。一种是JSP+JavaBean模式(模型1)。一种是Servlet+JSP+JavaBean模式(模型2)。
  2. *JSP+JavaBean模式适合开发业务逻辑不太复杂的web应用程序,这样的模式下。JavaBean用于封装业务数据。JSP即负责处理用户请求,又显示数据。
  3. (计算器演示样例)
  4. *Servlet+JSP+JavaBean(MVC)模式适合开发复杂的web应用,在这样的模式下,servlet负责处理用户请求,jsp负责数据显示,javabean负责封装数据。 Servlet+JSPJavaBean模式程序各个模块之间层次清晰,web开发推荐採用此种模式。(绘图MVC及三层架构)

5.3、jsp:useBean标签(重要内容:就是要封装标签里的内容!)

  1. <jsp:useBean>标签用来干什么的:创建对象的。
  2. <!--User user = new User();-->
  3. 在这样的标签里能够创建对象(用来取代页面里的java代码)
  4. 属性
  5. 1、id
  6. 2、class:我是用哪个类创建的
  7. 3、scope:作用范围:(拿去bean中的数据,在实例里用动作标签拿不出还有一个jsp里的session属性,尽管session是共享的。而jsp里的java代码能够拿出,老师给我们展示底层的代码)

实验:

12.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8" %>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>jsp:UseBean标签</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <!-- User user = new User() ; -->
  22. <jsp:useBean id="user" class="com.heima.bean.User" scope="session">
  23. <jsp:setProperty property="username" name="user" value="张无忌"/>
  24. </jsp:useBean>
  25. <jsp:getProperty property="username" name="user"/>
  26. <a href="http://blog.163.com/faith_yee/blog/13.jsp">13.jsp</a>
  27. </body>
  28. </html>

13.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8" %>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>jsp:UseBean标签</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <!-- 拿取bean中的数据 -->
  22. <!-- <jsp:getProperty property="username" name="user"/>-->
  23. <%
  24. User u = (User) session.getAttribute("user") ;
  25. out.write(u.getUsername()) ;
  26. %>
  27. </body>
  28. </html>

在浏览器输入http://localhost:8080/day1100jsp/12.jsp,输出结果例如以下:

在点击页面上的超链:进入了http://localhost:8080/day1100jsp/13.jsp,输出下面结果:
张无忌

5.4、jsp:useBean标签的内省机制

在jsp里怎么封装javaBean? 学习经验:假设搞不懂jsp怎么利用<jsp:useBean> 标签来封装JavaBean的话,能够观察jsp的底层代码:Servlet类的代码!

  1. <jsp:setProperty="*" name = "user"/>//内省
  2. <jsp:getProperty property="id" name="user"/>
  3. <jsp:getProperty property="username" name="user"/>
  4. <jsp:getProperty property="age" name="user"/>

以上过程叫内省机制。

那么内省的底层是怎么回事?

  1. 内省要求Bean的属性名和页面上的表单控制的名字一样即可

实验: 14.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8" %>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>jsp:UseBean标签的内省机制</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <form action="15.jsp" method="post">
  22. 编号: <input type = "text" name = "id"><br>
  23. 姓名: <input type = "text" name = "username"><br>
  24. 年龄: <input type = "text" name = "age"><br>
  25. <input type = "submit" value = "提交"><br>
  26. </form>
  27. <a href="http://blog.163.com/faith_yee/blog/15.jsp?id=111&username=abc&age=100">15.jsp</a><%--
  28. <jsp:forward page="15.jsp">
  29. <jsp:param value="1000" name="id"/>
  30. <jsp:param value="nba" name="username"/>
  31. <jsp:param value="50" name="age"/>
  32. </jsp:forward>
  33. --%></body>
  34. </html>

15.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8" %>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>jsp:UseBean标签的内省机制</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <% request.setCharacterEncoding("UTF-8") ; %>
  22. <jsp:useBean id="user" class="com.heima.bean.User"></jsp:useBean>
  23. <!-- 封装超链的数据 -->
  24. <jsp:useBean id="user1" class="com.heima.bean.User"></jsp:useBean>
  25. <jsp:setProperty property="*" name="user"/>
  26. <jsp:setProperty property="id" name="user" param="id"/>
  27. <jsp:setProperty property="*" name="user1"/>
  28. <jsp:getProperty property="id" name="user" />:
  29. <jsp:getProperty property="username" name="user"/>:
  30. <jsp:getProperty property="age" name="user"/>
  31. <hr>
  32. <jsp:getProperty property="id" name="user1"/>:
  33. <jsp:getProperty property="username" name="user1"/>:
  34. <jsp:getProperty property="age" name="user1"/>
  35. </body>
  36. </html>

在浏览器输入:http://localhost:8080/day1100jsp/14.jsp,页面例如以下:

在表单里填写例如以下信息:

点击提交:

或者在14.jsp页面里点击超链,结果例如以下:

在15.jsp里的总结

  1. <!--封装超链的数据-->
  2. < .........>
  3. <jsp:setProperty="*" name = "user1"/>(内省!
  4. <jsp:getProperty property="id" name="user1"/>
  5. <jsp:getProperty property="username" name="user1"/>
  6. <jsp:getProperty property="age" name="user1"/>

出现故障:在超链提交的数据怎么限制封装到哪个对象里?

  1. 主要看属性名+參数是否一致。
  2. 发现user所要封装的属性名+參数和user1所要封装的属性名+參数都一样。所以都封装成了对象(user+user1),所以在页面输出了两个对象的内容。15.jsp不会关心其它jsp传过来的数据是否反复或者是什么方式传过来的,仅仅要传过来的属性名+參数满足JavaBean对象里的属性名和方法的接口。那么就为这些数据新建对象。
  3. 假设想把传过来的数据指定特定的对象名能够吗?貌似不行。以上是内省机制。

六、四大域对象(相当重要)

a. PageContext:页面范围的数据。用的非常少

b. ServletRequest:请求范围的数据。

用的非常多。显示一次数据后就没实用了。这种数据应该放到该范围中

c. HttpSession:会话范围的数据。用的非常多。每次请求和响应都须要共享的数据。比方登录信息,购物信息。

d. ServletContext(application域):应用范围的数据。用的不多。全部client都共享的信息。注意同步。

数据能不能取到,关键是不是从一个地方取的数据

使用的情况详细分析

七、EL表达式(属于JSP中的技术,今天最重要!

1、EL表达式简单介绍

EL 全名为Expression Language。EL主要作用:

1、获取数据:

  1. EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的web 中检索java对象、获取数据。(某个web 中的对象,訪问javabean的属性、訪问list集合、訪问map集合、訪问数组)

2、运行运算:

  1. 利用EL表达式能够在JSP页面中运行一些主要的关系运算、逻辑运算和算术运算,以在JSP页面中完毕一些简单的逻辑运算。${user==null}

3、获取web开发经常使用对象

  1. EL 表达式定义了一些隐式对象,利用这些隐式对象,web开发者能够非常轻松获得对web经常使用对象的引用。从而获得这些对象中的数据。

4、调用Java方法

  1. EL表达式同意用户开发自己定义EL函数,以在JSP页面中通过EL表达式调用Java类的方法。
  2. a、获取数据:
  3. 老做法:
  4. String name =(Stirng )session.getAttribute("name");
  5. out.write(name);
  6. EL表达式:
  7. El:${name} 拿到的name也是看设置好的范围来获取的。(观察底层代码)
  8. 指定某个域对象中拿去数据:${sessionScope.name}
  9. EL中存在了11个隐含对象
  10. 第一仅仅猫的样例:
  11. &{user.friend.cat.name}
  12. &{user["friend"]["cat"]["name"]}
  13. 用点的地方能够用中括号。反之不行
  14. EL表达式输出常量要加引號,不加的话会识别是变量 ${"abvca"}
  15. 使用EL表达式获取数据语法:“${标识符}”
  16. EL表达式语句在运行时,会调用pageContext.findAttribute方法,用标识符为keyword,分别从pagerequestsessionapplication四个域中查找对应的对象。找到则返回对应对象。找不到则返回”” (注意。不是null,而是空字符串)。
  17. 演示样例:${user}
  18. EL表达式也能够非常轻松获取JavaBean的属性,或获取数组、CollectionMap类型集合的数据,比如:
  19. ${user.address.city}
  20. ${user.list[0]}:訪问有序集合某个位置的元素
  21. ${map.key} 获得map集合中指定key的值
  22. 结合JSTLforeach标签,使用EL表达式也能够非常轻松迭代各种类型的数组或集合。演示样例:
  23. 迭代数组
  24. 迭代collection类型集合
  25. 迭代map类型集合

实验:project架构:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmFpdGhfeWVl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

在实验中要使用到的类: User.java

  1. package com.heima.bean;
  2. public class User {
  3. private String id ;
  4. private String username ;
  5. private Friend friend;
  6. private int age ;
  7. public User() {
  8. }
  9. public User(String id, String username, int age) {
  10. super();
  11. this.id = id;
  12. this.username = username;
  13. this.age = age;
  14. }
  15. public String getId() {
  16. return id;
  17. }
  18. public void setId(String id) {
  19. this.id = id;
  20. }
  21. public Friend getFriend() {
  22. return friend;
  23. }
  24. public void setFriend(Friend friend) {
  25. this.friend = friend;
  26. }
  27. public String getUsername() {
  28. return username;
  29. }
  30. public void setUsername(String username) {
  31. this.username = username;
  32. }
  33. public int getAge() {
  34. return age;
  35. }
  36. public void setAge(int age) {
  37. this.age = age;
  38. }
  39. }

Friend.java

  1. package com.heima.bean;
  2. public class Friend {
  3. private Cat cat ;
  4. public Cat getCat() {
  5. return cat;
  6. }
  7. public void setCat(Cat cat) {
  8. this.cat = cat;
  9. }
  10. }

Cat.java

  1. package com.heima.bean;
  2. public class Cat {
  3. private String name ;
  4. private String color ;
  5. public Cat() {
  6. }
  7. public Cat(String name, String color) {
  8. super();
  9. this.name = name;
  10. this.color = color;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public String getColor() {
  16. return color;
  17. }
  18. }

实验:1.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>el表达式从域对象中获取数据</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <%
  22. pageContext.setAttribute("name", "小龙女") ;
  23. request.setAttribute("name", "赵敏") ;
  24. session.setAttribute("name", "黄蓉") ;
  25. application.setAttribute("name", "周芷若") ;
  26. String name = (String) session.getAttribute("name") ;
  27. out.write(name) ;
  28. User user = new User() ;
  29. Friend f = new Friend() ;
  30. f.setCat(new Cat("喵喵","白色")) ;
  31. user.setFriend(f) ;
  32. request.setAttribute("user", user) ;
  33. List<User> list = new ArrayList<User>() ;
  34. list.add(new User("1","张无忌",20)) ;
  35. list.add(new User("2","乔峰",25)) ;
  36. list.add(new User("3","郭靖",30)) ;
  37. request.setAttribute("list", list) ;
  38. Map<String,User> map = new HashMap<String,User>() ;
  39. map.put("a", new User("1","张无忌",20)) ;
  40. map.put("b", new User("2","乔峰",25)) ;
  41. map.put("c", new User("3","郭靖",30)) ;
  42. request.setAttribute("map", map) ;
  43. %>
  44. <br>
  45. 採用el表达式输出常量:${"abcde"}<br>
  46. 採用el表达式拿取数据: ${name}<br>
  47. 指定从session中拿取数据: ${sessionScope.name }<br>
  48. 拿到人的朋友的第一仅仅猫的名字: ${user.friend.cat.name}:${user["friend"]["cat"]["name"]} <br>
  49. 拿到人的朋友的第一仅仅猫的颜色: ${user.friend.cat.color} <br>
  50. 拿取list中的第一个人的名字:${list[0].username} <br>
  51. 拿取map中的乔峰的名字:${map["b"].username}:${map.b.username} <br>
  52. </body>
  53. </html>

在浏览器输入:http://localhost:8080/day1101el/1.jsp,页面输出下面结果:

  1. b、运行运算:
  2. 语法:
  3. ${运算表达式},EL表达式支持例如以下运算符:

  1. empty运算符:
  2. 检查对象是否为null或“空”,非常好用!!!
  3. 三元表达式:
  4. ${user!=null?
  5. user.name : “”} ,非常好用!
  6. [ ] . 号运算符

实验:2.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>el表达式的数学运算</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <%
  22. int a = 10 ;
  23. request.setAttribute("a", a) ;
  24. String s = (String) request.getAttribute("name") ;
  25. out.write(s) ;
  26. request.setAttribute("name", s) ;
  27. Map<String,User> map = new HashMap<String,User>() ;
  28. map.put("a", new User("1","张无忌",20)) ;
  29. map.put("b", new User("2","乔峰",25)) ;
  30. map.put("c", new User("3","郭靖",30)) ;
  31. request.setAttribute("map", map) ;
  32. %>
  33. 运行加法: ${1+1 }<br>
  34. 运行比較运算:${ 10 >5}:${ 10 gt 5}<br>
  35. 运行比較运算:${ a >5}<br>
  36. 运行逻辑运算: ${a > 5 || a < 0 }<br>
  37. 运行null运算:${name == null}<br>
  38. 运行三元运算符: ${a>5?"哈哈":"呵呵" }<br>
  39. 检測map是否为空:${empty map }:${ not empty map }
  40. </body>
  41. </html>

在浏览器输入:http://localhost:8080/day1101el/2.jsp。页面输出结果例如以下:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmFpdGhfeWVl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

  1. c、获取web开发经常使用对象

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmFpdGhfeWVl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

实验:

3.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>el表达式的内置对象</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <%request.setCharacterEncoding("UTF-8") ; %>
  22. <form action="4.jsp" method="get">
  23. 姓名: <input type = "text" name = "username"><br>
  24. password: <input type = "text" name = "pass"><br>
  25. 反复password: <input type = "text" name = "pass"><br>
  26. <input type = "submit" value = "提交"><br>
  27. </form>
  28. <a href="http://blog.163.com/faith_yee/blog/4.jsp?username=张无忌">4.jsp</a>
  29. <%--<jsp:forward page="4.jsp">
  30. <jsp:param value="东方不败" name="username"/>
  31. </jsp:forward>
  32. --%></body>
  33. </html>

4.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  10. <title>el表达式的内置对象</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  18. -->
  19. </head>
  20. <body><%--
  21. <%
  22. String name = request.getParameter("username") ;
  23. name = new String(name.getBytes("ISO-8859-1"),"GBK") ;
  24. out.write(name) ;
  25. %>--%>
  26. 拿取表单传递的參数: ${param.username }<br>
  27. 拿取超链传递的參数: ${param.username }<br>
  28. 拿取请求转发传递的參数: ${param.username }<br>
  29. 拿取重名參数的值: ${paramValues.pass[0] }: ${paramValues.pass[1] }<br>
  30. 获取请求头的值: ${header.Referer }<br>
  31. 获取请求头的值: ${headerValues.Referer[0] }<br>
  32. 获取全局參数的值: ${initParam.name }<br>
  33. 获取Cookie(是一个map): ${cookie.JSESSIONID }<br>
  34. 获取Cookie(是一个Cookie对象)的名字: ${cookie.JSESSIONID.name }<br>
  35. 获取Cookie(是一个Cookie对象)的值: ${cookie.JSESSIONID.value }<br>
  36. </body>
  37. </html>

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <display-name></display-name>
  8. <context-param>
  9. <param-name>name</param-name>
  10. <param-value>山本五十六</param-value>
  11. </context-param>
  12. <welcome-file-list>
  13. <welcome-file>index.jsp</welcome-file>
  14. </welcome-file-list>
  15. </web-app>

在浏览器输入http://localhost:8080/day1101el/3.jsp。页面结果例如以下,输入例如以下信息:

点击提交后进入4.jsp,显演示样例如以下结果:

假设在13.jsp点击超链,得到例如以下结果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmFpdGhfeWVl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

获取Cookie(是一个map)的键:${cookie.key}//拿不出

  1. d、调用Java方法
  2. ${"abc"+"de"}//不行:+支持整数浮点数,不支持字符串
  3. EL不支持字符串的不论什么操作
  4. 但我们能够定义函数来实现
  5. ${fun:toupper("abcde")}//EL不识别函数,那么怎么来实现这种方式?EL一定和底层的JAVA代码相关联,所以我们能够自己定义标签函数

实验: 5.jsp

  1. <%@ page language="java" import="java.util.*,com.heima.bean.*" pageEncoding="UTF-8"%>
  2. <%@ taglib uri="http://java.sun.com/jsp/myfun" prefix="fun" %>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
  4. <%
  5. String path = request.getContextPath();
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  7. %>
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html>
  10. <head>
  11. <base href="http://blog.163.com/faith_yee/blog/<%=basePath%>">
  12. <title>el表达式的内置对象</title>
  13. <meta http-equiv="pragma" content="no-cache">
  14. <meta http-equiv="cache-control" content="no-cache">
  15. <meta http-equiv="expires" content="0">
  16. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  17. <meta http-equiv="description" content="This is my page">
  18. <!--
  19. <link rel="stylesheet" type="text/css" href="http://blog.163.com/faith_yee/blog/styles.css">
  20. -->
  21. </head>
  22. <body>
  23. ${fun:toupper("abcde")}
  24. ${fun:toupper("aaaaaaaaa") }
  25. ${fun:out("abcde") }
  26. </body>
  27. </html>

a.tld

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  5. version="2.0">
  6. <tlib-version>1.1</tlib-version>
  7. <short-name>fun</short-name>
  8. <uri>http://java.sun.com/jsp/myfun</uri>
  9. <function>
  10. <name>toupper</name>
  11. <function-class>com.heima.demo.Demo</function-class>
  12. <function-signature>java.lang.String demo(java.lang.String)</function-signature>
  13. </function>
  14. <function>
  15. <name>out</name>
  16. <function-class>com.heima.demo.Demo</function-class>
  17. <function-signature>void demo1(java.lang.String)</function-signature>
  18. </function>
  19. </taglib>

Demo.java

  1. package com.heima.demo;
  2. public class Demo {
  3. //将參数转变为大写
  4. public static String demo(String str){
  5. return str.toUpperCase() ;
  6. }
  7. public static void demo1(String str){
  8. System.out.println(str);
  9. }
  10. }

在浏览器输入:http://localhost:8080/day1101el/5.jsp。得到例如以下结果:

方法总结:

  1. 1、创建一个类(后台)
  2. 2、类里写方法(静态)
  3. 3、写描写叙述性文件(注意路径)
  4. 4、页面上要用taglib指令标签引入
  5. 5、能够用EL语句是使用了
  6. taglib里能够引用sun公司已经定义好了的方法。
  7. 用属性来改变要用法的名字

八、在地址栏里输入中文,server解析为乱码的IE解决的方法

地址栏输入有中文:

  1. String s = request.getQueryString("username");
  2. //String s = request.getParameter("username");
  3. s = new String(s.getBytes("iso-8859-1"),"gbk");
  4. out.write(s);

下载

版权声明:本文博客原创文章。博客,未经同意,不得转载。

JavaWeb-11 (JSP&amp;EL表达)的更多相关文章

  1. 在jsp中用EL 表达来获取表单中的参数

     在一个JSP页面转到另一个JSP页面时,对表单中的参数用EL表达式提取为:     <form action="sampleJsp.jsp" method="po ...

  2. Javaweb 第12天 JSP、EL技术

    第12天 JSP.EL技术 今日任务: JSP技术入门和常用指令 JSP的内置对象&标签介绍 EL表达式&EL的内置对象 课堂笔记 1.JSP技术入门和常用指令 1.1.JSP的由来. ...

  3. 超全面的JavaWeb笔记day12<Jsp&JavaBean&El表达式>

    1.JSP三大指令 page include taglib 2.9个内置对象 out page pageContext request response session application exc ...

  4. 11、Jsp加强/EL表达式/jsp标签

    1 Jsp基础回顾 Jsp基础 1)Jsp的执行过程 tomcat服务器完成:jsp文件->翻译成java文件->编译成class字节码文件-> 构造类对象-> 调用方法 to ...

  5. Javaweb学习笔记6—EL表达式与JSTL及自定义标签

    今天来讲javaweb的第六阶段学习. EL表达式与JSTL及自定义标签是对上篇文章介绍的JSP的扩展,不能说是很重要的东西,但是也要了解. 老规矩,首先先用一张思维导图来展现今天的博客内容. ps: ...

  6. javaweb回顾第九篇EL表达式

    前言:关于EL表示式开发用的非常多,现在我们回顾一下关于如果去操作EL表达式 1:EL表达式语法 所有EL表达式都是由{开始}结束,表达式中用.和[]操作符来访问数据比喻${user.userName ...

  7. javaweb 与jsp页面的交互流程 (初次接触时写)

    javaweb 与jsp页面的交互流程 javaweb项目目录 1. javaweb项目的一般目录: 2. jsp 页面一般情况下放在 top(前台页面) back(后台页面) 3. 后台代码 放在s ...

  8. 第75节:Java的中的JSP,EL和JSTL

    第75节:Java中的JSP,EL和JSTL 哭吧看不完的!!! Cookie和`Session 请求转发和重定向的区别: 地址不一样 请求次数也不一样 数据无法传递 4.跳转范围有限制 效率 请求转 ...

  9. JSP、EL、JSTL

    JSP(Java Server Pages) 什么是JSP Java Server Pages(Java服务器端的页面) 使用JSP:SP = HTML + Java代码 + JSP自身的东西.执行J ...

随机推荐

  1. hdu2845(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2845 题意:给你一个n*m的矩阵,每个位置有一定数量的豆子,如果你去map[x][y]位置上的豆子,则 ...

  2. WPF-20:richtextbox相关操作(转)

    WPF中的richtextbox与winform中的richtextbox的使用不同,看看下面的基本操作: 一.取出richTextBox里面的内容  (1)将richTextBox的内容以字符串的形 ...

  3. The method getDispatcherType() is undefined for the type HttpServletRequest 升级到tomcat8(转)

    配置项目,从tomcat低版本,放到tomcat8时,正常的项目居然报错了: The method getDispatcherType() is undefined for the type Http ...

  4. HYSBZ 2243(树链剖分)

    题目连接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28982#problem/D 题意:给定一棵有n个节点的无根树及点权和m个操作, ...

  5. python面向对象具体解释(上)

    创建类 Python 类使用 class 关键字来创建.简单的类的声明能够是关键字后紧跟类名: class ClassName(bases): 'class documentation string' ...

  6. hdu2222Keywords Search (特里)

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  7. maven中的java库

    /* *  *         <dependency>    <groupId>io.netty</groupId>    <artifactId>n ...

  8. Leetcode_191_Number of 1 Bits

    本文是在学习中的总结.欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/44486547 Write a function that ...

  9. C++第11周(春)项目1 - 存储班长信息的学生类

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目1 - 存储班长信息的学生类] clas ...

  10. 什么是 CGI,什么是 IIS,什么是VPS

    该公司来到天.我们所从事的事情在网站上.这对我来说确实是一个很大的挑战.个人一直从事Android,对于web而一个开发网站server知识的几乎为零.在这里应该说,现在我只是有一个技术人员,昨天相遇 ...