diff --git a/.classpath b/.classpath
new file mode 100644
index 0000000..942da55
--- /dev/null
+++ b/.classpath
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..84c048a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/build/
diff --git a/.project b/.project
new file mode 100644
index 0000000..f970b92
--- /dev/null
+++ b/.project
@@ -0,0 +1,31 @@
+
+
+ SimpleWebApp
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+ org.eclipse.wst.common.project.facet.core.builder
+
+
+
+
+ org.eclipse.wst.validation.validationbuilder
+
+
+
+
+
+ org.eclipse.jem.workbench.JavaEMFNature
+ org.eclipse.wst.common.modulecore.ModuleCoreNature
+ org.eclipse.wst.common.project.facet.core.nature
+ org.eclipse.jdt.core.javanature
+ org.eclipse.wst.jsdt.core.jsNature
+
+
diff --git a/.settings/.jsdtscope b/.settings/.jsdtscope
new file mode 100644
index 0000000..92e666d
--- /dev/null
+++ b/.settings/.jsdtscope
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..0c68a61
--- /dev/null
+++ b/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,7 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
+org.eclipse.jdt.core.compiler.compliance=1.8
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.8
diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component
new file mode 100644
index 0000000..2e5e846
--- /dev/null
+++ b/.settings/org.eclipse.wst.common.component
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
diff --git a/.settings/org.eclipse.wst.common.project.facet.core.xml b/.settings/org.eclipse.wst.common.project.facet.core.xml
new file mode 100644
index 0000000..03e6f9a
--- /dev/null
+++ b/.settings/org.eclipse.wst.common.project.facet.core.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.container b/.settings/org.eclipse.wst.jsdt.ui.superType.container
new file mode 100644
index 0000000..3bd5d0a
--- /dev/null
+++ b/.settings/org.eclipse.wst.jsdt.ui.superType.container
@@ -0,0 +1 @@
+org.eclipse.wst.jsdt.launching.baseBrowserLibrary
\ No newline at end of file
diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.name b/.settings/org.eclipse.wst.jsdt.ui.superType.name
new file mode 100644
index 0000000..05bd71b
--- /dev/null
+++ b/.settings/org.eclipse.wst.jsdt.ui.superType.name
@@ -0,0 +1 @@
+Window
\ No newline at end of file
diff --git a/WebContent/META-INF/MANIFEST.MF b/WebContent/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..5e94951
--- /dev/null
+++ b/WebContent/META-INF/MANIFEST.MF
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Class-Path:
+
diff --git a/WebContent/WEB-INF/lib/javax.servlet.jsp.jstl-1.2.4.jar b/WebContent/WEB-INF/lib/javax.servlet.jsp.jstl-1.2.4.jar
new file mode 100644
index 0000000..890d50d
Binary files /dev/null and b/WebContent/WEB-INF/lib/javax.servlet.jsp.jstl-1.2.4.jar differ
diff --git a/WebContent/WEB-INF/lib/javax.servlet.jsp.jstl-api-1.2.1.jar b/WebContent/WEB-INF/lib/javax.servlet.jsp.jstl-api-1.2.1.jar
new file mode 100644
index 0000000..4b3b433
Binary files /dev/null and b/WebContent/WEB-INF/lib/javax.servlet.jsp.jstl-api-1.2.1.jar differ
diff --git a/WebContent/WEB-INF/lib/mysql-connector-java-5.1.40-bin.jar b/WebContent/WEB-INF/lib/mysql-connector-java-5.1.40-bin.jar
new file mode 100644
index 0000000..60bef5c
Binary files /dev/null and b/WebContent/WEB-INF/lib/mysql-connector-java-5.1.40-bin.jar differ
diff --git a/WebContent/WEB-INF/tags/wrapper.tag b/WebContent/WEB-INF/tags/wrapper.tag
new file mode 100644
index 0000000..69c0489
--- /dev/null
+++ b/WebContent/WEB-INF/tags/wrapper.tag
@@ -0,0 +1,54 @@
+<%@tag description="Page Wrapper Tag" pageEncoding="UTF-8"%>
+<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+<%@attribute name="title" required="true" %>
+
+
+
+
+
+ ${pageTitle}
+
+ " integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">
+
+ " rel="stylesheet">
+
+
+
+
+ My Simple JSP Web Application
+
+
+
+
+
+
+
+
+
+ integrity="sha384-BLiI7JTZm+JWlgKa0M0kGRpJbF2J8q+qreVrKBC47e3K6BW78kGLrCkeRX6I9RoK" crossorigin="anonymous">
+
+
\ No newline at end of file
diff --git a/WebContent/WEB-INF/views/createProductView.jsp b/WebContent/WEB-INF/views/createProductView.jsp
new file mode 100644
index 0000000..aeb7de1
--- /dev/null
+++ b/WebContent/WEB-INF/views/createProductView.jsp
@@ -0,0 +1,30 @@
+<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
+<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
+
+
+
+ ${errorString}
+
+
+
\ No newline at end of file
diff --git a/WebContent/WEB-INF/views/deleteProductErrorView.jsp b/WebContent/WEB-INF/views/deleteProductErrorView.jsp
new file mode 100644
index 0000000..2f6ad73
--- /dev/null
+++ b/WebContent/WEB-INF/views/deleteProductErrorView.jsp
@@ -0,0 +1,7 @@
+<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
+<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
+
+
+ ${errorString}
+ Product List
+
\ No newline at end of file
diff --git a/WebContent/WEB-INF/views/editProductView.jsp b/WebContent/WEB-INF/views/editProductView.jsp
new file mode 100644
index 0000000..bd180fd
--- /dev/null
+++ b/WebContent/WEB-INF/views/editProductView.jsp
@@ -0,0 +1,28 @@
+<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
+<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
+<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
+
+
+
+ ${errorString}
+
+
+
+
+
\ No newline at end of file
diff --git a/WebContent/WEB-INF/views/homeView.jsp b/WebContent/WEB-INF/views/homeView.jsp
new file mode 100644
index 0000000..9da41d0
--- /dev/null
+++ b/WebContent/WEB-INF/views/homeView.jsp
@@ -0,0 +1,17 @@
+<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
+<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
+
+
+ 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/WebContent/WEB-INF/views/loginView.jsp b/WebContent/WEB-INF/views/loginView.jsp
new file mode 100644
index 0000000..a991361
--- /dev/null
+++ b/WebContent/WEB-INF/views/loginView.jsp
@@ -0,0 +1,35 @@
+<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
+<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
+
+
+ ${errorString}
+
+
+
+ User Name: lani, password: lani001 or tom/tom001 or jerry/jerry001
+
\ No newline at end of file
diff --git a/WebContent/WEB-INF/views/productListView.jsp b/WebContent/WEB-INF/views/productListView.jsp
new file mode 100644
index 0000000..b9306fc
--- /dev/null
+++ b/WebContent/WEB-INF/views/productListView.jsp
@@ -0,0 +1,34 @@
+<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
+<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
+
+
+
+ ${errorString}
+
+
+
+ Code
+ Name
+ Price
+ Edit
+ Delete
+
+
+
+ ${product.code}
+ ${product.name}
+ ${product.price}
+
+ Edit
+
+
+ Delete
+
+
+
+
+
+ Create Product
+
+
\ No newline at end of file
diff --git a/WebContent/WEB-INF/views/userInfoView.jsp b/WebContent/WEB-INF/views/userInfoView.jsp
new file mode 100644
index 0000000..20ec736
--- /dev/null
+++ b/WebContent/WEB-INF/views/userInfoView.jsp
@@ -0,0 +1,9 @@
+<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
+<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
+
+
+ User Name: ${user.userName}
+
+ Gender: ${user.gender}
+
+
\ No newline at end of file
diff --git a/WebContent/WEB-INF/web.xml b/WebContent/WEB-INF/web.xml
new file mode 100644
index 0000000..fab2922
--- /dev/null
+++ b/WebContent/WEB-INF/web.xml
@@ -0,0 +1,16 @@
+
+
+ SimpleWebApp
+
+ home
+ index.html
+
+
+ jdbcFilter
+ /*
+
+
+ cookieFilter
+ /*
+
+
\ No newline at end of file
diff --git a/WebContent/styles.css b/WebContent/styles.css
new file mode 100644
index 0000000..388aa3a
--- /dev/null
+++ b/WebContent/styles.css
@@ -0,0 +1,39 @@
+body {
+ font-size: 16px;
+}
+
+h2 {
+ margin-bottom: 20px;
+}
+
+.container {
+ max-width: 700px;
+ padding: 0 20px;
+}
+
+.site-name {
+ margin: 0;
+ padding: 5px 0;
+}
+
+.navbar {
+ padding: 20px 40px;
+ font-size: .9rem;
+
+}
+.nav-link {
+ text-transform: uppercase;
+}
+
+.header {
+ padding-top: 2rem;
+}
+
+.btn-submit {
+ margin-right: 20px;
+}
+
+.login-info {
+ margin-top: 20px;
+ color: #AAACAC;
+}
\ No newline at end of file
diff --git a/src/com/lanihuang/simplewebapp/beans/Product.java b/src/com/lanihuang/simplewebapp/beans/Product.java
new file mode 100644
index 0000000..6f82c46
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/beans/Product.java
@@ -0,0 +1,43 @@
+package com.lanihuang.simplewebapp.beans;
+
+public class Product {
+
+ private String code;
+ private String name;
+ private float price;
+
+ public Product() {
+
+ }
+
+ public Product(String code, String name, float price) {
+ this.code = code;
+ this.name = name;
+ this.price = price;
+ }
+
+ public String getCode() {
+ return code;
+ }
+
+ public void setCode(String code) {
+ this.code = code;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public float getPrice() {
+ return price;
+ }
+
+ public void setPrice(float price) {
+ this.price = price;
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/lanihuang/simplewebapp/beans/UserAccount.java b/src/com/lanihuang/simplewebapp/beans/UserAccount.java
new file mode 100644
index 0000000..142f618
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/beans/UserAccount.java
@@ -0,0 +1,41 @@
+package com.lanihuang.simplewebapp.beans;
+
+public class UserAccount {
+
+ public static final String GENDER_MALE = "M";
+ public static final String GENDER_FEMALE = "F";
+
+ private String userName;
+ private String gender;
+ private String password;
+
+
+ public UserAccount() {
+
+ }
+
+ public String getUserName() {
+ return userName;
+ }
+
+ public void setUserName(String userName) {
+ this.userName = userName;
+ }
+
+ public String getGender() {
+ return gender;
+ }
+
+ public void setGender(String gender) {
+ this.gender = gender;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/lanihuang/simplewebapp/conn/ConnectionUtils.java b/src/com/lanihuang/simplewebapp/conn/ConnectionUtils.java
new file mode 100644
index 0000000..eae2bdc
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/conn/ConnectionUtils.java
@@ -0,0 +1,32 @@
+package com.lanihuang.simplewebapp.conn;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+public class ConnectionUtils {
+
+ public static Connection getConnection()
+ throws ClassNotFoundException, SQLException {
+
+ // Here I using Oracle Database.
+// return OracleConnUtils.getOracleConnection();
+
+ return MySQLConnUtils.getMySQLConnection();
+ // return SQLServerConnUtils_JTDS.getSQLServerConnection_JTDS();
+ // return SQLServerConnUtils_SQLJDBC.getSQLServerConnection_SQLJDBC();
+ }
+
+ 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/src/com/lanihuang/simplewebapp/conn/MySQLConnUtils.java b/src/com/lanihuang/simplewebapp/conn/MySQLConnUtils.java
new file mode 100644
index 0000000..5422811
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/conn/MySQLConnUtils.java
@@ -0,0 +1,36 @@
+package com.lanihuang.simplewebapp.conn;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+public class MySQLConnUtils {
+
+ public static Connection getMySQLConnection()
+ throws ClassNotFoundException, SQLException {
+
+ // Note: Change the connection parameters accordingly.
+ String hostName = "localhost";
+ String dbName = "jspapp";
+ String userName = "root";
+ String password = "root";
+ return getMySQLConnection(hostName, dbName, userName, password);
+ }
+
+ public static Connection getMySQLConnection(String hostName, String dbName,
+ String userName, String password) throws SQLException,
+ ClassNotFoundException {
+
+ // Declare the class driver for MySQL DB
+ Class.forName("com.mysql.jdbc.Driver");
+
+ // URL Connection for MySQL
+ // Example: jdbc:mysql://localhost:3306/simplehr
+ String connectionURL = "jdbc:mysql://" + hostName + ":8889/" + dbName;
+
+ Connection conn = DriverManager.getConnection(connectionURL, userName,
+ password);
+ return conn;
+ }
+
+}
diff --git a/src/com/lanihuang/simplewebapp/filter/CookieFilter.java b/src/com/lanihuang/simplewebapp/filter/CookieFilter.java
new file mode 100644
index 0000000..411603b
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/filter/CookieFilter.java
@@ -0,0 +1,74 @@
+package com.lanihuang.simplewebapp.filter;
+
+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 com.lanihuang.simplewebapp.beans.UserAccount;
+import com.lanihuang.simplewebapp.utils.DBUtils;
+import com.lanihuang.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.
+ session.setAttribute("COOKIE_CHECKED", "CHECKED");
+ }
+
+ chain.doFilter(request, response);
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/lanihuang/simplewebapp/filter/EncodingFilter.java b/src/com/lanihuang/simplewebapp/filter/EncodingFilter.java
new file mode 100644
index 0000000..f367d5c
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/filter/EncodingFilter.java
@@ -0,0 +1,38 @@
+package com.lanihuang.simplewebapp.filter;
+
+import java.io.IOException;
+
+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;
+
+
+@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/src/com/lanihuang/simplewebapp/filter/JDBCFilter.java b/src/com/lanihuang/simplewebapp/filter/JDBCFilter.java
new file mode 100644
index 0000000..dbe94db
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/filter/JDBCFilter.java
@@ -0,0 +1,129 @@
+package com.lanihuang.simplewebapp.filter;
+
+
+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 com.lanihuang.simplewebapp.conn.ConnectionUtils;
+import com.lanihuang.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 need
+ // connection. (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 connection
+ conn = ConnectionUtils.getConnection();
+
+ // Set Auto commit to false
+ conn.setAutoCommit(false);
+
+ // Store connection in attribute of request.
+ MyUtils.storeConnection(request, conn);
+
+ // Allow request to go forward
+ // (Go to the next filter or target)
+ chain.doFilter(request, response);
+
+ // Commit change.
+ 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/src/com/lanihuang/simplewebapp/servlet/CreateProductServlet.java b/src/com/lanihuang/simplewebapp/servlet/CreateProductServlet.java
new file mode 100644
index 0000000..b50df5a
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/servlet/CreateProductServlet.java
@@ -0,0 +1,35 @@
+package com.lanihuang.simplewebapp.servlet;
+
+import java.io.IOException;
+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.RequestDispatcher;
+
+@WebServlet(urlPatterns = { "/createProduct" })
+public class CreateProductServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
+ public CreateProductServlet() {
+ super();
+ }
+
+ @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);
+
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ doGet(request, response);
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/lanihuang/simplewebapp/servlet/DeleteProductServlet.java b/src/com/lanihuang/simplewebapp/servlet/DeleteProductServlet.java
new file mode 100644
index 0000000..a180327
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/servlet/DeleteProductServlet.java
@@ -0,0 +1,67 @@
+package com.lanihuang.simplewebapp.servlet;
+
+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 com.lanihuang.simplewebapp.utils.DBUtils;
+import com.lanihuang.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 an error redirected to an 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/src/com/lanihuang/simplewebapp/servlet/DoCreateProductServlet.java b/src/com/lanihuang/simplewebapp/servlet/DoCreateProductServlet.java
new file mode 100644
index 0000000..3c40dde
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/servlet/DoCreateProductServlet.java
@@ -0,0 +1,86 @@
+package com.lanihuang.simplewebapp.servlet;
+
+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 com.lanihuang.simplewebapp.beans.Product;
+import com.lanihuang.simplewebapp.utils.DBUtils;
+import com.lanihuang.simplewebapp.utils.MyUtils;
+
+@WebServlet(urlPatterns = { "/doCreateProduct" })
+public class DoCreateProductServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
+ public DoCreateProductServlet() {
+ super();
+ }
+
+ @Override
+ protected void doGet(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");
+ }
+
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ doGet(request, response);
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/lanihuang/simplewebapp/servlet/DoEditProductServlet.java b/src/com/lanihuang/simplewebapp/servlet/DoEditProductServlet.java
new file mode 100644
index 0000000..d7a2500
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/servlet/DoEditProductServlet.java
@@ -0,0 +1,75 @@
+package com.lanihuang.simplewebapp.servlet;
+
+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 com.lanihuang.simplewebapp.beans.Product;
+import com.lanihuang.simplewebapp.utils.DBUtils;
+import com.lanihuang.simplewebapp.utils.MyUtils;
+
+@WebServlet(urlPatterns = { "/doEditProduct" })
+public class DoEditProductServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
+ public DoEditProductServlet() {
+ super();
+ }
+
+ @Override
+ protected void doGet(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");
+ }
+
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ doGet(request, response);
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/lanihuang/simplewebapp/servlet/DoLoginServlet.java b/src/com/lanihuang/simplewebapp/servlet/DoLoginServlet.java
new file mode 100644
index 0000000..8b9b1e0
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/servlet/DoLoginServlet.java
@@ -0,0 +1,109 @@
+package com.lanihuang.simplewebapp.servlet;
+
+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 com.lanihuang.simplewebapp.beans.UserAccount;
+import com.lanihuang.simplewebapp.utils.DBUtils;
+import com.lanihuang.simplewebapp.utils.MyUtils;
+
+@WebServlet(urlPatterns = { "/doLogin" })
+public class DoLoginServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
+ public DoLoginServlet() {
+ super();
+ }
+
+ @Override
+ protected void doGet(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 {
+
+ 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");
+ }
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ doGet(request, response);
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/lanihuang/simplewebapp/servlet/EditProductServlet.java b/src/com/lanihuang/simplewebapp/servlet/EditProductServlet.java
new file mode 100644
index 0000000..1892854
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/servlet/EditProductServlet.java
@@ -0,0 +1,69 @@
+package com.lanihuang.simplewebapp.servlet;
+
+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 com.lanihuang.simplewebapp.beans.Product;
+import com.lanihuang.simplewebapp.utils.DBUtils;
+import com.lanihuang.simplewebapp.utils.MyUtils;
+
+@WebServlet(urlPatterns = { "/editProduct" })
+public class EditProductServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
+ public EditProductServlet() {
+ super();
+ }
+
+ @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);
+
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ doGet(request, response);
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/lanihuang/simplewebapp/servlet/HomeServlet.java b/src/com/lanihuang/simplewebapp/servlet/HomeServlet.java
new file mode 100644
index 0000000..a9c40aa
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/servlet/HomeServlet.java
@@ -0,0 +1,38 @@
+package com.lanihuang.simplewebapp.servlet;
+
+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/src/com/lanihuang/simplewebapp/servlet/LoginServlet.java b/src/com/lanihuang/simplewebapp/servlet/LoginServlet.java
new file mode 100644
index 0000000..2a7acb3
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/servlet/LoginServlet.java
@@ -0,0 +1,38 @@
+package com.lanihuang.simplewebapp.servlet;
+
+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 = { "/login"})
+public class LoginServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
+ public LoginServlet() {
+ super();
+ }
+
+ @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);
+
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+ doGet(request, response);
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/lanihuang/simplewebapp/servlet/ProductListServlet.java b/src/com/lanihuang/simplewebapp/servlet/ProductListServlet.java
new file mode 100644
index 0000000..d445722
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/servlet/ProductListServlet.java
@@ -0,0 +1,58 @@
+package com.lanihuang.simplewebapp.servlet;
+
+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 com.lanihuang.simplewebapp.beans.Product;
+import com.lanihuang.simplewebapp.utils.DBUtils;
+import com.lanihuang.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/src/com/lanihuang/simplewebapp/servlet/UserInfoServlet.java b/src/com/lanihuang/simplewebapp/servlet/UserInfoServlet.java
new file mode 100644
index 0000000..1bb0603
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/servlet/UserInfoServlet.java
@@ -0,0 +1,58 @@
+package com.lanihuang.simplewebapp.servlet;
+
+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 com.lanihuang.simplewebapp.beans.UserAccount;
+import com.lanihuang.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 in request attribute
+ request.setAttribute("user", loginedUser);
+
+
+ // Logined, forward to /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/src/com/lanihuang/simplewebapp/utils/DBUtils.java b/src/com/lanihuang/simplewebapp/utils/DBUtils.java
new file mode 100644
index 0000000..6f879ee
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/utils/DBUtils.java
@@ -0,0 +1,127 @@
+package com.lanihuang.simplewebapp.utils;
+
+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 com.lanihuang.simplewebapp.beans.Product;
+import com.lanihuang.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/src/com/lanihuang/simplewebapp/utils/MyUtils.java b/src/com/lanihuang/simplewebapp/utils/MyUtils.java
new file mode 100644
index 0000000..ea25b8d
--- /dev/null
+++ b/src/com/lanihuang/simplewebapp/utils/MyUtils.java
@@ -0,0 +1,78 @@
+package com.lanihuang.simplewebapp.utils;
+
+import java.sql.Connection;
+
+import javax.servlet.ServletRequest;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import com.lanihuang.simplewebapp.beans.UserAccount;
+
+public class MyUtils {
+
+ public static final String ATT_NAME_CONNECTION = "ATTRIBUTE_FOR_CONNECTION";
+
+ private static final String ATT_NAME_USER_NAME = "ATTRIBUTE_FOR_STORE_USER_NAME_IN_COOKIE";
+
+
+ // Store Connection in request attribute.
+ // (Information stored only exist during requests)
+ public static void storeConnection(ServletRequest request, Connection conn) {
+ request.setAttribute(ATT_NAME_CONNECTION, conn);
+ }
+
+ // Get the Connection object has been stored in one attribute of the request.
+ public static Connection getStoredConnection(ServletRequest request) {
+ Connection conn = (Connection) request.getAttribute(ATT_NAME_CONNECTION);
+ return conn;
+ }
+
+ // Store user info in Session.
+ public static void storeLoginedUser(HttpSession session, UserAccount loginedUser) {
+
+ // On the JSP can access ${loginedUser}
+ session.setAttribute("loginedUser", loginedUser);
+ }
+
+
+ // Get the user information stored in the session.
+ public static UserAccount getLoginedUser(HttpSession session) {
+ UserAccount loginedUser = (UserAccount) session.getAttribute("loginedUser");
+ return loginedUser;
+ }
+
+
+ // Store info in Cookie
+ public static void storeUserCookie(HttpServletResponse response, UserAccount user) {
+ System.out.println("Store user cookie");
+ Cookie cookieUserName = new Cookie(ATT_NAME_USER_NAME, user.getUserName());
+
+ // 1 day (Convert to seconds)
+ cookieUserName.setMaxAge(24 * 60 * 60);
+ response.addCookie(cookieUserName);
+ }
+
+ public static String getUserNameInCookie(HttpServletRequest request) {
+ Cookie[] cookies = request.getCookies();
+ if (cookies != null) {
+ for (Cookie cookie : cookies) {
+ if (ATT_NAME_USER_NAME.equals(cookie.getName())) {
+ return cookie.getValue();
+ }
+ }
+ }
+ return null;
+ }
+
+
+ // Delete cookie.
+ public static void deleteUserCookie(HttpServletResponse response) {
+ Cookie cookieUserName = new Cookie(ATT_NAME_USER_NAME, null);
+
+ // 0 seconds (Expires immediately)
+ cookieUserName.setMaxAge(0);
+ response.addCookie(cookieUserName);
+ }
+}
\ No newline at end of file