serverlet中,使用 request.getRequestDispatcher发送数据到jsp页面中处理失败
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletEx...
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String num1 = request.getParameter("num1");
String num2 = request.getParameter("num2");
String oper = request.getParameter("oper");
int res = Operation.getResult(num1, num2, oper);
request.setAttribute("num1", num1);
request.setAttribute("num2", num2);
request.setAttribute("oper", oper);
request.setAttribute("result", num1);
System.out.println(res);
request.getRequestDispatcher("/view/result.jsp").forward(request, response);
}
result.jsp:
<%
String num1=request.getParameter("num1");
String num2=request.getParameter("num2");
String oper=request.getParameter("oper");
String result=request.getParameter("result");
System.out.println(result);
%>
第一个数:<%=num1 %><br>
第二个数:<%=num2 %><br>
运算符:<%=oper %><br>
结果:<%=result %>
结果4个数都没能够取到值,num1,num2,oper可以显示,但应该是最初的那个页面发过来的值,result显示为null 展开
throws ServletException, IOException {
String num1 = request.getParameter("num1");
String num2 = request.getParameter("num2");
String oper = request.getParameter("oper");
int res = Operation.getResult(num1, num2, oper);
request.setAttribute("num1", num1);
request.setAttribute("num2", num2);
request.setAttribute("oper", oper);
request.setAttribute("result", num1);
System.out.println(res);
request.getRequestDispatcher("/view/result.jsp").forward(request, response);
}
result.jsp:
<%
String num1=request.getParameter("num1");
String num2=request.getParameter("num2");
String oper=request.getParameter("oper");
String result=request.getParameter("result");
System.out.println(result);
%>
第一个数:<%=num1 %><br>
第二个数:<%=num2 %><br>
运算符:<%=oper %><br>
结果:<%=result %>
结果4个数都没能够取到值,num1,num2,oper可以显示,但应该是最初的那个页面发过来的值,result显示为null 展开
展开全部
<%
String num1=request.getParameter("num1");
String num2=request.getParameter("num2");
String oper=request.getParameter("oper");
String result=request.getParameter("result");
System.out.println(result);
%>
这个地方应该改为
<%
String num1=(String)request.getAttribute("num1");
String num2=(String)request.getAttribute("num2");
String oper=(String)request.getAttribute("oper");
String result=(String)request.getAttribute("result");
System.out.println(result);
%>
String num1=request.getParameter("num1");
String num2=request.getParameter("num2");
String oper=request.getParameter("oper");
String result=request.getParameter("result");
System.out.println(result);
%>
这个地方应该改为
<%
String num1=(String)request.getAttribute("num1");
String num2=(String)request.getAttribute("num2");
String oper=(String)request.getAttribute("oper");
String result=(String)request.getAttribute("result");
System.out.println(result);
%>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询