赞
踩
1.通过HTML表单输入两个数字,提交给jsp程序,完成此两个数字相加结果的输出。
1)HTML代码:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>求和</title> </head> <body> <form action="sum.jsp" method="post"> <h1 align="center">求和</h1> <br> <br> <br> <br> <br> <br> <p align="center">输入a:<input type="text" name="number1" placeholder="请输入一个数"></p> <p align="center"> 输入b:<input type="text" name="number2" placeholder="请输入一个数"></p> <p align="center"> <input type="submit"value="求和"></p> </form> </body> </html>
2)jap程序代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>求和</title> </head> <body> <h1 align="center"> <% String number1 =request.getParameter("number1"); String number2=request.getParameter("number2"); double a=Double.parseDouble(number1); double b=Double.parseDouble(number2); double c; c=a+b; out.print("a+b="+c); %></h1> <a href="sum.html">返回上一级</a> </body> </html>
运行结果:
使用工具:Tomcat
2.通过HTML表单输入3个字符串,提交给jsp程序,完成在字符串1中统计字符串2出现的次数,并把各个字符串2字串在字符串1中替换为字符串3字串,最后把替换结果输出到浏览器。
1)HTML代码:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>字符串操作</title> </head> <body> <form action="str.jsp" method="post"> <h1 align="center">字符串操作</h1> <p align="center">字符串1:<input type="text" name="str1" placeholder="请输入一个字符串"></p> <p align="center">字符串2:<input type="text" name="str2" placeholder="请输入一个字符串"></p> <p align="center">字符串3:<input type="text" name="str3" placeholder="请输入一个字符串"></p> <p align="center"><input type="submit" value="提交"></p> </form> </body> </html>
2)jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>字符串操作</title> </head> <body> <h1 align="center">字符串操作</h1> <br> <br> <br> <br> <% request.setCharacterEncoding("UTF-8"); String str1=request.getParameter("str1"); String str2=request.getParameter("str2"); String str3=request.getParameter("str3"); String str4=str1; int num=0; int l=str2.length(); for(int i=0;i<=str1.length()-l;i++){ if(str2.indexOf(str1.substring(i, i+l))>-1){ num++; str4=str1.replace(str1.substring(i,i+l), str3); } } %> <p align="center"><% out.print("字符串1为:"+str1);%></P> <p align="center"><% out.print("字符串2为:"+str2);%></P> <p align="center"><% out.print("字符串3为:"+str3);%></P> <p align="center"><% out.print("字符串2被字符串1包含了"+num+"次");%></P> <p align="center"><% out.print("字符串2在字符串1中替换为字符串3后:"+str4);%></P> <a href=str.html>返回上一级</a> </body> </html>
运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。