Jsp1









JSP表达式<%= 表达式 %>里面表达式结尾不要加分号;声明:<%! 声明变量或者方法 %>
  


JSP页面发生更改,服务器都要重新编译JSP页面,并生成新的字节码文件和servelet文件

Work文档目录:把jsp页面生成的servelet文件存放在这个目录当中。
work目录中除了对JSP文件转换而成的字节码文件外,
生成的servelet文件,init初始化方法只会在第一次请求的时候调用,并长留与内存当中,由多线程调用service方法来处理客户其他请求。
实战:
    九九乘法表
 

  1. <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme() + "://"
  5. + request.getServerName() + ":" + request.getServerPort()
  6. + path + "/";
  7. %>
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12. <title>My JSP 'index.jsp' starting page</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="styles.css">
  20. -->
  21. </head>
  22. <body>
  23. <%!//返回九九乘法表对应的html代码。通关过表达式来调用,在页面显示
  24. String printMultiTable1(){
  25. String s = "";
  26. for (int i = 1; i <= 9; i++) {
  27. for (int j = 1; j <= i; j++) {
  28. s+=i+"*"+j+"="+(i*j)+"&nbsp;&nbsp;&nbsp;&nbsp;";
  29. }
  30. s+="<br>";//追加换行标签
  31. }
  32. return s;
  33. }
  34. //JSP内置 out对象,使用脚本方式打印九九乘法表
  35. void printMultiTable2(JspWriter out)throws Exception{
  36. for (int i = 1; i <= 9; i++) {
  37. for (int j = 1; j <= i; j++) {
  38. out.println(i+"*"+j+"="+(i*j)+"&nbsp;&nbsp;&nbsp;&nbsp;");
  39. }
  40. out.println("<br>");
  41. }
  42. }
  43. %>
  44. <h1>九九乘法表</h1>
  45. <hr>
  46. <%=printMultiTable1()%>
  47. <br>
  48. <% printMultiTable2(out);%>
  49. </body>
  50. </html>


    




原文地址:https://www.cnblogs.com/liuruimiku/p/5310706.html