<%@ page contentType="text/html;charset=UTF-8"%> <!-- 字符编码为utf-8,不然会乱码。。。。 --> <%@ page import="java.util.Date"%> <%!Date date; public int continueSum(int start, int end) { int sum = 0; for (int i = start; i <= end; i++) sum += i; return sum; }%> <html> <head> <title>first blood</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <!-- head中间要加这句话,不然会乱码 --> </head> <body> <font size=4><p> 程序片创建Date对象: <% date = new Date(); out.println("<BR>" + date); int start = 1; int end = 100; int sum = continueSum(start, end); %> <br>从 <%=start%> 至 <%=end%> 的连续和是 <%=sum%></font> </body> </html>
<%@ page contentType="text/html;charset=gb2312"%> <html> <head> <title>first blood</title> </head> <body bgcolor=yellow> <font size=3> <%!int i = 0;%> <% i++; %> <p> 您是第 <%=i%> 个访问本站的用户. </body> </html>
<%@ page contentType="text/html; charset=utf-8" %> <%@page info="山水图像ic_shanshui.jpg" %> <% String s=getServletInfo(); String str[]=s.split("图像"); %> <html> <body background="image/<%=str[1]%>"><font size="4"> <br><center>这里的<%=str[0]%>不错</center> </body> </html>
info 是一串字符串, 可以通过getServletInfo()来获取, split是个截取函数,将字符串的2边分割。
<bgsound src="sound/ic_yan.mp3" loop=-1></bgsound>
<embed src="sound/ic_mo.mp3" loop=1 ></embed> 2 中音乐播放方式在IE在才可以实现 ,loop=1 播放1次, =-1循环播放;
include指令标记
<jsp:include page="文件的url"/>或<jsp:include page="文件的url"/> param子标记 </jsp:include>
例:<jsp:include page="myfile.jsp">
<jsp:param name="a" value="<%=a%>"/>
</jsp:include>
:之间没有空格, 有/当不需要用param时,必须使用第一种。
forward指令标记
<jsp:forward page="要转向的页面"/>
或
<jsp:forward page="要转向的页面">
<jsp:param name=" " value=" "/>
</jsp:forward>
处理汉字信息
String str=request.getParameter("message");
byte b[]=str.getBytes("iso-8859-1");
str=new String(b);
request.setCharacterEncoding("gb2312");
response的http文件头
例
<%@ page language="java" pageEncoding="GBK"%> <%@page contentType="application/msword;charset=gb2312"%> <% response.setHeader("Content-disposition",//就是弹出对话框 "attachment; filename=exdata.doc"); %> <table id="PrintA" border=1> <tr class="line-odd"> <td align="right">资助项目名称:</td> <td align="left">资助项目名称</td> </tr> <tr class="line-even"> <td align="right">流水号:</td> <td align="left">流水号</td> </tr> <tr class="line-odd"> <td align="right">资助编号:</td> <td align="left">资助编号</td> </tr> </table>
<%@page contentType="application/msword;charset=gb2312"%>
<%
response.setHeader("Content-disposition",//就是弹出对话框
"attachment; filename=exdata.doc");
%>
contentType="application/msword打开方式为word
response.setHeader("Content-disposition",//就是弹出下载对话框
get和post方法的主要区别是:get方法提交的信息会在提交的过程中显示浏览器的栏中,而post不会。
处理超链接
例如
不能有汉字,不然会乱码。
<a href="example.jsp?id=A1001&price=8765">购买</a>
可以用request对象来接收
jsp的全局变量的定义的方式为:<%! %>
jsp的局部变量的定义方式为:<% %>