20170828_chr_MyCalculator 用servlet编写计算器程序
来源:互联网 发布:linux snmp v3 编辑:程序博客网 时间:2024/06/06 20:36
20170828_chr_MyCalculator 用servlet编写计算器程序
20170828_chr_MyCalculator
- /20170828_chr_MyCalculator/src/nuc/sw/servlet/CalServlet.java
package nuc.sw.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import nuc.sw.vo.Calculator;public class CalServlet extends HttpServlet { /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doDelete method of the servlet. <br> * * This method is called when a HTTP delete request is received. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Calculator cal = new Calculator(); cal.setNum1(Float.parseFloat(request.getParameter("num1"))); cal.setNum2(Float.parseFloat(request.getParameter("num2"))); cal.setOp(request.getParameter("op")); float result = 0; switch (cal.getOp().charAt(0)) { case '+': result = cal.getNum1() + cal.getNum2();break; case '-': result = cal.getNum1() - cal.getNum2();break; case '*': result = cal.getNum1() * cal.getNum2();break; case '/': result = cal.getNum1() / cal.getNum2();break; } request.setAttribute("cal", cal); request.setAttribute("result", result); request.getRequestDispatcher("cal.jsp").forward(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here }}
- /20170828_chr_MyCalculator/src/nuc/sw/vo/Calculator.java
package nuc.sw.vo;public class Calculator { private float num1; private String op; private float num2; public float getNum1() { return num1; } public void setNum1(float num1) { this.num1 = num1; } public String getOp() { return op; } public void setOp(String op) { this.op = op; } public float getNum2() { return num2; } public void setNum2(float num2) { this.num2 = num2; }}
- /20170828_chr_MyCalculator/WebRoot/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>20170828_chr_MyCalculator</display-name> <display-name>CalServlet</display-name> <description>This is the description of my J2EE component</description> <servlet> <servlet-name>CalServlet</servlet-name> <servlet-class>nuc.sw.servlet.CalServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CalServlet</servlet-name> <url-pattern>/CalServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list></web-app>
- /20170828_chr_MyCalculator/WebRoot/cal.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <title>简易计算器</title></head><body><form action="CalServlet" method="get"> 操作数1:<input type="text" name="num1"value="${cal.num1}"><br> 运算符:<input type="radio" name="op" value="+"${requestScope.cal.op=='+'?"checked":""}>+ <input type="radio" name="op" value="-"${requestScope.cal.op=='-'?"checked":""}>- <input type="radio" name="op" value="*"${requestScope.cal.op=='*'?"checked":""}>* <input type="radio" name="op" value="/"${requestScope.cal.op=='/'?"checked":""}>/<br> 操作数2:<input type="text" name="num2" value="${cal.num2}"><br> <input type="submit" value="计算"><br> 结果:<input type="text" value=" ${requestScope.result}" > </form></body></html>
阅读全文
0 0
- 20170828_chr_MyCalculator 用servlet编写计算器程序
- 用C#编写的计算器小程序
- 用java scanner 编写计算器程序
- 用servlet编写下载程序
- 【分享】【计算器程序--java编写】
- 5.编写一个计算器程序
- 用servlet编写的文件上传程序
- 编写 Servlet 程序 - 1
- 编写 Servlet 程序 - 2
- 编写 Servlet 程序 - 3
- 手工编写servlet程序
- IntelliJIDEA编写servlet程序
- 用VB编写的第一个WINDOWS程序----计算器
- 用NetBeans编写计算器
- 用C++编写计算器
- Java编写的计算器程序及源代码
- Java编写的计算器程序及源代码
- Java编写一个计算器程序过程介绍
- LEAM(生活体验算法模型)
- NASA的10条代码编写原则
- PL/SQL Developer 安装详细图文教程
- Apache Tomcat漏洞 之 CVE-2017-12617
- 通过ADB命令获取Android手机的分辨率(升级版)
- 20170828_chr_MyCalculator 用servlet编写计算器程序
- POJ3294 Life Forms 【后缀数组】
- sphinx rst文件转化为html文件
- 【UE4学习】21_Crypto++_AES加密解密
- 【ML项目】基于网络爬虫和数据挖掘算法的web招聘数据分析(一)——数据获取与处理
- 润乾报表V5超链接回退
- Express的安装与使用
- mysql 存储过程 小事例
- 手机页面点击链接蓝色背景去掉