Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated AddBookServlet and RemoveBookServlet; improved login handling #169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
Expand All @@ -27,6 +26,31 @@
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
Expand Down
88 changes: 44 additions & 44 deletions .project
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>onlinebookstore</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
<name>onlinebookstore</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
<filteredResources>
<filter>
<id>1665573956898</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>

<filteredResources>
<filter>
<id>1665573956898</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
40 changes: 19 additions & 21 deletions src/main/java/servlets/AddBookServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import com.bittercode.util.StoreUtil;

public class AddBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
BookService bookService = new BookServiceImpl();

public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
PrintWriter pw = res.getWriter();
res.setContentType(BookStoreConstants.CONTENT_TYPE_TEXT_HTML);

Expand All @@ -37,52 +39,48 @@ public void service(HttpServletRequest req, HttpServletResponse res) throws IOEx
rd.include(req, res);
StoreUtil.setActiveTab(pw, "addbook");
pw.println("<div class='container my-2'>");
if(bName == null || bName.isBlank()) {
//render the add book form;

if (bName == null || bName.trim().isEmpty()) { // Use trim() for better handling of blank spaces
showAddBookForm(pw);
return;
} //else process the add book


}

try {
String uniqueID = UUID.randomUUID().toString();
String bCode = uniqueID;
String bAuthor = req.getParameter(BooksDBConstants.COLUMN_AUTHOR);
double bPrice = Integer.parseInt(req.getParameter(BooksDBConstants.COLUMN_PRICE));
double bPrice = Double.parseDouble(req.getParameter(BooksDBConstants.COLUMN_PRICE));
int bQty = Integer.parseInt(req.getParameter(BooksDBConstants.COLUMN_QUANTITY));

Book book = new Book(bCode, bName, bAuthor, bPrice, bQty);
String message = bookService.addBook(book);

if ("SUCCESS".equalsIgnoreCase(message)) {
pw.println(
"<table class=\"tab\"><tr><td>Book Detail Updated Successfully!<br/>Add More Books</td></tr></table>");
pw.println("<table class=\"tab\"><tr><td>Book Detail Updated Successfully!<br/>Add More Books</td></tr></table>");
} else {
pw.println("<table class=\"tab\"><tr><td>Failed to Add Books! Fill up CareFully</td></tr></table>");
//rd.include(req, res);
pw.println("<table class=\"tab\"><tr><td>Failed to Add Books! Fill up Carefully</td></tr></table>");
}
} catch (NumberFormatException e) {
pw.println("<table class=\"tab\"><tr><td>Invalid number format. Please check your input.</td></tr></table>");
} catch (Exception e) {
e.printStackTrace();
pw.println("<table class=\"tab\"><tr><td>Failed to Add Books! Fill up CareFully</td></tr></table>");
e.printStackTrace(); // Optionally log this to a logging framework
pw.println("<table class=\"tab\"><tr><td>Failed to Add Books! Please try again later.</td></tr></table>");
}
}

private static void showAddBookForm(PrintWriter pw) {
String form = "<table class=\"tab my-5\" style=\"width:40%;\">\r\n"
+ " <tr>\r\n"
+ " <td>\r\n"
+ " <form action=\"addbook\" method=\"post\">\r\n"
+ " <!-- <label for=\"bookCode\">Book Code : </label><input type=\"text\" name=\"barcode\" id=\"bookCode\" placeholder=\"Enter Book Code\" required><br/> -->\r\n"
+ " <label for=\"bookName\">Book Name : </label> <input type=\"text\" name=\"name\" id=\"bookName\" placeholder=\"Enter Book's name\" required><br/>\r\n"
+ " <label for=\"bookAuthor\">Book Author : </label><input type=\"text\" name=\"author\" id=\"bookAuthor\" placeholder=\"Enter Author's Name\" required><br/>\r\n"
+ " <label for=\"bookPrice\">Book Price : </label><input type=\"number\" name=\"price\" placeholder=\"Enter the Price\" required><br/>\r\n"
+ " <label for=\"bookPrice\">Book Price : </label><input type=\"number\" name=\"price\" placeholder=\"Enter the Price\" required step=\"0.01\"><br/>\r\n" // Added step for decimal support
+ " <label for=\"bookQuantity\">Book Qnty : </label><input type=\"number\" name=\"quantity\" id=\"bookQuantity\" placeholder=\"Enter the quantity\" required><br/>\r\n"
+ " <input class=\"btn btn-success my-2\" type=\"submit\" value=\" Add Book \">\r\n"
+ " <input class=\"btn btn-success my-2\" type=\"submit\" value=\"Add Book\">\r\n"
+ " </form>\r\n"
+ " </td>\r\n"
+ " </tr> \r\n"
+ " <!-- <tr>\r\n"
+ " <td><a href=\"index.html\">Go Back To Home Page</a></td>\r\n"
+ " </tr> -->\r\n"
+ " </tr>\r\n"
+ " </table>";
pw.println(form);
}
Expand Down
53 changes: 34 additions & 19 deletions src/main/java/servlets/CustomerLoginServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,55 @@
import com.bittercode.service.impl.UserServiceImpl;

public class CustomerLoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

UserService authService = new UserServiceImpl();

public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
res.setContentType(BookStoreConstants.CONTENT_TYPE_TEXT_HTML + "; charset=UTF-8");
PrintWriter pw = res.getWriter();
res.setContentType(BookStoreConstants.CONTENT_TYPE_TEXT_HTML);

String uName = req.getParameter(UsersDBConstants.COLUMN_USERNAME);
String pWord = req.getParameter(UsersDBConstants.COLUMN_PASSWORD);
User user = authService.login(UserRole.CUSTOMER, uName, pWord, req.getSession());
User user = null;

try {
user = authService.login(UserRole.CUSTOMER, uName, pWord, req.getSession());

if (user != null) {

RequestDispatcher rd = req.getRequestDispatcher("CustomerHome.html");
rd.include(req, res);
pw.println(" <div id=\"topmid\"><h1>Welcome to Online <br>Book Store</h1></div>\r\n"
+ " <br>\r\n"
+ " <table class=\"tab\">\r\n"
+ " <tr>\r\n"
+ " <td><p>Welcome "+user.getFirstName()+", Happy Learning !!</p></td>\r\n"
+ " </tr>\r\n"
+ " </table>");

pw.println("<div id=\"topmid\"><h1>Welcome to Online <br>Book Store</h1></div>\r\n"
+ "<br>\r\n"
+ "<table class=\"tab\">\r\n"
+ "<tr>\r\n"
+ "<td><p>Welcome " + escapeHtml(user.getFirstName()) + ", Happy Learning !!</p></td>\r\n"
+ "</tr>\r\n"
+ "</table>");
} else {

RequestDispatcher rd = req.getRequestDispatcher("CustomerLogin.html");
rd.include(req, res);
pw.println("<table class=\"tab\"><tr><td>Incorrect UserName or PassWord</td></tr></table>");
handleLoginFailure(req, res, pw);
}

} catch (Exception e) {
e.printStackTrace();
e.printStackTrace(); // Log this to a logging framework
pw.println("<table class=\"tab\"><tr><td>Something went wrong. Please try again later.</td></tr></table>");
}
}

}
private void handleLoginFailure(HttpServletRequest req, HttpServletResponse res, PrintWriter pw) throws ServletException, IOException {
RequestDispatcher rd = req.getRequestDispatcher("CustomerLogin.html");
rd.include(req, res);
pw.println("<table class=\"tab\"><tr><td>Incorrect Username or Password</td></tr></table>");
}

private String escapeHtml(String input) {
if (input == null) {
return "";
}
return input.replace("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;")
.replace("\"", "&quot;")
.replace("'", "&#x27;");
}
}
43 changes: 29 additions & 14 deletions src/main/java/servlets/RemoveBookServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,32 @@
import com.bittercode.util.StoreUtil;

public class RemoveBookServlet extends HttpServlet {

private static final long serialVersionUID = 1L;
BookService bookService = new BookServiceImpl();

public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
// Show the remove book form for GET requests
PrintWriter pw = res.getWriter();
res.setContentType("text/html");
res.setContentType("text/html; charset=UTF-8");

if (!StoreUtil.isLoggedIn(UserRole.SELLER, req.getSession())) {
RequestDispatcher rd = req.getRequestDispatcher("SellerLogin.html");
rd.include(req, res);
pw.println("<table class=\"tab\"><tr><td>Please Login First to Continue!!</td></tr></table>");
return;
}

pw.println("<div class='container'>");
showRemoveBookForm(pw);
pw.println("</div>");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
PrintWriter pw = res.getWriter();
res.setContentType("text/html; charset=UTF-8");

if (!StoreUtil.isLoggedIn(UserRole.SELLER, req.getSession())) {
RequestDispatcher rd = req.getRequestDispatcher("SellerLogin.html");
rd.include(req, res);
Expand All @@ -35,26 +55,23 @@ public void service(HttpServletRequest req, HttpServletResponse res) throws IOEx
rd.include(req, res);
StoreUtil.setActiveTab(pw, "removebook");
pw.println("<div class='container'>");
if (bookId == null || bookId.isBlank()) {
// render the remove book form;

if (bookId == null || bookId.trim().isEmpty()) {
// Render the remove book form again if no bookId is provided
showRemoveBookForm(pw);
return;
} // else continue
}

String responseCode = bookService.deleteBookById(bookId.trim());
if (ResponseCode.SUCCESS.name().equalsIgnoreCase(responseCode)) {
pw.println("<table class=\"tab my-5\"><tr><td>Book Removed Successfully</td></tr></table>");
pw.println(
"<table class=\"tab\"><tr><td><a href=\"removebook\">Remove more Books</a></td></tr></table>");

} else {
pw.println("<table class=\"tab my-5\"><tr><td>Book Not Available In The Store</td></tr></table>");
pw.println(
"<table class=\"tab\"><tr><td><a href=\"removebook\">Remove more Books</a></td></tr></table>");
}
pw.println("<table class=\"tab\"><tr><td><a href=\"removebook\">Remove more Books</a></td></tr></table>");
pw.println("</div>");
} catch (Exception e) {
e.printStackTrace();
e.printStackTrace(); // Optionally log this to a logging framework
pw.println("<table class=\"tab\"><tr><td>Failed to Remove Books! Try Again</td></tr></table>");
}
}
Expand All @@ -69,10 +86,8 @@ private static void showRemoveBookForm(PrintWriter pw) {
+ " <input class=\"btn btn-danger my-2\" type=\"submit\" value=\"Remove Book\">\r\n"
+ " </td>\r\n"
+ " </tr>\r\n"
+ "\r\n"
+ " </table>\r\n"
+ " </form>";
pw.println(form);
}

}