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;
publicclassCalServletextendsHttpServlet {/**
* Destruction of the servlet. <br>
*/publicvoiddestroy() {
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
*/publicvoiddoDelete(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
*/publicvoiddoGet(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
*/publicvoiddoPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/publicvoidinit() throws ServletException {
// Put your code here
}
}
<?xml version="1.0" encoding="UTF-8"?><web-appxmlns: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><formaction="CalServlet"method="get">
操作数1:<inputtype="text"name="num1"value="${cal.num1}"><br>
运算符:<inputtype="radio"name="op"value="+"${requestScope.cal.op=='+'?"checked":""}>+
<inputtype="radio"name="op"value="-"${requestScope.cal.op=='-'?"checked":""}>-
<inputtype="radio"name="op"value="*"${requestScope.cal.op=='*'?"checked":""}>*
<inputtype="radio"name="op"value="/"${requestScope.cal.op=='/'?"checked":""}>/<br>
操作数2:<inputtype="text"name="num2"value="${cal.num2}"><br><inputtype="submit"value="计算"><br>
结果:<inputtype="text"value=" ${requestScope.result}" ></form></body></html>