题目要求:第一个Servlet1文件,读取两个请求参数first和second,将其转换为int值后相加,和存放在请求范围内,然后把请求转发第二个Servlet2.。第二个Servlet2文件(url为output),向客户输出Servlet1计算的结果。根据注释补充下列代码。
public class Servlet1 extends HttpServlet {
public void service(ServletRequest request,ServletResponse response)
throws ServletException, IOException {
//读取两个请求参数,并将其转换为int值
int first = Integer.valueOf(request.getParameter(“first”)).intValue();________(3分)
int second=Integer.valueOf(request.getParameter(“second”)).intValue();___(3分)
//将两个数字相加
int sum=first+second;_____(3分)
//在request对象中添加sum属性
_ request.setAttribute(“sum”, String.valueOf(sum));______(3分)
/*把请求转发给Servlet2 */
_ ServletContext context = getServletContext();
RequestDispatcher dispatcher =context.getRequestDispatcher(“/output”);
dispatcher.forward(request, response);_____(5分)
}
}
public class Servlet2 extends HttpServlet {
public void service(ServletRequest request,ServletResponse response)
throws ServletException, IOException {
//读取请求范围内Servlet1计算出的和
_ String sum=(String)request.getAttribute(“sum”);_________(3分)
//向客户端输出和
PrintWriter out=response.getWriter();
out.print(“the sum is “+sum);_________(3分)
}
}
|