Skip to content
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
Original file line number Diff line number Diff line change
@@ -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) {
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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");
}
}

}
Original file line number Diff line number Diff line change
@@ -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"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Create Product</title>
</head>
<body>

<jsp:include page="_header.jsp"></jsp:include>
<jsp:include page="_menu.jsp"></jsp:include>

<h3>Create Product</h3>

<p style="color: red;">${errorString}</p>

<form method="POST" action="${pageContext.request.contextPath}/createProduct">
<table border="0">
<tr>
<td>Code</td>
<td><input type="text" name="code" value="${product.code}" /></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="name" value="${product.name}" /></td>
</tr>
<tr>
<td>Price</td>
<td><input type="text" name="price" value="${product.price}" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit" />
<a href="productList">Cancel</a>
</td>
</tr>
</table>
</form>

<jsp:include page="_footer.jsp"></jsp:include>

</body>
</html>
129 changes: 129 additions & 0 deletions Aashim Dhawan 1710991013/product list login Web app/DBUtills.java
Original file line number Diff line number Diff line change
@@ -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<Product> 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<Product> list = new ArrayList<Product>();
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();
}

}
Loading