JSP编程教程:基于JSP实现一个简单计算器的方法
2015-5-7编辑:ljnbset
本文实例讲述了基于JSP实现一个简单计算器的方法。分享给大家供大家参考。具体实现方法如下:
index.jsp
复制代码 代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
我的计算器
result.jsp用于显示结果
复制代码 代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
String num1=request.getParameter("num1");
String num2 = request.getParameter("num2");
String flag = request.getParameter("flag");
int s_num1=Integer.parseInt(num1);
int s_num2=Integer.parseInt(num2);
int result=0;
if(flag.equals("+")){
//加
result=s_num1+s_num2;
}else if(flag.equals("-")){
//减
result=s_num1-s_num2;
}else if(flag.equals("/")){
result=s_num1/s_num2;
//除
}else{
//乘
result=s_num1*s_num2;
}
out.println("结果是:"+result);
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
String num1=request.getParameter("num1");
String num2 = request.getParameter("num2");
String flag = request.getParameter("flag");
int s_num1=Integer.parseInt(num1);
int s_num2=Integer.parseInt(num2);
int result=0;
if(flag.equals("+")){
//加
result=s_num1+s_num2;
}else if(flag.equals("-")){
//减
result=s_num1-s_num2;
}else if(flag.equals("/")){
result=s_num1/s_num2;
//除
}else{
//乘
result=s_num1*s_num2;
}
out.println("结果是:"+result);
%>