-kric- 2019-05-27 19:04 采纳率: 0%
浏览 2190
已采纳

创建List对象,老是报错The left-hand side of an assignment must be a variable?

想利用List实现向数据库中插入一堆数据,但是创建List对象时出错。
List list = (List)session.getAttribute("listname");
if(list==null)
List list = new ArrayList(); //???The left-hand side of an assignment must be a variable?
list.add(one);
session.setAttribute("listname", list);

    完整代码
package servlet;

import java.io.IOException;
import java.util.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.St;

/**
 * Servlet implementation class Insert_2
 */
@WebServlet("/insert_2")
public class Insert_2 extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private ServletRequest session;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Insert_2() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");

        String xh = request.getParameter("xh");
        String xm = request.getParameter("xm");
        double cj = Double.parseDouble(request.getParameter("cj"));

        String xz = request.getParameter("xz");

        St one = new St(xh, xm, cj);

        List list = (List)session.getAttribute("listname");
        if(list==null) 
            List<St> list = new ArrayList<St>();   //???
        list.add(one);
        session.setAttribute("listname", list);

        if(!xz.equals("no")) {
            request.setAttribute("aaa", list);
            RequestDispatcher rd = request.getRequestDispatcher("insert_22");
            rd.forward(request, response);
            System.out.println(xz+"结束");
        }
        else {
            RequestDispatcher rd = request.getRequestDispatcher("input_insert1.jsp");
            rd.forward(request, response);
            System.out.println(xz+"继续");
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

  • 写回答

1条回答 默认 最新

  • 毕小宝 博客专家认证 2019-05-28 09:10
    关注

    前面已经定义过 list 了,正确代码应该是:

     if(list==null) {
                 list = new ArrayList<St>();   //???
    }
    
            list.add(one);
    
    

    如果从会话中获取的 list 属性为空,就重新 new 一个。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 Spring MVC项目,访问不到相应的控制器方法
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?
  • ¥15 关于#genesiscsheel#的问题,如何解决?
  • ¥15 Android aidl for hal
  • ¥15 STM32CubeIDE下载程序报错
  • ¥15 微信好友如何转变为会员系统?(相关搜索:小程序)
  • ¥15 c# 直接使用c++ 类库文件
  • ¥15 一个主机电脑有两个显示器,当前有两个软件主体是网页html打包的exe程序,如何通过cmd命令bat同时打开软件一个在主屏幕显示,另外一个软件在2副屏幕上显示