Skip to content

kaartik2611/java-posts-servlets-tomcat

Repository files navigation

Tomcat Basics Project

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.

Project Structure (src/com)

dao package

  • PostDao.java: Handles database operations related to Post objects (e.g., creating, reading, updating, deleting posts).
  • UserDao.java: Manages database interactions for User objects (e.g., user registration, retrieval by username or ID).

filter package

  • 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.

model package

  • Post.java: Represents the Post entity with properties like ID, title, content, and author. This is a POJO (Plain Old Java Object).
  • User.java: Represents the User entity with properties such as ID, username, password, and role. This is also a POJO.

service package

  • PostService.java: Provides business logic for Post related operations, orchestrating interactions between servlets and DAOs.
  • UserService.java: Encapsulates business logic for User related operations, handling tasks like user registration and authentication.

servlet package

  • 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.

store package

  • 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.

util package

  • DBUtil.java: Provides utility methods for database connectivity, managing connections, and other common database-related tasks.

Setup and Running the Application

Prerequisites

  • Java Development Kit (JDK) 8 or higher
  • Apache Tomcat 9 or higher
  • Ant build tool (if building manually)

Dependencies

  • h2-1.4.200.jar: H2 Database Engine (for in-memory or file-based database)
  • jstl-1.2.jar: JSP Standard Tag Library
  • servlet-api.jar: Java Servlet API (usually provided by Tomcat)

These dependencies are located in the lib/ directory.

Test Dependencies

  • 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.

Building the Project

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 .war file in the dist folder:

    ant war

This will compile the Java source files and create a .war file in the dist/ directory.

Deploying to Tomcat

  1. Start your Apache Tomcat server.
  2. Copy the generated .war file (e.g., tomcat-basics.war) from the dist/ directory into Tomcat's webapps/ directory.
  3. Tomcat will automatically deploy the application. You can access it in your web browser, typically at http://localhost:8080/tomcat-basics/.

Database Configuration

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.

About

java servlets project dev + fat jar creation with ant

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages