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
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,30 @@
# javaee
# Java EE Management System Skeleton

This project provides a simple JSP/Servlet based management system.

## Features

- User registration with role selection (admin or user)
- Login and session handling
- Admin-only item management (add/delete)
- Item listing for all users
- Bootstrap-based interface

## Project Structure

```
src/ Java source files
WebContent/jsp/ JSP pages
WebContent/WEB-INF/ web.xml configuration
schema.sql MySQL schema
```

## Database Setup

Import `schema.sql` into your MySQL server and adjust the credentials in
`src/com/example/util/DBUtil.java`.

## Build

Use a servlet container such as Tomcat. Place the contents of this repository
as a web application and compile the Java sources.
9 changes: 9 additions & 0 deletions WebContent/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<welcome-file-list>
<welcome-file>jsp/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4 changes: 4 additions & 0 deletions WebContent/jsp/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%@ page contentType="text/html;charset=UTF-8" %>
<%
response.sendRedirect("login.jsp");
%>
77 changes: 77 additions & 0 deletions WebContent/jsp/items.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="com.example.model.User" %>
<%@ page import="java.util.List" %>
<%@ page import="com.example.model.Item" %>
<html>
<head>
<title>Items</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
</head>
<body class="container">
<%
User user = (User) session.getAttribute("user");
if(user == null){
response.sendRedirect("login.jsp");
return;
}
%>
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-3">
<div class="container-fluid">
<a class="navbar-brand" href="#">Management</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item"><a class="nav-link" href="items">Items</a></li>
<li class="nav-item"><a class="nav-link" href="logout.jsp">Logout</a></li>
</ul>
</div>
</div>
</nav>
<h2>Items</h2>
<%
List<Item> list = (List<Item>) request.getAttribute("list");
if(list == null){
list = new java.util.ArrayList<>();
}
String role = user.getRole();
%>
<% if("admin".equals(role)){ %>
<form action="items" method="post" class="mb-3">
<input type="hidden" name="action" value="add">
<div class="row g-2">
<div class="col">
<input type="text" name="name" class="form-control" placeholder="Name" required>
</div>
<div class="col">
<input type="number" name="quantity" class="form-control" placeholder="Quantity" required>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</div>
</form>
<% } %>
<table class="table table-bordered">
<thead>
<tr><th>ID</th><th>Name</th><th>Quantity</th><% if("admin".equals(role)){ %><th>Actions</th><% } %></tr>
</thead>
<tbody>
<% for(Item item: list){ %>
<tr>
<td><%= item.getId() %></td>
<td><%= item.getName() %></td>
<td><%= item.getQuantity() %></td>
<% if("admin".equals(role)){ %>
<td>
<form action="items" method="post" style="display:inline">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="id" value="<%= item.getId() %>">
<button class="btn btn-danger btn-sm">Delete</button>
</form>
</td>
<% } %>
</tr>
<% } %>
</tbody>
</table>
</body>
</html>
25 changes: 25 additions & 0 deletions WebContent/jsp/login.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>Login</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
</head>
<body class="container">
<h2>Login</h2>
<% String error = request.getParameter("error"); if("1".equals(error)){ %>
<div class="alert alert-danger">Invalid username or password</div>
<% } %>
<form action="login" method="post">
<div class="mb-3">
<label class="form-label">Username</label>
<input type="text" name="username" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label">Password</label>
<input type="password" name="password" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary">Login</button>
<a href="register.jsp" class="btn btn-link">Register</a>
</form>
</body>
</html>
5 changes: 5 additions & 0 deletions WebContent/jsp/logout.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%@ page contentType="text/html;charset=UTF-8" %>
<%
session.invalidate();
response.sendRedirect("login.jsp");
%>
29 changes: 29 additions & 0 deletions WebContent/jsp/register.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>Register</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
</head>
<body class="container">
<h2>Register</h2>
<form action="register" method="post">
<div class="mb-3">
<label class="form-label">Username</label>
<input type="text" name="username" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label">Password</label>
<input type="password" name="password" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label">Role</label>
<select name="role" class="form-select">
<option value="user">User</option>
<option value="admin">Admin</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Register</button>
<a href="login.jsp" class="btn btn-link">Login</a>
</form>
</body>
</html>
15 changes: 15 additions & 0 deletions schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE DATABASE IF NOT EXISTS management;
USE management;

CREATE TABLE IF NOT EXISTS users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(100) NOT NULL,
role VARCHAR(10) NOT NULL
);

CREATE TABLE IF NOT EXISTS items (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
quantity INT NOT NULL
);
80 changes: 80 additions & 0 deletions src/com/example/dao/ItemDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.example.dao;

import com.example.model.Item;
import com.example.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ItemDAO {
public void insert(Item item) throws SQLException {
String sql = "INSERT INTO items(name,quantity) VALUES(?,?)";
try (Connection conn = DBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
ps.setString(1, item.getName());
ps.setInt(2, item.getQuantity());
ps.executeUpdate();
try (ResultSet rs = ps.getGeneratedKeys()) {
if (rs.next()) {
item.setId(rs.getInt(1));
}
}
}
}

public void update(Item item) throws SQLException {
String sql = "UPDATE items SET name=?,quantity=? WHERE id=?";
try (Connection conn = DBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, item.getName());
ps.setInt(2, item.getQuantity());
ps.setInt(3, item.getId());
ps.executeUpdate();
}
}

public void delete(int id) throws SQLException {
String sql = "DELETE FROM items WHERE id=?";
try (Connection conn = DBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, id);
ps.executeUpdate();
}
}

public Item findById(int id) throws SQLException {
String sql = "SELECT * FROM items WHERE id=?";
try (Connection conn = DBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, id);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
Item item = new Item();
item.setId(rs.getInt("id"));
item.setName(rs.getString("name"));
item.setQuantity(rs.getInt("quantity"));
return item;
}
}
}
return null;
}

public List<Item> listAll() throws SQLException {
List<Item> list = new ArrayList<>();
String sql = "SELECT * FROM items";
try (Connection conn = DBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
Item item = new Item();
item.setId(rs.getInt("id"));
item.setName(rs.getString("name"));
item.setQuantity(rs.getInt("quantity"));
list.add(item);
}
}
return list;
}
}
62 changes: 62 additions & 0 deletions src/com/example/dao/UserDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.example.dao;

import com.example.model.User;
import com.example.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDAO {
public void insert(User user) throws SQLException {
String sql = "INSERT INTO users(username,password,role) VALUES(?,?,?)";
try (Connection conn = DBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
ps.setString(3, user.getRole());
ps.executeUpdate();
try (ResultSet rs = ps.getGeneratedKeys()) {
if (rs.next()) {
user.setId(rs.getInt(1));
}
}
}
}

public User findByUsername(String username) throws SQLException {
String sql = "SELECT * FROM users WHERE username=?";
try (Connection conn = DBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, username);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setRole(rs.getString("role"));
return user;
}
}
}
return null;
}

public List<User> listAll() throws SQLException {
List<User> list = new ArrayList<>();
String sql = "SELECT * FROM users";
try (Connection conn = DBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setRole(rs.getString("role"));
list.add(user);
}
}
return list;
}
}
16 changes: 16 additions & 0 deletions src/com/example/model/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.model;

public class Item {
private int id;
private String name;
private int quantity;

public int getId() { return id; }
public void setId(int id) { this.id = id; }

public String getName() { return name; }
public void setName(String name) { this.name = name; }

public int getQuantity() { return quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }
}
20 changes: 20 additions & 0 deletions src/com/example/model/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.model;

public class User {
private int id;
private String username;
private String password;
private String role; // admin or user

public int getId() { return id; }
public void setId(int id) { this.id = id; }

public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }

public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }

public String getRole() { return role; }
public void setRole(String role) { this.role = role; }
}
Loading