1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 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 23 <body> 24 <h1>大家好</h1> 25 <hr> 26 <!-- 我是HTML注释,在客户端可见 --> 27 <%-- 我是JSP注释,在客户端不可见 --%> 28 <%! 29 String s = "张三"; //声明了一个字符串变量 30 int add(int x,int y) //声明了一个返回整型的函数,实现两个整数的求和。 31 { 32 return x+y; 33 } 34 %> 35 36 <% 37 //单行注释 38 /*多行注释*/ 39 out.println("大家好,欢迎大家学习JAVAEE开发。"); 40 %> 41 <br> 42 你好,<%=s %><br> 43 x+y=<%=add(10,5) %><br> 44 </body> 45 </html>
JSP内置out对象
1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'exercise.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <%! 27 //返回九九乘法表对应的HTML代码,通过表达式来调用,在页面上显示 28 String printMultiTable1() 29 { 30 String s = ""; 31 for(int i=1;i<=9;i++) 32 { 33 for(int j=1;j<=i;j++) 34 { 35 s+=j+"*"+i+"="+(i*j)+" "; 36 } 37 s+="<br>"; //追加换行标签 38 } 39 return s; 40 } 41 42 //JSP内置out对象,使用脚本方式调用,打印九九乘法表 43 void printMultiTable2(JspWriter out) throws Exception 44 { 45 for(int i=1;i<=9;i++) 46 { 47 for(int j=1;j<=i;j++) 48 { 49 out.println(j+"*"+i+"="+(i*j)+" "); 50 } 51 out.println("<br>"); //追加换行标签 52 } 53 } 54 55 %> 56 <h1>九九乘法表</h1> 57 <hr> 58 <!-- 使用表达式来调用 --> 59 <%=printMultiTable1()%> 60 <br> 61 <!-- 使用脚本方式调用 --> 62 <% printMultiTable2(out);%> 63 <br> 64 <!-- 使用脚本方式调用printMultiTable1(),这种方式不行 --> 65 <% printMultiTable1();%> 66 <br> 67 68 </body> 69 </html>