JSP基础教程源代码.docx

上传人:b****1 文档编号:1016050 上传时间:2023-04-30 格式:DOCX 页数:229 大小:63.72KB
下载 相关 举报
JSP基础教程源代码.docx_第1页
第1页 / 共229页
JSP基础教程源代码.docx_第2页
第2页 / 共229页
JSP基础教程源代码.docx_第3页
第3页 / 共229页
JSP基础教程源代码.docx_第4页
第4页 / 共229页
JSP基础教程源代码.docx_第5页
第5页 / 共229页
JSP基础教程源代码.docx_第6页
第6页 / 共229页
JSP基础教程源代码.docx_第7页
第7页 / 共229页
JSP基础教程源代码.docx_第8页
第8页 / 共229页
JSP基础教程源代码.docx_第9页
第9页 / 共229页
JSP基础教程源代码.docx_第10页
第10页 / 共229页
JSP基础教程源代码.docx_第11页
第11页 / 共229页
JSP基础教程源代码.docx_第12页
第12页 / 共229页
JSP基础教程源代码.docx_第13页
第13页 / 共229页
JSP基础教程源代码.docx_第14页
第14页 / 共229页
JSP基础教程源代码.docx_第15页
第15页 / 共229页
JSP基础教程源代码.docx_第16页
第16页 / 共229页
JSP基础教程源代码.docx_第17页
第17页 / 共229页
JSP基础教程源代码.docx_第18页
第18页 / 共229页
JSP基础教程源代码.docx_第19页
第19页 / 共229页
JSP基础教程源代码.docx_第20页
第20页 / 共229页
亲,该文档总共229页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

JSP基础教程源代码.docx

《JSP基础教程源代码.docx》由会员分享,可在线阅读,更多相关《JSP基础教程源代码.docx(229页珍藏版)》请在冰点文库上搜索。

JSP基础教程源代码.docx

JSP基础教程源代码

第1章JSP简介

例子1

Example1_1.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

这是一个简单的JSP页面

<%

inti,sum=0;

for(i=1;i<=100;i++)

{

sum=sum+i;

}

%>

1到100的连续和是:


<%=sum%>

第2章JSP语法

例子1

Example2_1.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

<%!

inti=0;

%>

<%

i++;

%>

您是第

<%=i%>

个访问本站的客户。

例子2

Example2_2.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

<%!

intnumber=0;

synchronizedvoidcountPeople()

{

number++;

}

%>

<%

countPeople();//在程序片中调用方法。

%>

您是第

<%=number%>

个访问本站的客户。

例子3

Example2_3.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

<%@pageimport="java.io.*"%>

<%!

intnumber=0;

Filefile=newFile("count.txt");

synchronizedvoidcountPeople()//计算访问次数的同步方法

{

if(!

file.exists())

{

number++;

try{

file.createNewFile();

FileOutputStreamout=newFileOutputStream("count.txt");

DataOutputStreamdataOut=newDataOutputStream(out);

dataOut.writeInt(number);

out.close();

dataOut.close();

}

catch(IOExceptionee){}

}

else

{

try{

FileInputStreamin=newFileInputStream("count.txt");

DataInputStreamdataIn=newDataInputStream(in);

number=dataIn.readInt();

number++;

in.close();

dataIn.close();

FileOutputStreamout=newFileOutputStream("count.txt");

DataOutputStreamdataOut=newDataOutputStream(out);

dataOut.writeInt(number);

out.close();

dataOut.close();

}

catch(IOExceptionee){}

}

}

%>

<%

countPeople();

%>

您是第

<%=number%>

个访问本站的客户。

例子4

Example2_4.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

请输入圆的半径:


<%!

publicclassCircle

{

doubler;

Circle(doubler)

{

this.r=r;

}

double求面积()

{

returnMath.PI*r*r;

}

double求周长()

{

returnMath.PI*2*r;

}

}

%>

<%

Stringstr=request.getParameter("cat");

doubler;

if(str!

=null)

{

r=Double.parseDouble(str);

}

else

{

r=1;

}

Circlecircle=newCircle(r);//创建对象。

%>

圆的面积是:


<%=circle.求面积()%>

圆的周长是:


<%=circle.求周长()%>

例子5

Example2_5.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

<%!

longcontinueSum(intn)

{

intsum=0;

for(inti=1;i<=n;i++)

{

sum=sum+i;

}

returnsum;

}

%>

1到100的连续和:


<%

longsum;

sum=continueSum(100);

out.print(""+sum);

%>

例子6

Example2_6.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

请输入E-mail:


<%

Stringstr=request.getParameter("client");

if(str!

=null)

{

intindex=str.indexOf("@");

if(index==-1)

{

%>


您的email地址中没有@。

<%

}

else

{

intspace=str.indexOf("");

if(space!

=-1)

{

%>


您的email地址含有非法的空格。

<%

}

else

{

intstart=str.indexOf("@");

intend=str.lastIndexOf("@");

if(start!

=end)

{

%>


您的email地址有两个以上的符号:

@。

<%

}

else

{

out.print("
"+str);

%>


您的email地址书写正确。

<%

}

}

}

}

%>

例子7

Example2_7.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

Sin(0.9)除以3等于

<%=Math.sin(0.90)/3%>

3的平方是:

<%=Math.pow(3,2)%>

12345679乘72等于

<%=12345679*72%>

5的平方根等于

<%=Math.sqrt(5)%>

99大于100吗?

回答:

<%=99>100%>

例子8

Example2_8.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

请输入三角形的三个边a,b,c的长度:


--以下是HTML表单,向服务器发送三角形的三个边的长度-->

请输入三角形边a的长度:


请输入三角形边b的长度:


请输入三角形边c的长度:


<%--获取客户提交的数据--%>

<%

Stringstring_a=request.getParameter("a"),

string_b=request.getParameter("b"),

string_c=request.getParameter("c");

doublea=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)

{

doublep=(a+b+c)/2.0;

doublemianji=Math.sqrt(p*(p-a)*(p-b)*(p-c));

out.print("
"+"三角形面积:

"+mianji);

}

else

{

out.print("
"+"您输入的三边不能构成一个三角形");

}

}

catch(NumberFormatExceptione)

{

out.print("
"+"请输入数字字符");

}

%>

例子9

Example2_9.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

<%@includefile="Hello.txt"%>

注:

上述Example2_9.jsp等价于下面的JSP文件:

Example2_9_1.jsp。

Example2_9_1.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

你们好,很高兴认识你们呀!

例子10

Computer.jsp


<%

Stringa=request.getParameter("ok");

if(a==null)

{a="1";

}

try

{

doublenumber=Integer.parseInt(a);

out.print("
"+Math.sqrt(number));

}

catch(NumberFormatExceptione)

{

out.print("
"+"请输入数字字符");

}

%>

Example2_10.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

请输入一个正数,点击按钮求这个数的平方根。

<%@includefile="Computer.jsp"%>

Example2_10_1.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

请输入一个正数,点击按钮求这个数的平方根。


<%Stringa=request.getParameter("ok");

if(a==null)

{

a="1";

}

try{

doublenumber=Integer.parseInt(a);

out.print("
"+Math.sqrt(number));

}

catch(NumberFormatExceptione)

{

out.print("
"+"请输入数字字符");

}

%>

例子11

Example2_11.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

加载的文件:

includepage="Myfile/Hello.txt"/>

加载的图象:


includepage="image.html"/>

例子12

tom.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

<%

Stringstr=request.getParameter("computer");//获取值。

intn=Integer.parseInt(str);

intsum=0;

for(inti=1;i<=n;i++)

{

sum=sum+i;

}

%>

从1到<%=n%>的连续和是:


<%=sum%>

Example2_12.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

加载文件效果:

includepage="tom.jsp">

paramname="computer"value="300"/>

include>

例子13

Example2_13.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

<%

doublei=Math.random();

if(i>0.5)

{

%>

forwardpage="Example2_1.jsp"/>

<%

}

else

{

%>

forwardpage="Example2_3.jsp"/>

<%

}

%>

这句话和下面的表达式的值能输出吗?

<%=i%>

例子14

come.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

<%

Stringstr=request.getParameter("number");

doublen=Double.parseDouble(str);

%>

您传过来的数值是:


<%=n%>

Example2_14.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

<%

doublei=Math.random();

%>

forwardpage="come.jsp">

paramname="number"value="<%=i%>"/>

forward>

例子15

Example2_15.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

plugintype="applet"code="B.class"jreversion="1.2"width="200"height="260">

fallback>

PlugintagOBJECTorEMBEDnotsupportedbybrowser.

fallback>

plugin>

第3章JSP内置对象

例子1

Example3_1.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

tree.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

获取文本框提交的信息:

<%

StringtextContent=request.getParameter("boy");

%>


<%=textContent%>

获取按钮的名字:

<%

StringbuttonName=request.getParameter("submit");

%>


<%=buttonName%>

例子2

Example3_2.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

<%

StringtextContent=request.getParameter("girl");

doublenumber=0,r=0;

if(textContent==null)

{

textContent="";

}

try{

number=Double.parseDouble(textContent);

if(number>=0)

{

r=Math.sqrt(number);

out.print("
"+String.valueOf(number)+"的平方根:

");

out.print("
"+String.valueOf(r));

}

else

{

out.print("
"+"请输入一个正数");

}

}

catch(NumberFormatExceptione)

{

out.print("
"+"请输入数字字符");

}

%>

例子3

Example3_3.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2