JAVA:使用IntelliJ IDEA的JSP中显示MySQL数据

问题描述 投票:2回答:1

所以我近2个月后使用的IntelliJ和Java作为编程语言。我开发一个Web应用程序,我已经使用LoginServlet.java成功实施了用户的登录。但是我想,以显示JSP一个COUNT(*)输出,但我有是如何显示的数据输出的一些问题。我不知道如果我的servlet允许多个request.getRequestDispatcher或者我需要编写的代码在servlet(也许在JSP)之外。一些方向,将不胜感激。

这里是我的代码

UserLogin.java的Servlet

@WebServlet("/organizer")
public class UserLogin extends HttpServlet {

@SuppressWarnings("Duplicates")
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    try{

        String email = request.getParameter("email");
        String password = request.getParameter("password");

        OrganizerDao e = new OrganizerDao();

        HttpSession session = request.getSession();

        session.setAttribute("email", email);

        Connection con= OrganizerDao.getConnection();
        PreparedStatement st=con.prepareStatement("select id, firstname, lastname from users where email=? and password=?");
        st.setString(1,email);
        st.setString(2,password);
        ResultSet rs=st.executeQuery();
        if (rs.next()) {

            request.setAttribute("id",rs.getInt("id"));
            request.setAttribute("firstname",rs.getString("firstname"));
            request.setAttribute("lastname",rs.getString("lastname"));

            RequestDispatcher rd = request.getRequestDispatcher("dashboard.jsp");
            rd.include(request, response);

        } else {

            out.println("<div class=\"alert alert-danger\" role=\"alert\">");
            out.println("<center>Oh snap! Change a few things and try submitting again.</center>");
            out.println("</div>");

            request.getRequestDispatcher("login.jsp").include(request, response);
        }

        con.close();

    }catch(Exception ex){ex.printStackTrace();}


}


@SuppressWarnings("Duplicates")
protected void doPost(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException, IOException {

    processRequest(request,response);
    getEventsCount(request,response);
    getAffiliatesCount(request,response);

}


protected void doGet(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException, IOException {

    processRequest(request,response);
    getEventsCount(request,response);
    getAffiliatesCount(request, response);

}


@SuppressWarnings("Duplicates")
protected void getEventsCount(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    //call ConnectToDatabase


    try {

        String email = request.getParameter("email");
        String password = request.getParameter("password");

        HttpSession session = request.getSession();

        session.setAttribute("email", email);


        try {

            String query = "SELECT count(events.userid) AS etotal from events, users where events.userid=users.id and users.email=? and users.password=?";
            int count = 0;

            Connection connection = EventsDAO.getConnection();
            PreparedStatement st = connection.prepareStatement(query);
            st.setString(1, email);
            st.setString(2, password);
            ResultSet rs = st.executeQuery();
            while (rs.next()) {

                count = rs.getInt("etotal");

                request.setAttribute("count", count);

                RequestDispatcher rd = request.getRequestDispatcher("dashboard.jsp");
                rd.include(request, response);

            }

        } catch (SQLException e) {
            e.printStackTrace();
        }


    }  catch (Exception ex) {
    ex.printStackTrace();
}



}


@SuppressWarnings("Duplicates")
protected void getAffiliatesCount(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    //call ConnectToDatabase


    try {

        String email = request.getParameter("email");
        String password = request.getParameter("password");

        HttpSession session = request.getSession();

        session.setAttribute("email", email);


        try {

            String query = "SELECT count(affiliates.userid) AS affiliatestotal from affiliates, users where affiliates.userid=users.id and users.email=? and users.password=?";
            int affiliatescount = 0;

            Connection connection = EventsDAO.getConnection();
            PreparedStatement st = connection.prepareStatement(query);
            st.setString(1, email);
            st.setString(2, password);
            ResultSet rs = st.executeQuery();
            while (rs.next()){

                affiliatescount = rs.getInt("affiliatestotal");

                request.setAttribute("affiliatescount",affiliatescount);

                RequestDispatcher rd = request.getRequestDispatcher("dashboard.jsp");
                rd.include(request, response);

            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }catch(Exception ex){ex.printStackTrace();}
}

}

Dashboard.jsp

<div class="card card-sm">
                                <div class="card-body">
                                    <div class="d-flex justify-content-between mb-5">
                                        <div>
                                            <span class="d-block font-15 text-dark font-weight-500">My Events</span>
                                        </div>
                                        <div>
                                            <span class="text-warning font-14 font-weight-500">-2.8%</span>
                                        </div>
                                    </div>
                                    <div>
                                        <span class="d-block display-4 text-dark mb-5">${count}</span>
                                        <small class="d-block">100 Targeted</small>
                                    </div>
                                </div>
                            </div>

编辑:我也想知道,如果显示3种方法都带有请求调度重定向到同一页面允许作为在下面的代码

UserLogin.java的Servlet

@SuppressWarnings("Duplicates")
protected void doPost(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException, IOException {

    processRequest(request,response);
    getEventsCount(request,response);
    getAffiliatesCount(request,response);


}


protected void doGet(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException, IOException {

    processRequest(request,response);
    getEventsCount(request,response);
    getAffiliatesCount(request, response);

}

更新:我能得到使用字符串作为所有getDispatcher转发器的占位符解决数据显示问题。但是错误的页面现在犯规正常显示。这里是我的代码:

user login.Java

String page = "";

@SuppressWarnings("Duplicates")
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    //call ConnectToDatabase

    try{

        String email = request.getParameter("email");
        String password = request.getParameter("password");

        OrganizerDao e = new OrganizerDao();

        HttpSession session = request.getSession();

        session.setAttribute("email", email);

        Connection con= OrganizerDao.getConnection();
        PreparedStatement st=con.prepareStatement("select id, firstname, lastname from users where email=? and password=?");
        st.setString(1,email);
        st.setString(2,password);
        ResultSet rs=st.executeQuery();
        if (rs.next()) {

            request.setAttribute("id",rs.getInt("id"));
            request.setAttribute("firstname",rs.getString("firstname"));
            request.setAttribute("lastname",rs.getString("lastname"));

            //RequestDispatcher rd = request.getRequestDispatcher("dashboard.jsp");
            //rd.include(request, response);

            page = "dashboard.jsp";



        } else {

            out.println("<div class=\"alert alert-danger\" role=\"alert\">");
            out.println("<center>Oh snap! Change a few things and try submitting again.</center>");
            out.println("</div>");

            //request.getRequestDispatcher("/login.jsp").include(request, response);

            page = "/login.jsp";

        }

        con.close();

    }catch(Exception ex){ex.printStackTrace();}


}

 protected void doPost(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    //call ConnectToDatabase

    processRequest(request,response);
    getEventsCount(request,response);
    getAffiliatesCount(request,response);
    getTotalWithdrawal(request, response);

    if(page!=null){

        request.getRequestDispatcher("/dashboard.jsp").include(request, response);

    }else {

        out.println("<div class=\"alert alert-danger\" role=\"alert\">");
        out.println("<center>Oh snap! Change a few things and try submitting again.</center>");
        out.println("</div>");

        request.getRequestDispatcher("/login.jsp").include(request, response);


    }
}
java mysql jsp servlets intellij-idea
1个回答
0
投票

您的实现方式在这里下车。你需要做类似下面。您可以访问使用JSP EL要求的属性:

<p>Event Count is ${eventCount}</p>
<p>Affiliate Count is ${affiliateCount}</p>
<p>User Name is ${user.forename} ${user.surname}</p>

Servlet的

@WebServlet("/organizer")
public class UserLogin extends HttpServlet {

    protected void doGet(HttpServletRequest request,
                          HttpServletResponse response) 
         throws ServletException, IOException {

        doPost(request, response);
    }

    @SuppressWarnings("Duplicates")
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) 
         throws ServletException, IOException {

        String email = request.getParameter("email");
        String password = request.getParameter("password");

        User user = getUsert(emil password)

        if(user != null){
            request.setAttributes("eventCount", getEventsCount(email, password));
            request.setAttributes("affiliateCount", getAffiliatesCount(email,password));
            request.getRequestDispatcher("/dashboard.jsp").forward(request, response);
        }else{
            request.setAttribute("failedLogin", true);
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
    }

    protected User getUser(String email  String password){}

        User user = null; //create a class user with 3 fields

        //qeuery

        if(rs.next()){
            user = new User(rs.getInt("id"), 
                                rs.getString("firstname"), 
                                    rs.getString("lastname"))
        }

        return user;
    }

    protected int getEventsCount(String email, String password)
            throws ServletException, IOException {
        //query and return result
        return count;
    }

    protected void getAffiliatesCount(String email, String password)
            throws ServletException, IOException {
        //query and return result
        return count;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.