贴出,一个简单的基于Servlet的网络应用(实现 + - * / 功能)程序。
web.xml
<!-- Our servlet, the target of the form submission --> <servlet> <servlet-name>mathServlet</servlet-name> <servlet-class>vivi.test.dwr.MathServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>mathServlet</servlet-name> <url-pattern>/mathServlet</url-pattern> </servlet-mapping> <!-- Default page to load in context --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
MathServlet.java
/**
* @author vivi
* This servlet is the target of the form submission.
* It dispatches(调度, 调遣) to MathDelegate(代表, 委员, 特派员) class to do the actual work,
* then forward to index.jsp
*/
public class MathServlet extends HttpServlet {
/* The typical doPost() of a servlet
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//Get the number to operate on, and operation to perform,as specified by the caller
int a = Integer.parseInt(request.getParameter("a").toString());
int b = Integer.parseInt(request.getParameter("b").toString());
String op = request.getParameter("op").toString();
MathDelegate mathDelegate = new MathDelegate();
int answer = 0;
//Call the MathDelegate to perform the appropriate(适合的; 适当的; 相称的) operation,
//and store a math symbol(记号, 符号) representing(现, 表示, 描绘, 讲述, 代表, 象征,)
//the operation in request.
if("add".equalsIgnoreCase(op)){
answer = mathDelegate.add(a, b);
request.setAttribute("op", "+");
}else if("subtract".equalsIgnoreCase(op)){
answer = mathDelegate.subtract(a, b);
request.setAttribute("op", "-");
}else if("multiply".equalsIgnoreCase(op)){
answer = mathDelegate.multiply(a, b);
request.setAttribute("op", "*");
}else if("divide".equalsIgnoreCase(op)){
answer = mathDelegate.divide(a, b);
request.setAttribute("op", "/");
}
//Add the two number and answer to request, so our JSP can display it.
request.setAttribute("a", Integer.valueOf(a));
request.setAttribute("b", Integer.valueOf(b));
request.setAttribute("answer", Integer.valueOf(answer));
//Forward to index.jsp
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
}
}
MathDelegate.java
/**
* @author vivi
* A class perform basic mathmatical operations on two numbers.
*/
public class MathDelegate {
/**
* Add two number
* @param a the first number
* @param b the second number
* @return the result of the operation
*/
public int add(int a, int b){
return a+b;
}
/**
* Subtract two number
* @param a the first number
* @param b the second number
* @return the result of the operation
*/
public int subtract(int a, int b){
return a-b;
}
/**
* Multiply two number
* @param a the first number
* @param b the second number
* @return the result of the operation
*/
public int multiply(int a, int b){
return a*b;
}
/**
* Divide two number
* @param a the first number
* @param b the second number
* @return the result of the operation
*/
public int divide(int a, int b){
return a/b;
}
}
index.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=ISO-8859-1">
<title>第一个简单的DWR实例-----非DWR实现,用于对比DWR的便捷</title>
</head>
<body>
<%
Integer answer = (Integer)request.getAttribute("answer");
//if there's an answer attribute in request,we just performed an operation , so display the result
if(answer!=null){
Integer a = (Integer)request.getAttribute("a");
String op = (String)request.getAttribute("op");
Integer b = (Integer)request.getAttribute("b");
%>
<span>Result: <%= a%> <%= op%> <%= b%> = <%= answer%></span>
<%
}
%>
<br></br>
Please enter two numbers, select an operation , and click the equals button:
<br></br>
<form action="mathServlet" method="post">
<input type="text" name="a" size="4">
<select name="op">
<option value="add">+</option>
<option value="subtract">-</option>
<option value="multiply">*</option>
<option value="divide">/</option>
</select>
<input type="text" name="b" size="4">
<input type="submit" value="=">
</form>
</body>
</html>
************************************************************************