关于测试jsp、servlet应用的一道试题

来源:互联网 发布:c语言close函数 编辑:程序博客网 时间:2024/04/28 07:52

下面是用jsp、servlet开发的一简单的web应用。需求描述如下:用jsp、servlet等技术开发一个网站应用程序,实现两个整数的加减乘(除暂时不考虑)运算。现有如下应用程序,请尽可能的指出其中存在的问题,并修正。其中采用jsp1.2,servlet2.3,在tomcat 4服务器上运行。

程序介绍如下:

有两个jsp文件,index.jsp显示输入两个数据和一个运算符号,提交到servlet----MathServlet进行运算,运算的结果在result.jsp页面上显示:

index.jsp代码如下:

<%@ page contentType="text/html;charset=gb2312" language="java" %>
<html>
  <head><title>test java </title></head>
  <body>Place your content here</body>
   <FORM METHOD=POST ACTION="<%=request.getContextPath()%>/MathServlet">
    <BR>
    <BR> <INPUT NAME="p1"  VALUE="">
    <BR> <INPUT NAME="p2"  VALUE="">
    <BR> <INPUT NAME="method"  VALUE="">
    <BR> <INPUT TYPE=SUBMIT VALUE="submit">
    </FORM>
</html>

result.jsp代码如下:

<%@ page contentType="text/html;charset=gb2312" language="java" %>
<%
    int result=request.getParameter("reslut");
%>
<html>
  <br>
  <%=result%>
</html> 

servlet----MathServlet代码如下:

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;


public class MathServlet extends HttpServlet {

    private int a;
    private int b;
    private String mathMethod;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        a=new Integer(request.getParameter("p1")).intValue();
        b=new Integer(request.getParameter("p2")).intValue();
        mathMethod=request.getParameter("method");
        int result=Canculate();
        request.setAttribute("result",new Integer(result).toString());       
        getServletConfig().getServletContext().getRequestDispatcher("/result.jsp").forward(request, response);
    }


    public int Canculate() throws ServletException{
        MathBean mathBean=new MathBean(a,b);
        int result=0;
        if(mathMethod=="+"){
            result=mathBean.add();
        }else if(mathMethod=="-"){
            result=mathBean.substract();
        }else if(mathMethod=="*"){
            result=mathBean.multiple();
        }else{
            throw new ServletException("参数错误");
        }
        return result;
    }


}

其中MathBean代码如下:

public class MathBean {
    int a;
    int b;

    public MathBean(int a,int b){
        this.a=a;
        this.b=b;
    }
    //加
    public int add(){
        return a+b;
    }
    //减
    public int substract(){
        return a-b;
    }
    //乘
    public int multiple(){
        return a*b;
    }


}

部署文件web.xml如下:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

    <servlet>
        <servlet-name>MathServlet</servlet-name>
        <servlet-class>MathServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>MathServlet</servlet-name>
        <url-pattern>/MathServlet</url-pattern>
    </servlet-mapping>
</web-app>

原创粉丝点击