?? 第2章.txt
字號:
{ out.print(s+"<BR>");
}
in.close();
buffer.close();
}
catch(IOException ee)
{out.print("文件不存在");}
%>
</BODY>
</HTML>
(2)例子11(效果如圖2.9所示)
Example2_11.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY bgcolor=cyan><FONT size=1>
<P> Sin(0.9)除以3等于
<%=Math.sin(0.90)/3%>
<p>3的平方是:
<%=Math.pow(3,2)%>
<P>12345679乘72等于
<%=12345679*72%>
<P> 5的平方根等于
<%=Math.sqrt(5)%>
<P>99大于100嗎?回答:
<%=99>100%>
</BODY>
</HTML>
(2)例子12
Example2_12.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY>
<P> 請輸入三角形的三個邊a,b,c的長度:
<BR>
<!-- 以下是HTML表單,向服務器發送三角形的三個邊的長度 -->
<FORM action="Example2_12.jsp" method=post name=form>
<P>請輸入三角形邊a的長度:
<INPUT type="text" name="a">
<BR>
<P>請輸入三角形邊b的長度:
<INPUT type="text" name="b">
<BR>
<P>請輸入三角形邊c的長度:
<INPUT type="text" name="c">
<BR>
<INPUT TYPE="submit" value="送出" name=submit>
</FORM>
<%--獲取客戶提交的數據--%>
<% String string_a=request.getParameter("a"),
string_b=request.getParameter("b"),
string_c=request.getParameter("c");
double a=0,b=0,c=0;
%>
<%--判斷字符串是否是空對象,如果是空對象就初始化--%>
<% if(string_a==null)
{string_a="0";string_b="0";string_c="0";
}
%>
<%--求出邊長,并計算面積--%>
<% try{ a=Double.valueOf(string_a).doubleValue();
b=Double.valueOf(string_b).doubleValue();
c=Double.valueOf(string_c).doubleValue();
if(a+b>c&&a+c>b&&b+c>a)
{double p=(a+b+c)/2.0;
double mianji=Math.sqrt(p*(p-a)*(p-b)*(p-c));
out.print("<BR>"+"三角形面積:"+mianji);
}
else
{out.print("<BR>"+"您輸入的三邊不能構成一個三角形");
}
}
catch(NumberFormatException e)
{out.print("<BR>"+"請輸入數字字符");
}
%>
</BODY>
</HTML>
(2)例子13(效果如圖2.10所示)
Example2_13.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page info="我!張惠妹" %>
<HTML>
<BODY bgcolor=cyan><FONT Size=1>
<P> 誰呀?
<% String s=getServletInfo();
out.print("<BR>"+s);
%>
</BODY>
<HTML>
(2)例子14(效果如圖2.11所示)
Example2_14.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<BODY bgcolor=cyan>
<H3>
<%@ include file="Hello.txt" %>
</H3>
</BODY>
</HTML>
(2)例子15(效果如圖2.12所示)
Example2_15.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<BODY Bgcolor=cyan><FONT size=1>
<P>請輸入一個正數,點擊按鈕求這個數的平方根。
<CENTER>
<%@ include file="Computer.jsp"%>
</CENTER>
</BODY>
</HTML>
(2)例子16(效果如圖2.13所示)
Example2_16.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY BGCOLOR=Cyan><FONT Size=1>
<P>加載的文件:
<jsp:include page="Myfile/Hello.txt">
</jsp:include>
<P>加載的圖象:
<BR>
<jsp:include page="image.html">
</jsp:include>
</BODY>
</HTML>
(2)例子17(效果如圖2.14所示)
Example2_17.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<%! class FileJSP implements FilenameFilter
{ String str=null;
FileJSP(String s)
{str="."+s;
}
public boolean accept(File dir,String name)
{ return name.endsWith(str);
}
}
%>
<BODY bgcolor=cyan><FONT Size=1>
<P>下面列出了服務器上的一些jsp文件
<% File dir=new File("d:/Tomcat/Jakarta-tomcat-4.0/webapps/root/Myfile");
FileJSP file_jsp=new FileJSP("jsp");
String file_name[]=dir.list(file_jsp);
for(int i=0;i<file_name.length;i++)
{out.print("<BR>"+file_name[i]);
}
%>
<P>請輸入一個jsp文件的名字,加載這個jsp文件:
<FORM action="Example2_17.jsp" method=post name=form>
<INPUT type="text" name="ok">
<BR>
<INPUT TYPE="submit" value="送出" name=submit>
</FORM>
<% String fileName="/Myfile/";
fileName=fileName+request.getParameter("ok");
%>
<P>加載的效果:
<jsp:include page="<%= fileName %>" >
</jsp:include>
</BODY>
tom.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY>
<% String str=request.getParameter("computer"); //獲取值。
int n=Integer.parseInt(str);
int sum=0;
for(int i=1;i<=n;i++)
{ sum=sum+i;
}
%>
<P>
從1到<%=n%>的連續和是:
<BR>
<%=sum%>
</BODY>
</HTML>
(2)例子18(效果如圖2.15所示)
Example2_18.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY>
<P>加載文件效果:
<jsp:include page="tom.jsp">
<jsp:param name="computer" value="300" />
</jsp:include>
</BODY>
</HTML>
例子19(效果如圖2.16所示)
(2)Example2_19.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY>
<% double i=Math.random();
if(i>0.5)
{
%>
<jsp:forward page="/Myfile/Example2_11.jsp" >
</jsp:forward>
<%
}
else
{
%>
<jsp:forward page="Example2_2.jsp" >
</jsp:forward>
<%
}
%>
<P> 這句話和下面的表達式的值能輸出嗎?
<%=i%>
</BODY>
</HTML>
come.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY bgcolor=cyan><FONT Size=1>
<%String str=request.getParameter("number");
double n=Double.parseDouble(str);
%>
<P>您傳過來的數值是:<BR>
<%=n%>
</BODY>
</HTML>
(2)例子20(效果如圖2.17所示)
Example2_20.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY>
<% double i=Math.random();
%>
<jsp:forward page="come.jsp" >
<jsp:param name="number" value="<%=i%>" />
</jsp:forward>
</BODY>
</HTML>
(2)例子21
Example2_21.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY>
<jsp:plugin type="applet" code="B.class" jreversion="1.2" width="200" height="260" >
<jsp:fallback>
Plugin tag OBJECT or EMBED not supported by browser.
</jsp:fallback>
</jsp:plugin>
</body></html>
B.java:
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
public class B extends JApplet implements ActionListener
{ JTable table;Object a[][];
JPanel p1,p2;
Object name[]={"第1列","第2列"};
JButton button;JTextField text;
public void init()
{ a=new Object[2][2];
button=new JButton("確定");text=new JTextField(8);
p1=new JPanel();p2=new JPanel();
p1.setLayout(new GridLayout(2,1));
p1.add(new Label("輸入2階行列式的元素"));
p1.add(new Label("輸入或修改數據后,用鼠標點擊每個格,使數據生效"));
p2.add(button);
p2.add(new JLabel("結果:"));
p2.add(text);
table=new JTable(a,name);
button.addActionListener(this);
getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);
getContentPane().add(p1,BorderLayout.NORTH);
getContentPane().add(p2,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==button)
{ double d[][]=new double[2][2];
double result;
for(int i=0;i<2;i++)
{for(int j=0;j<2;j++)
{d[i][j]=Double.valueOf(a[i][j].toString()).doubleValue();
}
}
result=d[1][1]*d[0][0]-d[0][1]*d[1][0];
text.setText(String.valueOf(result));
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -