您好,欢迎来到意榕旅游网。
搜索
您的当前位置:首页jsp简单例子

jsp简单例子

来源:意榕旅游网
例子1

Example2_1.jsp

<%@ page contentType=\"text/html;charset=GB2312\" %>

<%! int i=0; %> <% i++; %>

您是第 <%=i%> 个访问本站的客户。

例子2

Example2_2.jsp

<%@ page contentType=\"text/html;charset=GB2312\" %> <%!

int number=0;

synchronized void countPeople() {

number++; } %> <%

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

您是第 <%=number%> 个访问本站的客户。

例子3 Example2_3.jsp

<%@ page contentType=\"text/html;charset=GB2312\" %> <%@ page import=\"java.io.*\" %>

<%!

int number=0;

File file=new File(\"count.txt\") ;

synchronized void countPeople()//计算访问次数的同步方法 {

if(!file.exists()) {

number++; try {

file.createNewFile();

FileOutputStream out=new FileOutputStream(\"count.txt\"); DataOutputStream dataOut=new DataOutputStream(out); dataOut.writeInt(number); out.close(); dataOut.close(); }

catch(IOException ee){} } else { try{

FileInputStream in=new FileInputStream(\"count.txt\"); DataInputStream dataIn=new DataInputStream(in); number=dataIn.readInt(); number++; in.close(); dataIn.close();

FileOutputStream out=new FileOutputStream(\"count.txt\"); DataOutputStream dataOut=new DataOutputStream(out); dataOut.writeInt(number); out.close(); dataOut.close(); }

catch(IOException ee){} } }

%> <%

countPeople(); %>

您是第

<%=number%> 个访问本站的客户。

例子4 Example2_4.jsp

<%@ page contentType=\"text/html;charset=GB2312\" %>

请输入圆的半径:

<%!

public class Circle {

double r; Circle(double r) {

this.r=r; }

double 求面积() {

return Math.PI*r*r; }

double 求周长() {

return Math.PI*2*r; } } %> <%

String str=request.getParameter(\"cat\"); double r;

if(str!=null) {

r=Double.parseDouble(str); } else { r=1; }

Circle circle=new Circle(r); //创建对象。 %>

圆的面积是:

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

圆的周长是:

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

例子5 Example2_5.jsp

<%@ page contentType=\"text/html;charset=GB2312\" %>

<%!

long continueSum(int n) {

int sum=0;

for(int i=1;i<=n;i++) {

sum=sum+i; } return sum; } %>

1到100的连续和:
<% long sum;

sum=continueSum(100); out.print(\"\"+sum); %>

例子6 Example2_6.jsp

<%@ page contentType=\"text/html;charset=GB2312\" %>

请输入E-mail:

<%

String str=request.getParameter(\"client\"); if(str!=null) {

int index=str.indexOf(\"@\"); if(index==-1) { %>


您的email地址中没有 @。 <% } else {

int space=str.indexOf(\" \"); if(space!=-1) { %>


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

} else {

int start=str.indexOf(\"@\"); int end=str.lastIndexOf(\"@\"); if(start!=end) { %>


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

} else

@。 {

out.print(\"
\"+str); %>


您的email地址书写正确。 <%

} } } } %>

例子7 Example2_7.jsp

<%@ page contentType=\"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

<%@ page contentType=\"text/html;charset=GB2312\" %>

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

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


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

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

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

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(\"
\"+\"三角形面积:\"+mianji); } else {

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

catch(NumberFormatException e)

{

out.print(\"
\"+\"请输入数字字符\"); } %>

例子9 Example2_9.jsp

<%@ page contentType=\"text/html;charset=GB2312\" %>

<%@ include file=\"Hello.txt\" %>

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

<%@ page contentType=\"text/html;charset=GB2312\" %>

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

例子10 Computer.jsp


<%

String a=request.getParameter(\"ok\"); if(a==null) { a=\"1\"; }

try {

double number=Integer.parseInt(a); out.print(\"
\"+Math.sqrt(number)); }

catch(NumberFormatException e) {

out.print(\"
\"+\"请输入数字字符\"); } %>

Example2_10.jsp

<%@ page contentType=\"text/html;charset=GB2312\" %>

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

<%@ include file=\"Computer.jsp\"%>

Example2_10_1.jsp

<%@ page contentType=\"text/html;charset=GB2312\" %>

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


<% String a=request.getParameter(\"ok\"); if(a==null) { a=\"1\"; } try{

double number=Integer.parseInt(a); out.print(\"
\"+Math.sqrt(number)); }

catch(NumberFormatException e)

{

out.print(\"
\"+\"请输入数字字符\"); } %>

例子11 Example2_11.jsp

<%@ page contentType=\"text/html;charset=GB2312\" %>

加载的文件:

加载的图象:

例子12 tom.jsp

<%@ page contentType=\"text/html;charset=GB2312\" %> <%

String str=request.getParameter(\"computer\"); //获取值。 int n=Integer.parseInt(str); int sum=0;

for(int i=1;i<=n;i++) {

sum=sum+i; } %>

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

Example2_12.jsp

<%@ page contentType=\"text/html;charset=GB2312\" %>

加载文件效果:

例子13 Example2_13.jsp

<%@ page contentType=\"text/html;charset=GB2312\" %> <%

double i=Math.random(); if(i>0.5) { %>

<% } else { %>

<% } %>

这句话和下面的表达式的值能输出吗? <%=i%>

例子14 come.jsp

<%@ page contentType=\"text/html;charset=GB2312\" %>

<%

String str=request.getParameter(\"number\"); double n=Double.parseDouble(str); %>

您传过来的数值是:
<%=n%>

Example2_14.jsp

<%@ page contentType=\"text/html;charset=GB2312\" %> <%

double i=Math.random(); %>

\" />

例子15 Example2_15.jsp

<%@ page contentType=\"text/html;charset=GB2312\" %>

Plugin tag OBJECT or EMBED not supported by browser.

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- yrrf.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务