qq_43764144
qq_43764144
2019-05-05 20:59
采纳率: 43.5%
浏览 769

int类型转换成User对象类型报错问题

想询问一下大佬们,出现了Integer转换成User对象报错的问题代码如下

private void UserUpdate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取数据
            String pwd=req.getParameter("pwd");
            //从session中获取用户信息//下面的User的转换出了问题
            User u=(User) req.getSession().getAttribute("servlet");
            int uid=u.getUid();
            //调用service处理
            int index=us.Update(pwd,uid);
            System.out.println("index="+index);
            if(index>0){
                //获取session对象
                HttpSession hs=req.getSession();
                hs.setAttribute("pwd","true");
                //重定向到登录页面
                resp.sendRedirect("/Login.jsp");
            }

            ——————————————————————————————————————————————————————
            : Servlet.service() for servlet [servlet] in context with path [/New] threw exception
java.lang.ClassCastException: java.lang.Integer cannot be cast to com.cwj.pojo.User
    at com.cwj.servlet.UserServlet.UserUpdate(UserServlet.java:88)
    at com.cwj.servlet.UserServlet.service(UserServlet.java:41)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.logging.log4j.core.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:66)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1137)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:319)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • pengcan52021
    pengcan52021 2019-05-05 21:18

    int 肯定不能直接强转成 user 对象 撒,具体错误分析 要看看 你放入session 的时候放的什么值

    点赞 评论
  • w2276698848
    w2276698848 2019-05-05 22:46

    这种是类型转换错误,int是整形,而user是个对象,所以不能直接强转

    点赞 评论

相关推荐