This project is a basic web application built with Java Servlets, JSP, and JDBC, running on Apache Tomcat. It demonstrates fundamental concepts of web development with Java, including user authentication, CRUD operations for posts, and administrative features.
PostDao.java: Handles database operations related toPostobjects (e.g., creating, reading, updating, deleting posts).UserDao.java: Manages database interactions forUserobjects (e.g., user registration, retrieval by username or ID).
AdminFilter.java: A Servlet Filter that restricts access to admin-only resources, ensuring only authenticated administrators can proceed.AuthFilter.java: A Servlet Filter responsible for authenticating users and ensuring that protected resources are only accessible to logged-in users.
Post.java: Represents thePostentity with properties like ID, title, content, and author. This is a POJO (Plain Old Java Object).User.java: Represents theUserentity with properties such as ID, username, password, and role. This is also a POJO.
PostService.java: Provides business logic forPostrelated operations, orchestrating interactions between servlets and DAOs.UserService.java: Encapsulates business logic forUserrelated operations, handling tasks like user registration and authentication.
AdminServlet.java: Handles requests related to administrative functionalities, such as managing users or posts by administrators.DBSchemaInitializer.java: A servlet that initializes the database schema when the application starts, creating necessary tables if they don't exist.LoginServlet.java: Processes user login requests, authenticates credentials, and manages user sessions.LogoutServlet.java: Handles user logout requests, invalidating the current user session.PostServlet.java: Manages CRUD operations for blog posts, allowing users to create, view, edit, and delete their posts.RegisterServlet.java: Processes new user registration requests, saving new user details to the database.RootServlet.java: The main entry point or a default servlet for the application, typically redirecting to the home page or a list of posts.
UserStore.java: A simple in-memory store for users. This might be used for quick testing or as a placeholder before integrating with a full database.
DBUtil.java: Provides utility methods for database connectivity, managing connections, and other common database-related tasks.
- Java Development Kit (JDK) 8 or higher
- Apache Tomcat 9 or higher
- Ant build tool (if building manually)
h2-1.4.200.jar: H2 Database Engine (for in-memory or file-based database)jstl-1.2.jar: JSP Standard Tag Libraryservlet-api.jar: Java Servlet API (usually provided by Tomcat)
These dependencies are located in the lib/ directory.
junit-jupiter-api-5.9.3.jar: JUnit 5 Jupiter API for writing tests.junit-jupiter-engine-5.9.3.jar: JUnit 5 Jupiter Engine for running tests.mockito-core-3.12.4.jar: Mockito for creating mock objects in tests.mockito-junit-jupiter-4.11.0.jar: Mockito integration with JUnit 5.apiguardian-api-1.1.2.jar: API Guardian for internal use by testing frameworks.byte-buddy-1.18.3.jar,byte-buddy-agent-1.18.3.jar: Byte Buddy for runtime code generation used by Mockito.junit-platform-commons-1.9.3.jar: JUnit Platform Commons for common utilities.junit-platform-engine-1.9.3.jar: JUnit Platform Engine for discovering and executing tests.junit-platform-launcher-1.9.3.jar: JUnit Platform Launcher for launching the test platform.objenesis-3.2.jar: Objenesis for instantiating objects without invoking their constructors, used by Mockito.opentest4j-1.2.0.jar: Open Test 4 J for common test interfaces.
These test dependencies are located in the lib/test/ directory.
If you are using Ant, navigate to the project root directory and use the following commands:
-
To clean, compile & run tests in the project:
ant clean ant compile ant compile-tests ant test -
To compile the project and create the
.warfile in the dist folder:ant war
This will compile the Java source files and create a .war file in the dist/ directory.
- Start your Apache Tomcat server.
- Copy the generated
.warfile (e.g.,tomcat-basics.war) from thedist/directory into Tomcat'swebapps/directory. - Tomcat will automatically deploy the application. You can access it in your web browser, typically at
http://localhost:8080/tomcat-basics/.
The application uses an H2 database. The DBSchemaInitializer.java servlet will create the necessary tables on application startup. Database connection details are configured in WebContent/META-INF/context.xml and src/com/util/DBUtil.java.