java web eclipse

登录获取的是数据库里面的username和password,目前想实现slist界面只显示登录账号的数据库信息,请问应该如何修改代码



```java
 dao层:public List<Only> findAll();
daoimpl层: public List<Only> findAll() {
        //使用JDBC操作数据库...
        //1.定义sql
        String sql = "select * from only";
        List<Only> students = template.query(sql, new BeanPropertyRowMapper<Only>(Only.class));
 
        return students;
    }
service层: public List<Only> findAll();
 serviceimpl层:public List<Only> findAll() {
        //调用Dao完成查询
        return dao.findAll();
servlet层:/获取条件查询参数
        Map<String, String[]> condition = request.getParameterMap();


        //2.调用service查询
        OnlyService service = new OnlyServiceImpl();
        PageBean<Only> pb = service.findUserByPage(currentPage,rows,condition);

        System.out.println(pb);

        //3.将PageBean存入request
        request.setAttribute("pb",pb);
        request.setAttribute("condition",condition);//将查询条件存入request
        //4.转发到list.jsp
        request.getRequestDispatcher("/slist.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
 

```

参考GPT和自己的思路:

你需要在servlet层中取出当前登录用户的用户名,然后将其作为条件传递给service层进行查询。具体的代码如下:

servlet层:
//获取当前登录用户的用户名
String currentUsername = (String) request.getSession().getAttribute("username");

//构建查询条件
Map<String, String[]> condition = new HashMap<String, String[]>();
condition.put("username", new String[]{currentUsername});

//调用service查询
OnlyService service = new OnlyServiceImpl();
List<Only> onlyList = service.findAll(condition);

//将查询结果存入request
request.setAttribute("onlyList", onlyList);

//转发到slist.jsp页面
request.getRequestDispatcher("/slist.jsp").forward(request, response);

这样就可以实现只显示当前登录用户的信息了。需要注意的是,需要在登录的servlet中将用户名保存至session中,以便后续使用。

没看到你显示的代码,是不是在jsp页面里面,找一下,修改下即可