繁体   English   中英

Servlet 的 OnClick > 需要从数据库中获取特定人员的数据并显示在 JSP

[英]OnClick of Servlet > Need to Fetch the Data of Particular Person from Database and Display it on JSP

Java -代码 这里我的数据库中有 8 个订阅者,它以表格格式显示在前端(列 - 用户名、订阅者类型、日期、有效期至)单击订阅者用户名 > 它必须获取该特定名称和他的有效性细节。 这必须使用 JAVA 来完成。 Here I have used Servlet Counter, So onclick the Count has been increased every time and the count has been passed as the parameter to my Model Class which accepts the count has parameter and the data from the database have been stored in ArrayList. 使用 ArrayList getInt 方法,计数已被传递到此。 该应用程序运行良好! 但是当应用程序到达更多访问者并且 servlet 计数未初始化回hitCount为零时。 如何在 java 中动态获取数据? 以及我可以在这段代码中改进什么以获得正确的结果!

小服务程序计数器

private int hitCount; 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();
        hitCount++
        try {
            Model m=new Model();

            System.out.println("Entering getstatement method...");
            String NAME=m.getStatement(hitCount);

                session.setAttribute("NAME", NAME);
                response.sendRedirect("ManageAccount.jsp");
                System.out.println(NAME);
            }


        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

Model Class

public class Model{

public String getStatement(int hitCount) throws Exception 

    {

       System.out.println("Inside getStatement..");
        pst = conn
                .prepareStatement("SELECT user FROM trillup.subscribers");
       ArrayList ar=new ArrayList();

        rs = pst.executeQuery();


        ar.add("");
        while(rs.next()==true) 
        {
           System.out.println("Inside while loop");
            ar.add(rs.getString("user"));
        }

            System.out.println(ar);

        hitCount++;
        String NAME=(String) ar.get(hitCount);
        return NAME;



    }  
}

您是否尝试在 DoGet 方法之前初始化 hitCount = 0 ? 查看 sql JSTL 库手册。 在获取数据方面非常有趣且易于使用。 不需要任何数据结构的掌握

如果我正确理解您的问题,您希望在每次请求订阅者时保持命中计数计数器增加。

不要在主 memory 中保留计数,如果 jvm 崩溃? 您将丢失数据,因此请在表级别维护此计数器。

编辑 - 让您的计数器在任何动态值上重新初始化(可能从某些配置中读取),并将表中的逻辑重新初始化为零。

并尝试整齐地定义 model class。

Class Model{
Private List<Subscriber> subscribers;
// getter setter
}

class Subscriber {
Long hitcount ;
//Rest attributes

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM