我在servlet中编写了一个代码,以执行两个数字的加法并显示它,并将servlet转发到另一个jsp。但是,不会显示相加的结果。
它仅转发到jsp页面而不显示结果。
public class Add extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException
{
int i=Integer.parseInt(request.getParameter("t1"));
int j=Integer.parseInt(request.getParameter("t2"));
int k=i+j;
request.setAttribute("sum",k);
RequestDispatcher rd=request.getRequestDispatcher("/Welcome.jsp");
rd.forward(request,response);
}
}
JSP代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<HTML>
<HEAD> <TITLE> The Welcome JSP </TITLE> </HEAD>
<BODY>
<H3> Welcome! </H3>
<% request.getAttribute("sum"); %>
<P><B> Today is. Have a nice day! </B></P>
</BODY>
</HTML>
我想要的输出为The sum is ......和其余的jsp部分。将该servlet转发到jsp页面,但不显示总和。