IDEA2023创建MavenWeb项目,并搭建Servlet工程

系统相关:

IntelliJ IDEA 2022.3.3

jdk v17+

tomcat v10.1.7

1、新建项目

我的是jdk17,选择电脑对应的jdk版本,后面涉及到这个的最好都保持一致。

2、创建出来的项目是没有java目录的,右键新建一个。

3、创建一个MyServlet.java类

package com.amos.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * @author Amos
 * @date 2023/4/13
 */

@WebServlet(name = "MyServlet", urlPatterns = "/myservlet")
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doGet====================");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doPost====================");
    }
}

4、添加Servlet的依赖库,这里需要注意下,别弄错了!

Tomcat 10是第一个不再使用javax.servlet和相关包的版本。在Tomcat 10中,Servlet API已经迁移到了Jakarta EE命名空间(jakarta.servlet)。这是因为Java EE已经转移到了Eclipse基金会,并更名为Jakarta EE。因此,Servlet API也需要进行相应的更改。

在Tomcat 10之前的版本中,Servlet API仍然使用javax.servlet和相关包。但是,如果您使用的是Tomcat 10或更高版本,则需要使用jakarta.servlet和相关包。如果您的应用程序使用旧的javax.servlet包,则需要将其迁移到新的jakarta.servlet包。

<!--tomcat 10+-->    
    <dependency>
        <groupId>jakarta.servlet</groupId>
        <artifactId>jakarta.servlet-api</artifactId>
        <version>5.0.0</version>
        <scope>provided</scope>
    </dependency>

<!--tomcat 10之前版本--> 
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
      </dependency>

5、编辑configuration文件,关联到本地已安装的tomcat,配置相关内容

6、这样直接运行可能会报如下错误,需要指定一下编译版本

在pom.xml文件中添加下面一段内容

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>

在Project Struture中选择一下Language level,这里为了保持一致,我选了17版本。

 最后,访问http://localhost:8080/servlet_test_war/myservlet,可以在控制台看到doGet方法已经被调用了。

注意:当添加完Artifact后,即servlet-test:war,在编辑configuration的server下的url,会自动给从http://localhost:8080变成http://localhost:8080/servlet_test_war/,所以最终访问路径就变成了http://localhost:8080/servlet_test_war/myservlet。

完结!

  • 18
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,我会尽力回答您的问题。 首先,您需要安装并配置好JavaMavenTomcat等必要的开发环境。接着,您可以按照以下步骤实现完整的登录业务功能: 1. 创建Maven Web项目Idea创建一个Maven项目,选择Web Application模板并填写相应信息。 2. 添加依赖 在pom.xml文件中添加以下依赖: ```xml <dependencies> <!-- Servlet API --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!-- JSP API --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <!-- JSTL --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- MySQL JDBC Driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> </dependencies> ``` 3. 创建数据库 使用MySQL等数据库管理系统创建一个名为`userdb`的数据库,并创建一个名为`users`的表,表结构如下: ```sql CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ``` 4. 创建登录页面 在Web应用程序的WebContent目录下创建一个名为`login.jsp`的JSP页面,该页面包含一个表单,用户可以输入用户名和密码。 ```html <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Login Page</title> </head> <body> <h1>Login Page</h1> <form method="post" action="login"> <label for="username">Username:</label> <input type="text" name="username" required><br> <label for="password">Password:</label> <input type="password" name="password" required><br> <input type="submit" value="Login"> </form> </body> </html> ``` 5. 创建登录ServletJava源代码的`src/main/java`目录下创建一个名为`LoginServlet.java`的Servlet类,该类实现了用户的登录功能。 ```java package com.example.webapp; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/login") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb", "root", "123456"); PreparedStatement statement = connection.prepareStatement("SELECT * FROM users WHERE username=? AND password=?"); statement.setString(1, username); statement.setString(2, password); ResultSet rs = statement.executeQuery(); if (rs.next()) { response.sendRedirect("welcome.jsp"); } else { response.sendRedirect("login.jsp"); } } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } } } ``` 6. 创建欢迎页面 在Web应用程序的WebContent目录下创建一个名为`welcome.jsp`的JSP页面,该页面显示欢迎消息。 ```html <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <html> <head> <title>Welcome Page</title> </head> <body> <h1>Welcome, <%= request.getParameter("username") %>!</h1> </body> </html> ``` 7. 部署应用程序 在Idea中配置Tomcat服务器,并将应用程序部署到Tomcat服务器上。 至此,完整的登录业务功能已经实现。 希望这些步骤能够帮助您实现您的Maven Web工程环境下的登录业务功能。如果您还有其他问题,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值