protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
ServletContext context=this.getServletContext();
if(context.getAttribute("count")==null){
context.setAttribute("count", 1);
String num=(String)context.getAttribute("count");
out.println(num);
}else{
Long i=(Long)context.getAttribute("count");
Integer num2=new Integer(String.valueOf(i));
out.println(num2);
}
出现下面错误
严重: Servlet.service() for servlet [my1] in context with path [/first] threw exception
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
求解答
本文探讨了在Servlet中遇到的ClassCastException错误,具体为尝试将Integer类型转换为String类型时引发的问题。通过分析代码片段,我们将深入理解类型转换的正确方式,并提供解决方案。
1248

被折叠的 条评论
为什么被折叠?



