gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:16:45 26 4
gpt4 key购买 nike

所以我已经将近 2 个月来使用 intelliJ 和 Java 作为编程语言。我正在开发一个 Web 应用程序,我已经使用 LoginServlet.java 成功实现了用户登录。但是我想在 JSP 中显示 COUNT(*) 输出,但我对如何显示数据输出有一些问题。我不确定我的 servlet 是否允许多个 request.getRequestDispatcher 或者我需要在 servlet 之外(可能在 jsp 中)编写代码。一些指导将不胜感激。

这是我的代码

用户登录.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();}
}

}

仪表板.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 种方法,所有方法都带有重定向到同一页面的请求调度程序,如下面的代码所示

用户登录.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 转发器的占位符,我能够解决数据显示问题。但是错误页面现在不能正确显示。这是我的代码:

用户登录.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);


}
}

最佳答案

您的实现还差得远。您需要执行以下操作。您可以使用 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;
}
}

关于JAVA:使用IntelliJ IDEA在JSP中显示mysql数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54617618/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com