我使用一个html文件从用户获取值,当我在浏览器中执行web应用程序时,servlet使用该文件执行特定的calculations.And。它从用户获取值,但当我在获取值后单击提交按钮时,会抛出此错误。“访问/servlet/MvcServlet时出现HTTP ERROR 500问题。原因:无法将java.lang.Integer强制转换为java.lang.String”
我的servlet代码如下所示:
int gpa=total/c;
req.setAttribute("gpa",gpa);
RequestDispatcher view = req.getRequestDispatcher("/result.jsp");
view.forward(req, resp); `我的jsp代码是:
String gpa = (String) request.getAttribute("gpa");
int r=Integer.parseInt(gpa);
out.println("Your Result is "+ r);请帮助我将servlet中的整数值"gpa“传递给jsp。
发布于 2014-05-17 19:18:06
int gpa =(整数) request.getAttribute("gpa");
发布于 2014-05-18 01:35:25
显而易见的答案是以Integer的形式获取数据,并让Java将其自动装箱到int中。这段代码值得注意的是:
int gpa = (Integer) request.getAttribute("gpa");但是你**should avoid having scriptlets (Java code) directly in your code。因此,最好的方法是直接在JSP代码中使用表达式语言:
<!DOCTYPE html>
<html lang="es">
<head>
<!-- head content here -->
</head>
<body>
<!-- other content in your JSP file -->
Your result is: ${gpa}
<!-- There's no need of senseless scriptlet code -->
</body>
</html>发布于 2014-05-17 18:55:43
当您将int值发送到jsp时,您可以尝试这样做。
int gpa = total/c;
String test = ""+gpa;
req.setAttribute("test",test);
RequestDispatcher view = req.getRequestDispatcher("/result.jsp");
view.forward(req, resp); 然后,在jsp中,您不必解析它。直接去吧-
String gpa = (String) request.getAttribute("test");
out.println("Your Result is "+ gpa);试试这个:D
https://stackoverflow.com/questions/23710314
复制相似问题