为了账号安全,请及时绑定邮箱和手机立即绑定

JAVA:使用 IntelliJ IDEA 在 JSP 中显示 mysql 数据

JAVA:使用 IntelliJ IDEA 在 JSP 中显示 mysql 数据

千巷猫影 2022-06-15 10:37:34
所以我使用 intelliJ 和 Java 作为编程语言已经快 2 个月了。我正在开发一个 Web 应用程序,并且我已经成功实现了使用LoginServlet.java. 但是我想COUNT(*)在 JSP 中显示一个输出,但是我在如何显示数据输出方面遇到了一些问题。我不确定我的 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();}}}}
查看完整描述

1 回答

?
慕村225694

TA贡献1880条经验 获得超4个赞

你的实现在这里很遥远。您需要执行以下操作。您可以使用 JSP EL 访问请求属性:


<p>Event Count is ${eventCount}</p>

<p>Affiliate Count is ${affiliateCount}</p>

<p>User Name is ${user.forename} ${user.surname}</p>

小服务程序


@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

        

        // Query

        

        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;

    }

}


查看完整回答
反对 回复 2022-06-15
  • 1 回答
  • 0 关注
  • 244 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信