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 as
SimpleLoginApp
), 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 in
Name
,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.
- fill in
- 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 MySQLuser
Table, 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
:
<form action="LoginServlet" method="post"> username: <input type="text" name="username" required><br> password: <input type="password" name="password" required><br> <button type="submit">Log in</button> </form>
-
Registration page
:
<form action="RegisterServlet" method="post"> username: <input type="text" name="username" required><br> password: <input type="password" name="password" required><br> Mail: <input type="email" name="email"><br> gender: <select name="gender"> <option value="male">male</option> <option value="female">female</option> <option value="other">other</option> </select><br> Birthday: <input type="date" name="birthday"><br> <button type="submit">register</button> </form>
-
User List Page
:
<%@ page import="" %> <% ResultSet rs = (ResultSet) ("users"); %> <table border="1"> <tr> <th>username</th> <th>Mail</th> <th>gender</th> <th>Birthday</th> </tr> <% while (()) { %> <tr> <td><%= ("username") %></td> <td><%= ("email") %></td> <td><%= ("gender") %></td> <td><%= ("birthday") %></td> </tr> <% } %> </table>
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!