SoFunction
Updated on 2025-03-05

Complete steps to create a simple Java Web project using IntelliJ IDEA

The following are the steps to create a few simple Java Web projects using IntelliJ IDEA, each project implements basic login, registration and viewing list functions, relying onServlet/JSPand basic Java web development.

Preparation

  • Make sure it is installedIntelliJ IDEA Ultimate(Community version does not support web applications).
  • Make sure it is installedJDK 8 or above
  • Download and configureTomcat Server(Make sure Tomcat is configured in IDEA).
  • Create a MySQL database and prepare the corresponding table structure.

Project function implementation steps

1. Create a project

  • Open IntelliJ IDEA and clickFile -> New -> Project
  • Select on the leftJava Enterprise, on the right side:
    • CheckWeb Application
    • CheckJava(Select the corresponding JDK).
    • ClickNext
  • Enter the project name (such asSimpleLoginApp), select the project location.
  • CheckCreate Generate a standard web project structure.
  • ClickFinish

2. Configure Tomcat

  • Click on the top menuRun -> Edit Configurations
  • Click on the upper left corner+,chooseTomcat Server -> Local
  • Configure Tomcat:
    • fill inName,likeTomcat-SimpleLoginApp
    • existApplication ServerSelect Tomcat (click Configure to point to the local Tomcat installation directory).
    • existDeploymentIn the tab, click+,chooseAdd Artifactand add the project's WAR package.
  • ClickApply -> OK

3. Project file structure

The basic structure of the created project is as follows:

SimpleLoginApp/
├── src/main/java/
│   └── /
│       ├── 
│       ├── 
│       ├── 
│       └── 
├── src/main/resources/
├── src/main/webapp/
│   ├── WEB-INF/
│   │   ├── 
│   ├── 
│   ├── 
│   ├── 
│   └── 

4. Create databases and tables

Create a simple one with MySQLuserTable, used to store username, password and other information.

CREATE DATABASE simple_login_app;

USE simple_login_app;

CREATE TABLE user (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    email VARCHAR(100),
    gender ENUM('male', 'female', 'other'),
    birthday DATE
);

5. Implement functions

1. Database Connection Tool Class

existCreated in package

package ;

import ;
import ;

public class DatabaseConnection {
    private static final String URL = "jdbc:mysql://localhost:3306/simple_login_app";
    private static final String USER = "root";
    private static final String PASSWORD = "your_password";

    public static Connection getConnection() throws Exception {
        ("");
        return (URL, USER, PASSWORD);
    }
}

2. Login function

existCreated in package

package ;

import .*;
import .*;
import ;
import ;
import ;
import ;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = ("username");
        String password = ("password");

        try (Connection conn = ()) {
            String query = "SELECT * FROM user WHERE username = ? AND password = ?";
            PreparedStatement stmt = (query);
            (1, username);
            (2, password);

            ResultSet rs = ();
            if (()) {
                ().setAttribute("username", username);
                ("");
            } else {
                ().println("<h3>Invalid username or password</h3>");
            }
        } catch (Exception e) {
            ();
        }
    }
}

3. Registration function

existCreated in package

package ;

import .*;
import .*;
import ;
import ;
import ;

public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = ("username");
        String password = ("password");
        String email = ("email");
        String gender = ("gender");
        String birthday = ("birthday");

        try (Connection conn = ()) {
            String query = "INSERT INTO user (username, password, email, gender, birthday) VALUES (?, ?, ?, ?, ?)";
            PreparedStatement stmt = (query);
            (1, username);
            (2, password);
            (3, email);
            (4, gender);
            (5, birthday);

            ();
            ("");
        } catch (Exception e) {
            ();
        }
    }
}

4. View user list function

existCreated in package

package ;

import .*;
import .*;
import ;
import ;
import ;
import ;

public class UserListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try (Connection conn = ()) {
            String query = "SELECT * FROM user";
            PreparedStatement stmt = (query);

            ResultSet rs = ();
            ("users", rs);
            RequestDispatcher dispatcher = ("");
            (request, response);
        } catch (Exception e) {
            ();
        }
    }
}

5. JSP page

  • Login page :
&lt;form action="LoginServlet" method="post"&gt;
    username: &lt;input type="text" name="username" required&gt;&lt;br&gt;
    password: &lt;input type="password" name="password" required&gt;&lt;br&gt;
    &lt;button type="submit"&gt;Log in&lt;/button&gt;
&lt;/form&gt;
  • Registration page :
&lt;form action="RegisterServlet" method="post"&gt;
    username: &lt;input type="text" name="username" required&gt;&lt;br&gt;
    password: &lt;input type="password" name="password" required&gt;&lt;br&gt;
    Mail: &lt;input type="email" name="email"&gt;&lt;br&gt;
    gender: 
    &lt;select name="gender"&gt;
        &lt;option value="male"&gt;male&lt;/option&gt;
        &lt;option value="female"&gt;female&lt;/option&gt;
        &lt;option value="other"&gt;other&lt;/option&gt;
    &lt;/select&gt;&lt;br&gt;
    Birthday: &lt;input type="date" name="birthday"&gt;&lt;br&gt;
    &lt;button type="submit"&gt;register&lt;/button&gt;
&lt;/form&gt;
  • User List Page :
&lt;%@ page import="" %&gt;
&lt;%
    ResultSet rs = (ResultSet) ("users");
%&gt;
&lt;table border="1"&gt;
    &lt;tr&gt;
        &lt;th&gt;username&lt;/th&gt;
        &lt;th&gt;Mail&lt;/th&gt;
        &lt;th&gt;gender&lt;/th&gt;
        &lt;th&gt;Birthday&lt;/th&gt;
    &lt;/tr&gt;
    &lt;%
        while (()) {
    %&gt;
    &lt;tr&gt;
        &lt;td&gt;&lt;%= ("username") %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= ("email") %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= ("gender") %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= ("birthday") %&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;%
        }
    %&gt;
&lt;/table&gt;

6. Configuration

<web-app>
    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class></servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/LoginServlet</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>RegisterServlet</servlet-name>
        <servlet-class></servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RegisterServlet</servlet-name>
        <url-pattern>/RegisterServlet</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>UserListServlet</servlet-name>
        <servlet-class></servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserListServlet</servlet-name>
        <url-pattern>/UserListServlet</url-pattern>
    </servlet-mapping>
</web-app>

Start the project

  • Run the Tomcat server.
  • Open the browser to access the login page:http://localhost:8080/SimpleLoginApp/
  • Test the ability to register, log in, and view user list.

This allows you to implement a basic Java Web application. If there is more demand, you can continue to expand!

Summarize

This is the end of this article about using IntelliJ IDEA to create a simple Java Web project. For more information about IDEA creating Java Web Projects, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!