My main goal is to list properties from the DB and have authentication capabilities using entities, ejb, servlets and jsps. The first issue is my list of properties won't show on the jsp. I also can't seem to send inputted data back to the same page. I'm not sure what I am missing here. Using means of testing, I can display whats below and nothing else. "House by the beach" is actually coming the propertiesSessionBean.listAllProperties(). I took that column out before sending it to the servlet and that works but I can't seem to send and use the list like examples I've seen. I have a lot of commented code show what I've been trying. As you can see as well "hi" is supposed to display for each item in the list however there is nothing. The list seems to be emptying itself along the line. Any help would be great Thank you.
yo1
Table headings: ID Description Rent or Sale Price
yo2
Connection Exist
House by the beach
Log in
Success!
Here is my index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://ift.tt/QfKAz6" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Homepage</title>
</head>
<body>
<table border="1">
<th>ID</th>
<th>Description</th>
<th>Rent or Sale</th>
<th>Price</th>
yo1
<c:forEach items="${allProperties}" var="p">
hi
<tr>
<td>${p.id}</td>
<td>${p.description}</td>
<td>${p.rentsale}</td>
<td>${p.price}</td>
</tr>
</c:forEach>
</table>
yo2
<p>${con}</p>
<p>${queryResult}</p>
<h4>Log in</h4>
<form action="indexServlet" method="post">
<input type="hidden" name="loginformhiddeninput" value="login"/>
<input type="text" name="username" placeholder="username" value="" />
<input type="text" name="password" placeholder="password" value=""/>
<button name="loginbutton" type="submit"> Log in </button>
</form>
<p>${loggedinuser}</p>
<p>${details[1]}</p>
<p>${details.password}</p>
<p>${test[3]}</p>
</body>
</html>
Here's my indexServlet
@WebServlet(name = "indexServlet", urlPatterns = {"/indexServlet"})
public class indexServlet extends HttpServlet {
@EJB
private authentication authentication;
@EJB
private propertiesSessionBean propertiesSessionBean;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//List<Properties> allProperties = propertiesSessionBean.listAllProperties();
request.setAttribute("allProperties",propertiesSessionBean.listAllProperties());
//request.setAttribute("allProperties", allProperties);
//request.getRequestDispatcher("/index.jsp").forward(request, response);
boolean test = propertiesSessionBean.CheckConnection();
String con = "";
if (test)
{
con = "Connection Exist";
}
else
{
con = "Connection FAILED!";
}
String queryTest = propertiesSessionBean.checkQuery();
String queryResult = "";
if (queryTest != "")
{
queryResult = queryTest;
}
else
{
queryResult = "Query Result is Empty";
}
request.setAttribute("con", con);
request.setAttribute("queryResult", queryResult);
request.setAttribute("test", propertiesSessionBean.test());
// request.setAttribute("allProperty", allProperties);
request.getRequestDispatcher("./index.jsp").forward(request, response);
//
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet indexServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>You typed " + request.getParameter("username") + " </h1>");
out.println("</body>");
out.println("</html>");
out.println("Hello " + request.getParameter("username") + " at " + new java.util.Date());
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
String username = request.getParameter("username");
String password = request.getParameter("password");
String s = username;
request.setAttribute("details", s);
// String keyword = request.getParameter("loginformhiddeninput");
// if (keyword == "login") {
//
// authentication.login(username, password);
// request.setAttribute("loggedinuser", authentication.login(username, password));
request.getRequestDispatcher("index.jsp").forward(request, response);
// response.sendRedirect("customerhomepage.jsp");
//
//
//
// } else {
// // no such user exits please register
// }
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Here is the propertiesSessionBean
public class propertiesSessionBean {
@EJB
private databaseSessionBean databaseSessionBean;
public List<Properties> listAllProperties() {
Connection connection = databaseSessionBean.dbConnection();
List<Properties> property = new ArrayList<>();
try {
//1.Check if customer
String properties = "select * from ESTATEDB.\"Properties\";";
PreparedStatement stmt = connection.prepareStatement(properties);
ResultSet propertyresult = stmt.executeQuery();
while(propertyresult.next()) {
Properties p = new Properties();
p.setId(propertyresult.getInt(1));
p.setOwner(propertyowner(propertyresult.getInt(2)));
p.setDescription(propertyresult.getString(3));
p.setType(propertyresult.getString(3));
p.setRooms(propertyresult.getInt(4));
p.setBathrooms(propertyresult.getString(5));
p.setFurnished(propertyresult.getString(6));
p.setGarden(propertyresult.getString(7));
p.setSize(propertyresult.getString(8));
p.setPrice(propertyresult.getDouble(9));
p.setRentSale(propertyresult.getString(10));
p.setStreet(propertyresult.getString(11));
p.setCity(propertyresult.getString(12));
p.setPostcode(propertyresult.getString(13));
p.setCountry(propertyresult.getString(14));
property.add(p);
}
} catch (SQLException err) {
System.out.println("Error in the listAllProperties Session Bean (listAllProperties Method) " + err);
}
return property;
}
public String checkQuery()
{
Connection connection = databaseSessionBean.dbConnection();
List<Properties> property = new ArrayList<Properties>();
try {
//1.Check if customer
String properties = "select * from ESTATEDB.\"Properties\"";
PreparedStatement stmt = connection.prepareStatement(properties);
ResultSet propertyresult = stmt.executeQuery();
if (propertyresult.next())
{
String description = propertyresult.getString(3);
return description;
}
else
{
return "";
}
} catch (SQLException err) {
System.out.println("Error in the listAllProperties Session Bean " + err);
return "";
}
}
private PropertyOwners propertyowner(int id) {
Connection connection = databaseSessionBean.dbConnection();
PropertyOwners po = new PropertyOwners();
try {
//1.Check if customer
String propertyowner = "select * from ESTATEDB.\"Property_Owners\" where ID= " + id + ";";
PreparedStatement stmt = connection.prepareStatement(propertyowner);
ResultSet ownerresult = stmt.executeQuery();
while(ownerresult.next()) {
//PropertyOwners po = new PropertyOwners();
po.setId(ownerresult.getInt(1));
po.setFirstname(ownerresult.getString(2));
po.setLastname(ownerresult.getString(3));
po.setGender(ownerresult.getString(4));
po.setDob(ownerresult.getDate(5));
po.setUsername(ownerresult.getString(6));
po.setPassword(ownerresult.getString(7));
po.setEmail(ownerresult.getString(8));
po.setTelephone(ownerresult.getInt(9));
po.setStreet(ownerresult.getString(10));
po.setCity(ownerresult.getString(11));
po.setPostcode(ownerresult.getString(12));
po.setCountry(ownerresult.getString(13));
}
} catch (SQLException err) {
System.out.println("Error in the listAllProperties Session Bean (propertyowner method) " + err);
}
return po;
}
public boolean CheckConnection()
{
return true;
}
public String[] test()
{
String[] s = new String[]{ "1" , "2" , "3" , "Success!" };
return s;
}
}
Aucun commentaire:
Enregistrer un commentaire