From 7cff621c53d7ab4ea9b7445fcdecfc53d22be59b Mon Sep 17 00:00:00 2001
From: Xoro <44221693+AashimDhawan@users.noreply.github.com>
Date: Mon, 20 Apr 2020 19:31:26 +0530
Subject: [PATCH 1/2] Aashim 1710991013
---
.../ConnectionUtills.java | 35 +++++
.../CookieFilter.java | 71 ++++++++++
.../CreateProductServlet.java | 89 ++++++++++++
.../CreateProductView.jsp | 46 +++++++
.../product list login Web app/DBUtills.java | 129 ++++++++++++++++++
.../DeleteProductServlet.java | 64 +++++++++
.../EditProductServlet.java | 105 ++++++++++++++
.../EditProductView.jsp | 48 +++++++
.../EncodingFilter.java | 45 ++++++
.../product list login Web app/Footer.jsp | 9 ++
.../product list login Web app/Header.jsp | 18 +++
.../HomeServlet.java | 39 ++++++
.../product list login Web app/Homeviews.jsp | 30 ++++
.../JDBCFilter.java | 121 ++++++++++++++++
.../LoginServlet.java | 111 +++++++++++++++
.../product list login Web app/LoginView.jsp | 44 ++++++
.../product list login Web app/Menu.jsp | 14 ++
.../OracleConUtils.java | 37 +++++
.../ProductListServlet.java | 56 ++++++++
.../UserInfoServlet.java | 55 ++++++++
.../UserInfoView.jsp | 23 ++++
.../deleteProductErrorView.jsp | 25 ++++
.../product list login Web app/index.html | 19 +++
.../product list login Web app/myUtills.java | 72 ++++++++++
.../prepare database.txt | 34 +++++
.../product list login Web app/product.java | 43 ++++++
.../productListView.jsp | 47 +++++++
.../useraccount.java | 41 ++++++
.../product list login Web app/web.xml | 29 ++++
29 files changed, 1499 insertions(+)
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/ConnectionUtills.java
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/CookieFilter.java
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/CreateProductServlet.java
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/CreateProductView.jsp
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/DBUtills.java
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/DeleteProductServlet.java
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/EditProductServlet.java
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/EditProductView.jsp
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/EncodingFilter.java
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/Footer.jsp
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/Header.jsp
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/HomeServlet.java
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/Homeviews.jsp
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/JDBCFilter.java
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/LoginServlet.java
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/LoginView.jsp
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/Menu.jsp
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/OracleConUtils.java
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/ProductListServlet.java
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/UserInfoServlet.java
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/UserInfoView.jsp
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/deleteProductErrorView.jsp
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/index.html
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/myUtills.java
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/prepare database.txt
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/product.java
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/productListView.jsp
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/useraccount.java
create mode 100644 Aashim Dhawan 1710991013/product list login Web app/web.xml
diff --git a/Aashim Dhawan 1710991013/product list login Web app/ConnectionUtills.java b/Aashim Dhawan 1710991013/product list login Web app/ConnectionUtills.java
new file mode 100644
index 0000000..45d0654
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/ConnectionUtills.java
@@ -0,0 +1,35 @@
+package javaproject3;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+public class ConnectionUtils {
+
+ public static Connection getConnection()
+ throws ClassNotFoundException, SQLException {
+
+ // Here I using Oracle Database.
+ // (You can change to use another database.)
+ return OracleConnUtils.getOracleConnection();
+
+ // return OracleConnUtils.getOracleConnection();
+ // return MySQLConnUtils.getMySQLConnection();
+ // return SQLServerConnUtils_JTDS.getSQLServerConnection_JTDS();
+ // return SQLServerConnUtils_SQLJDBC.getSQLServerConnection_SQLJDBC();
+ // return PostGresConnUtils.getPostGresConnection();
+ }
+
+ public static void closeQuietly(Connection conn) {
+ try {
+ conn.close();
+ } catch (Exception e) {
+ }
+ }
+
+ public static void rollbackQuietly(Connection conn) {
+ try {
+ conn.rollback();
+ } catch (Exception e) {
+ }
+ }
+}
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/CookieFilter.java b/Aashim Dhawan 1710991013/product list login Web app/CookieFilter.java
new file mode 100644
index 0000000..31d5b5f
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/CookieFilter.java
@@ -0,0 +1,71 @@
+package javaproject3;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.annotation.WebFilter;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+import org.o7planning.simplewebapp.beans.UserAccount;
+import org.o7planning.simplewebapp.utils.DBUtils;
+import org.o7planning.simplewebapp.utils.MyUtils;
+
+@WebFilter(filterName = "cookieFilter", urlPatterns = { "/*" })
+public class CookieFilter implements Filter {
+
+ public CookieFilter() {
+ }
+
+ @Override
+ public void init(FilterConfig fConfig) throws ServletException {
+
+ }
+
+ @Override
+ public void destroy() {
+
+ }
+
+ @Override
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+ throws IOException, ServletException {
+ HttpServletRequest req = (HttpServletRequest) request;
+ HttpSession session = req.getSession();
+
+ UserAccount userInSession = MyUtils.getLoginedUser(session);
+ //
+ if (userInSession != null) {
+ session.setAttribute("COOKIE_CHECKED", "CHECKED");
+ chain.doFilter(request, response);
+ return;
+ }
+
+ // Connection was created in JDBCFilter.
+ Connection conn = MyUtils.getStoredConnection(request);
+
+ // Flag check cookie
+ String checked = (String) session.getAttribute("COOKIE_CHECKED");
+ if (checked == null && conn != null) {
+ String userName = MyUtils.getUserNameInCookie(req);
+ try {
+ UserAccount user = DBUtils.findUser(conn, userName);
+ MyUtils.storeLoginedUser(session, user);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ // Mark checked Cookies.
+ session.setAttribute("COOKIE_CHECKED", "CHECKED");
+ }
+
+ chain.doFilter(request, response);
+ }
+
+}
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/CreateProductServlet.java b/Aashim Dhawan 1710991013/product list login Web app/CreateProductServlet.java
new file mode 100644
index 0000000..8059907
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/CreateProductServlet.java
@@ -0,0 +1,89 @@
+package javaproject3;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.o7planning.simplewebapp.beans.Product;
+import org.o7planning.simplewebapp.utils.DBUtils;
+import org.o7planning.simplewebapp.utils.MyUtils;
+
+@WebServlet(urlPatterns = { "/createProduct" })
+public class CreateProductServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
+ public CreateProductServlet() {
+ super();
+ }
+
+ // Show product creation page.
+ @Override
+ protected void doGet(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+
+ RequestDispatcher dispatcher = request.getServletContext()
+ .getRequestDispatcher("/WEB-INF/views/createProductView.jsp");
+ dispatcher.forward(request, response);
+ }
+
+ // When the user enters the product information, and click Submit.
+ // This method will be called.
+ @Override
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ Connection conn = MyUtils.getStoredConnection(request);
+
+ String code = (String) request.getParameter("code");
+ String name = (String) request.getParameter("name");
+ String priceStr = (String) request.getParameter("price");
+ float price = 0;
+ try {
+ price = Float.parseFloat(priceStr);
+ } catch (Exception e) {
+ }
+ Product product = new Product(code, name, price);
+
+ String errorString = null;
+
+ // Product ID is the string literal [a-zA-Z_0-9]
+ // with at least 1 character
+ String regex = "\\w+";
+
+ if (code == null || !code.matches(regex)) {
+ errorString = "Product Code invalid!";
+ }
+
+ if (errorString == null) {
+ try {
+ DBUtils.insertProduct(conn, product);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ errorString = e.getMessage();
+ }
+ }
+
+ // Store infomation to request attribute, before forward to views.
+ request.setAttribute("errorString", errorString);
+ request.setAttribute("product", product);
+
+ // If error, forward to Edit page.
+ if (errorString != null) {
+ RequestDispatcher dispatcher = request.getServletContext()
+ .getRequestDispatcher("/WEB-INF/views/createProductView.jsp");
+ dispatcher.forward(request, response);
+ }
+ // If everything nice.
+ // Redirect to the product listing page.
+ else {
+ response.sendRedirect(request.getContextPath() + "/productList");
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/CreateProductView.jsp b/Aashim Dhawan 1710991013/product list login Web app/CreateProductView.jsp
new file mode 100644
index 0000000..55d9b55
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/CreateProductView.jsp
@@ -0,0 +1,46 @@
+<%@ page language="java" contentType="text/html; charset=UTF-8"
+ pageEncoding="UTF-8"%>
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
+
+
+
+
+
+ Create Product
+
+
+
+
+
+
+
Create Product
+
+
${errorString}
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/DBUtills.java b/Aashim Dhawan 1710991013/product list login Web app/DBUtills.java
new file mode 100644
index 0000000..ba8a084
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/DBUtills.java
@@ -0,0 +1,129 @@
+package javaproject3;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.o7planning.simplewebapp.beans.Product;
+import org.o7planning.simplewebapp.beans.UserAccount;
+
+public class DBUtils {
+
+ public static UserAccount findUser(Connection conn, //
+ String userName, String password) throws SQLException {
+
+ String sql = "Select a.User_Name, a.Password, a.Gender from User_Account a " //
+ + " where a.User_Name = ? and a.password= ?";
+
+ PreparedStatement pstm = conn.prepareStatement(sql);
+ pstm.setString(1, userName);
+ pstm.setString(2, password);
+ ResultSet rs = pstm.executeQuery();
+
+ if (rs.next()) {
+ String gender = rs.getString("Gender");
+ UserAccount user = new UserAccount();
+ user.setUserName(userName);
+ user.setPassword(password);
+ user.setGender(gender);
+ return user;
+ }
+ return null;
+ }
+
+ public static UserAccount findUser(Connection conn, String userName) throws SQLException {
+
+ String sql = "Select a.User_Name, a.Password, a.Gender from User_Account a "//
+ + " where a.User_Name = ? ";
+
+ PreparedStatement pstm = conn.prepareStatement(sql);
+ pstm.setString(1, userName);
+
+ ResultSet rs = pstm.executeQuery();
+
+ if (rs.next()) {
+ String password = rs.getString("Password");
+ String gender = rs.getString("Gender");
+ UserAccount user = new UserAccount();
+ user.setUserName(userName);
+ user.setPassword(password);
+ user.setGender(gender);
+ return user;
+ }
+ return null;
+ }
+
+ public static List queryProduct(Connection conn) throws SQLException {
+ String sql = "Select a.Code, a.Name, a.Price from Product a ";
+
+ PreparedStatement pstm = conn.prepareStatement(sql);
+
+ ResultSet rs = pstm.executeQuery();
+ List list = new ArrayList();
+ while (rs.next()) {
+ String code = rs.getString("Code");
+ String name = rs.getString("Name");
+ float price = rs.getFloat("Price");
+ Product product = new Product();
+ product.setCode(code);
+ product.setName(name);
+ product.setPrice(price);
+ list.add(product);
+ }
+ return list;
+ }
+
+ public static Product findProduct(Connection conn, String code) throws SQLException {
+ String sql = "Select a.Code, a.Name, a.Price from Product a where a.Code=?";
+
+ PreparedStatement pstm = conn.prepareStatement(sql);
+ pstm.setString(1, code);
+
+ ResultSet rs = pstm.executeQuery();
+
+ while (rs.next()) {
+ String name = rs.getString("Name");
+ float price = rs.getFloat("Price");
+ Product product = new Product(code, name, price);
+ return product;
+ }
+ return null;
+ }
+
+ public static void updateProduct(Connection conn, Product product) throws SQLException {
+ String sql = "Update Product set Name =?, Price=? where Code=? ";
+
+ PreparedStatement pstm = conn.prepareStatement(sql);
+
+ pstm.setString(1, product.getName());
+ pstm.setFloat(2, product.getPrice());
+ pstm.setString(3, product.getCode());
+ pstm.executeUpdate();
+ }
+
+ public static void insertProduct(Connection conn, Product product) throws SQLException {
+ String sql = "Insert into Product(Code, Name,Price) values (?,?,?)";
+
+ PreparedStatement pstm = conn.prepareStatement(sql);
+
+ pstm.setString(1, product.getCode());
+ pstm.setString(2, product.getName());
+ pstm.setFloat(3, product.getPrice());
+
+ pstm.executeUpdate();
+ }
+
+ public static void deleteProduct(Connection conn, String code) throws SQLException {
+ String sql = "Delete From Product where Code= ?";
+
+ PreparedStatement pstm = conn.prepareStatement(sql);
+
+ pstm.setString(1, code);
+
+ pstm.executeUpdate();
+ }
+
+}
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/DeleteProductServlet.java b/Aashim Dhawan 1710991013/product list login Web app/DeleteProductServlet.java
new file mode 100644
index 0000000..f3b1476
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/DeleteProductServlet.java
@@ -0,0 +1,64 @@
+package javaproject3;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.o7planning.simplewebapp.utils.DBUtils;
+import org.o7planning.simplewebapp.utils.MyUtils;
+
+@WebServlet(urlPatterns = { "/deleteProduct" })
+public class DeleteProductServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
+ public DeleteProductServlet() {
+ super();
+ }
+
+ @Override
+ protected void doGet(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ Connection conn = MyUtils.getStoredConnection(request);
+
+ String code = (String) request.getParameter("code");
+
+ String errorString = null;
+
+ try {
+ DBUtils.deleteProduct(conn, code);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ errorString = e.getMessage();
+ }
+
+ // If has an error, redirecte to the error page.
+ if (errorString != null) {
+ // Store the information in the request attribute, before forward to views.
+ request.setAttribute("errorString", errorString);
+ //
+ RequestDispatcher dispatcher = request.getServletContext()
+ .getRequestDispatcher("/WEB-INF/views/deleteProductErrorView.jsp");
+ dispatcher.forward(request, response);
+ }
+ // If everything nice.
+ // Redirect to the product listing page.
+ else {
+ response.sendRedirect(request.getContextPath() + "/productList");
+ }
+
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ doGet(request, response);
+ }
+
+}
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/EditProductServlet.java b/Aashim Dhawan 1710991013/product list login Web app/EditProductServlet.java
new file mode 100644
index 0000000..c4d8fa5
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/EditProductServlet.java
@@ -0,0 +1,105 @@
+package javaproject3;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.o7planning.simplewebapp.beans.Product;
+import org.o7planning.simplewebapp.utils.DBUtils;
+import org.o7planning.simplewebapp.utils.MyUtils;
+
+@WebServlet(urlPatterns = { "/editProduct" })
+public class EditProductServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
+ public EditProductServlet() {
+ super();
+ }
+
+ // Show product edit page.
+ @Override
+ protected void doGet(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ Connection conn = MyUtils.getStoredConnection(request);
+
+ String code = (String) request.getParameter("code");
+
+ Product product = null;
+
+ String errorString = null;
+
+ try {
+ product = DBUtils.findProduct(conn, code);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ errorString = e.getMessage();
+ }
+
+ // If no error.
+ // The product does not exist to edit.
+ // Redirect to productList page.
+ if (errorString != null && product == null) {
+ response.sendRedirect(request.getServletPath() + "/productList");
+ return;
+ }
+
+ // Store errorString in request attribute, before forward to views.
+ request.setAttribute("errorString", errorString);
+ request.setAttribute("product", product);
+
+ RequestDispatcher dispatcher = request.getServletContext()
+ .getRequestDispatcher("/WEB-INF/views/editProductView.jsp");
+ dispatcher.forward(request, response);
+
+ }
+
+ // After the user modifies the product information, and click Submit.
+ // This method will be executed.
+ @Override
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ Connection conn = MyUtils.getStoredConnection(request);
+
+ String code = (String) request.getParameter("code");
+ String name = (String) request.getParameter("name");
+ String priceStr = (String) request.getParameter("price");
+ float price = 0;
+ try {
+ price = Float.parseFloat(priceStr);
+ } catch (Exception e) {
+ }
+ Product product = new Product(code, name, price);
+
+ String errorString = null;
+
+ try {
+ DBUtils.updateProduct(conn, product);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ errorString = e.getMessage();
+ }
+ // Store infomation to request attribute, before forward to views.
+ request.setAttribute("errorString", errorString);
+ request.setAttribute("product", product);
+
+ // If error, forward to Edit page.
+ if (errorString != null) {
+ RequestDispatcher dispatcher = request.getServletContext()
+ .getRequestDispatcher("/WEB-INF/views/editProductView.jsp");
+ dispatcher.forward(request, response);
+ }
+ // If everything nice.
+ // Redirect to the product listing page.
+ else {
+ response.sendRedirect(request.getContextPath() + "/productList");
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/EditProductView.jsp b/Aashim Dhawan 1710991013/product list login Web app/EditProductView.jsp
new file mode 100644
index 0000000..361111f
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/EditProductView.jsp
@@ -0,0 +1,48 @@
+<%@ page language="java" contentType="text/html; charset=UTF-8"
+ pageEncoding="UTF-8"%>
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
+
+
+
+
+ Edit Product
+
+
+
+
+
+
+
Edit Product
+
+
${errorString}
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/EncodingFilter.java b/Aashim Dhawan 1710991013/product list login Web app/EncodingFilter.java
new file mode 100644
index 0000000..b4c7434
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/EncodingFilter.java
@@ -0,0 +1,45 @@
+package javaproject3;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.util.Collection;
+import java.util.Map;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRegistration;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.annotation.WebFilter;
+import javax.servlet.http.HttpServletRequest;
+
+import org.o7planning.simplewebapp.conn.ConnectionUtils;
+import org.o7planning.simplewebapp.utils.MyUtils;
+
+@WebFilter(filterName = "encodingFilter", urlPatterns = { "/*" })
+public class EncodingFilter implements Filter {
+
+ public EncodingFilter() {
+ }
+
+ @Override
+ public void init(FilterConfig fConfig) throws ServletException {
+
+ }
+
+ @Override
+ public void destroy() {
+
+ }
+
+ @Override
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+ throws IOException, ServletException {
+ request.setCharacterEncoding("UTF-8");
+
+ chain.doFilter(request, response);
+ }
+
+}
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/Footer.jsp b/Aashim Dhawan 1710991013/product list login Web app/Footer.jsp
new file mode 100644
index 0000000..26a10ac
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/Footer.jsp
@@ -0,0 +1,9 @@
+<%@ page language="java" contentType="text/html; charset=UTF-8"
+ pageEncoding="UTF-8"%>
+
+
+
+ @Copyright o7planning.org
+
+
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/Header.jsp b/Aashim Dhawan 1710991013/product list login Web app/Header.jsp
new file mode 100644
index 0000000..00daac1
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/Header.jsp
@@ -0,0 +1,18 @@
+<%@ page language="java" contentType="text/html; charset=UTF-8"
+ pageEncoding="UTF-8"%>
+
+
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/HomeServlet.java b/Aashim Dhawan 1710991013/product list login Web app/HomeServlet.java
new file mode 100644
index 0000000..119c1e2
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/HomeServlet.java
@@ -0,0 +1,39 @@
+package javaproject3;
+
+import java.io.IOException;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+@WebServlet(urlPatterns = { "/home"})
+public class HomeServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
+ public HomeServlet() {
+ super();
+ }
+
+ @Override
+ protected void doGet(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+
+
+ // Forward to /WEB-INF/views/homeView.jsp
+ // (Users can not access directly into JSP pages placed in WEB-INF)
+ RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/WEB-INF/views/homeView.jsp");
+
+ dispatcher.forward(request, response);
+
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ doGet(request, response);
+ }
+
+}
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/Homeviews.jsp b/Aashim Dhawan 1710991013/product list login Web app/Homeviews.jsp
new file mode 100644
index 0000000..dbf2273
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/Homeviews.jsp
@@ -0,0 +1,30 @@
+<%@ page language="java" contentType="text/html; charset=UTF-8"
+ pageEncoding="UTF-8"%>
+
+
+
+
+ Home Page
+
+
+
+
+
+
+
Home Page
+
+ This is demo Simple web application using jsp,servlet & Jdbc.
+ It includes the following functions:
+
+
Login
+
Storing user information in cookies
+
Product List
+
Create Product
+
Edit Product
+
Delete Product
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/JDBCFilter.java b/Aashim Dhawan 1710991013/product list login Web app/JDBCFilter.java
new file mode 100644
index 0000000..51ee84e
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/JDBCFilter.java
@@ -0,0 +1,121 @@
+package javaproject3;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.util.Collection;
+import java.util.Map;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRegistration;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.annotation.WebFilter;
+import javax.servlet.http.HttpServletRequest;
+
+import org.o7planning.simplewebapp.conn.ConnectionUtils;
+import org.o7planning.simplewebapp.utils.MyUtils;
+
+@WebFilter(filterName = "jdbcFilter", urlPatterns = { "/*" })
+public class JDBCFilter implements Filter {
+
+ public JDBCFilter() {
+ }
+
+ @Override
+ public void init(FilterConfig fConfig) throws ServletException {
+
+ }
+
+ @Override
+ public void destroy() {
+
+ }
+
+ // Check the target of the request is a servlet?
+ private boolean needJDBC(HttpServletRequest request) {
+ System.out.println("JDBC Filter");
+ //
+ // Servlet Url-pattern: /spath/*
+ //
+ // => /spath
+ String servletPath = request.getServletPath();
+ // => /abc/mnp
+ String pathInfo = request.getPathInfo();
+
+ String urlPattern = servletPath;
+
+ if (pathInfo != null) {
+ // => /spath/*
+ urlPattern = servletPath + "/*";
+ }
+
+ // Key: servletName.
+ // Value: ServletRegistration
+ Map servletRegistrations = request.getServletContext()
+ .getServletRegistrations();
+
+ // Collection of all servlet in your Webapp.
+ Collection extends ServletRegistration> values = servletRegistrations.values();
+ for (ServletRegistration sr : values) {
+ Collection mappings = sr.getMappings();
+ if (mappings.contains(urlPattern)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+ throws IOException, ServletException {
+
+ HttpServletRequest req = (HttpServletRequest) request;
+
+ // Only open connections for the special requests.
+ // (For example, the path to the servlet, JSP, ..)
+ //
+ // Avoid open connection for commons request.
+ // (For example: image, css, javascript,... )
+ //
+ if (this.needJDBC(req)) {
+
+ System.out.println("Open Connection for: " + req.getServletPath());
+
+ Connection conn = null;
+ try {
+ // Create a Connection.
+ conn = ConnectionUtils.getConnection();
+ // Set outo commit to false.
+ conn.setAutoCommit(false);
+
+ // Store Connection object in attribute of request.
+ MyUtils.storeConnection(request, conn);
+
+ // Allow request to go forward
+ // (Go to the next filter or target)
+ chain.doFilter(request, response);
+
+ // Invoke the commit() method to complete the transaction with the DB.
+ conn.commit();
+ } catch (Exception e) {
+ e.printStackTrace();
+ ConnectionUtils.rollbackQuietly(conn);
+ throw new ServletException();
+ } finally {
+ ConnectionUtils.closeQuietly(conn);
+ }
+ }
+ // With commons requests (images, css, html, ..)
+ // No need to open the connection.
+ else {
+ // Allow request to go forward
+ // (Go to the next filter or target)
+ chain.doFilter(request, response);
+ }
+
+ }
+
+}
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/LoginServlet.java b/Aashim Dhawan 1710991013/product list login Web app/LoginServlet.java
new file mode 100644
index 0000000..9f47846
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/LoginServlet.java
@@ -0,0 +1,111 @@
+package javaproject3;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import org.o7planning.simplewebapp.beans.UserAccount;
+import org.o7planning.simplewebapp.utils.DBUtils;
+import org.o7planning.simplewebapp.utils.MyUtils;
+
+@WebServlet(urlPatterns = { "/login" })
+public class LoginServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
+ public LoginServlet() {
+ super();
+ }
+
+ // Show Login page.
+ @Override
+ protected void doGet(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+
+ // Forward to /WEB-INF/views/loginView.jsp
+ // (Users can not access directly into JSP pages placed in WEB-INF)
+ RequestDispatcher dispatcher //
+ = this.getServletContext().getRequestDispatcher("/WEB-INF/views/loginView.jsp");
+
+ dispatcher.forward(request, response);
+
+ }
+
+ // When the user enters userName & password, and click Submit.
+ // This method will be executed.
+ @Override
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ String userName = request.getParameter("userName");
+ String password = request.getParameter("password");
+ String rememberMeStr = request.getParameter("rememberMe");
+ boolean remember = "Y".equals(rememberMeStr);
+
+ UserAccount user = null;
+ boolean hasError = false;
+ String errorString = null;
+
+ if (userName == null || password == null || userName.length() == 0 || password.length() == 0) {
+ hasError = true;
+ errorString = "Required username and password!";
+ } else {
+ Connection conn = MyUtils.getStoredConnection(request);
+ try {
+ // Find the user in the DB.
+ user = DBUtils.findUser(conn, userName, password);
+
+ if (user == null) {
+ hasError = true;
+ errorString = "User Name or password invalid";
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ hasError = true;
+ errorString = e.getMessage();
+ }
+ }
+ // If error, forward to /WEB-INF/views/login.jsp
+ if (hasError) {
+ user = new UserAccount();
+ user.setUserName(userName);
+ user.setPassword(password);
+
+ // Store information in request attribute, before forward.
+ request.setAttribute("errorString", errorString);
+ request.setAttribute("user", user);
+
+ // Forward to /WEB-INF/views/login.jsp
+ RequestDispatcher dispatcher //
+ = this.getServletContext().getRequestDispatcher("/WEB-INF/views/loginView.jsp");
+
+ dispatcher.forward(request, response);
+ }
+ // If no error
+ // Store user information in Session
+ // And redirect to userInfo page.
+ else {
+ HttpSession session = request.getSession();
+ MyUtils.storeLoginedUser(session, user);
+
+ // If user checked "Remember me".
+ if (remember) {
+ MyUtils.storeUserCookie(response, user);
+ }
+ // Else delete cookie.
+ else {
+ MyUtils.deleteUserCookie(response);
+ }
+
+ // Redirect to userInfo page.
+ response.sendRedirect(request.getContextPath() + "/userInfo");
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/LoginView.jsp b/Aashim Dhawan 1710991013/product list login Web app/LoginView.jsp
new file mode 100644
index 0000000..7772d41
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/LoginView.jsp
@@ -0,0 +1,44 @@
+<%@ page language="java" contentType="text/html; charset=UTF-8"
+ pageEncoding="UTF-8"%>
+
+
+
+
+ Login
+
+
+
+
+
+
Login Page
+
${errorString}
+
+
+
+
+
User Name: tom, password: tom001 or jerry/jerry001
+
+
+
+
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/Menu.jsp b/Aashim Dhawan 1710991013/product list login Web app/Menu.jsp
new file mode 100644
index 0000000..940ff6c
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/Menu.jsp
@@ -0,0 +1,14 @@
+<%@ page language="java" contentType="text/html; charset=UTF-8"
+ pageEncoding="UTF-8"%>
+
+
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/OracleConUtils.java b/Aashim Dhawan 1710991013/product list login Web app/OracleConUtils.java
new file mode 100644
index 0000000..5bfdeda
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/OracleConUtils.java
@@ -0,0 +1,37 @@
+package javaproject3;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+public class OracleConnUtils {
+
+ public static Connection getOracleConnection()
+ throws ClassNotFoundException, SQLException {
+
+ // Note: Change the connection parameters accordingly.
+ String hostName = "localhost";
+ String sid = "db12c";
+ String userName = "mytest";
+ String password = "12345";
+
+ return getOracleConnection(hostName, sid, userName, password);
+ }
+
+ public static Connection getOracleConnection(String hostName, String sid,
+ String userName, String password) throws ClassNotFoundException,
+ SQLException {
+
+ Class.forName("oracle.jdbc.driver.OracleDriver");
+
+ // URL Connection for Oracle
+ // Example:
+ // jdbc:oracle:thin:@localhost:1521:db11g
+ // jdbc:oracle:thin:@//HOSTNAME:PORT/SERVICENAME
+ String connectionURL = "jdbc:oracle:thin:@" + hostName + ":1521:" + sid;
+
+ Connection conn = DriverManager.getConnection(connectionURL, userName,
+ password);
+ return conn;
+ }
+}
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/ProductListServlet.java b/Aashim Dhawan 1710991013/product list login Web app/ProductListServlet.java
new file mode 100644
index 0000000..eb09351
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/ProductListServlet.java
@@ -0,0 +1,56 @@
+package javaproject3;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.List;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.o7planning.simplewebapp.beans.Product;
+import org.o7planning.simplewebapp.utils.DBUtils;
+import org.o7planning.simplewebapp.utils.MyUtils;
+
+@WebServlet(urlPatterns = { "/productList" })
+public class ProductListServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
+ public ProductListServlet() {
+ super();
+ }
+
+ @Override
+ protected void doGet(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ Connection conn = MyUtils.getStoredConnection(request);
+
+ String errorString = null;
+ List list = null;
+ try {
+ list = DBUtils.queryProduct(conn);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ errorString = e.getMessage();
+ }
+ // Store info in request attribute, before forward to views
+ request.setAttribute("errorString", errorString);
+ request.setAttribute("productList", list);
+
+ // Forward to /WEB-INF/views/productListView.jsp
+ RequestDispatcher dispatcher = request.getServletContext()
+ .getRequestDispatcher("/WEB-INF/views/productListView.jsp");
+ dispatcher.forward(request, response);
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ doGet(request, response);
+ }
+
+}
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/UserInfoServlet.java b/Aashim Dhawan 1710991013/product list login Web app/UserInfoServlet.java
new file mode 100644
index 0000000..b992a35
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/UserInfoServlet.java
@@ -0,0 +1,55 @@
+package javaproject3;
+
+import java.io.IOException;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import org.o7planning.simplewebapp.beans.UserAccount;
+import org.o7planning.simplewebapp.utils.MyUtils;
+
+@WebServlet(urlPatterns = { "/userInfo" })
+public class UserInfoServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
+ public UserInfoServlet() {
+ super();
+ }
+
+ @Override
+ protected void doGet(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ HttpSession session = request.getSession();
+
+ // Check User has logged on
+ UserAccount loginedUser = MyUtils.getLoginedUser(session);
+
+ // Not logged in
+ if (loginedUser == null) {
+ // Redirect to login page.
+ response.sendRedirect(request.getContextPath() + "/login");
+ return;
+ }
+ // Store info to the request attribute before forwarding.
+ request.setAttribute("user", loginedUser);
+
+ // If the user has logged in, then forward to the page
+ // /WEB-INF/views/userInfoView.jsp
+ RequestDispatcher dispatcher //
+ = this.getServletContext().getRequestDispatcher("/WEB-INF/views/userInfoView.jsp");
+ dispatcher.forward(request, response);
+
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ doGet(request, response);
+ }
+
+}
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/UserInfoView.jsp b/Aashim Dhawan 1710991013/product list login Web app/UserInfoView.jsp
new file mode 100644
index 0000000..e4174c0
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/UserInfoView.jsp
@@ -0,0 +1,23 @@
+<%@ page language="java" contentType="text/html; charset=UTF-8"
+ pageEncoding="UTF-8"%>
+
+
+
+
+ User Info
+
+
+
+
+
+
+
Hello: ${user.userName}
+
+ User Name: ${user.userName}
+
+ Gender: ${user.gender }
+
+
+
+
+
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/deleteProductErrorView.jsp b/Aashim Dhawan 1710991013/product list login Web app/deleteProductErrorView.jsp
new file mode 100644
index 0000000..9f51365
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/deleteProductErrorView.jsp
@@ -0,0 +1,25 @@
+<%@ page language="java" contentType="text/html; charset=UTF-8"
+ pageEncoding="UTF-8"%>
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
+
+
+
+
+
+ Delete Product
+
+
+
+
+
+
+
+
Delete Product
+
+
${errorString}
+ Product List
+
+
+
+
+
\ No newline at end of file
diff --git a/Aashim Dhawan 1710991013/product list login Web app/index.html b/Aashim Dhawan 1710991013/product list login Web app/index.html
new file mode 100644
index 0000000..37487d6
--- /dev/null
+++ b/Aashim Dhawan 1710991013/product list login Web app/index.html
@@ -0,0 +1,19 @@
+
+
+
+
+ Simple Web Application
+
+
+
+
+