使用json参数单击jsp文件中的按钮来运行servlet时出现问题

问题描述 投票:0回答:1

当我单击

index.jsp
中的按钮时,servlet 不会运行。(实际上什么也没发生)

我安装了jdk17和intellij和tomcat9。

我所做的就是:当我在 chrome 中运行项目时,地址是:

http://localhost:8080/test_register_war_exploded/

然后我填写表格并单击按钮,但什么也没发生。

index.jsp

            <form method="post">
            <table cellpadding="3pt">
                <tr>
                    <td>Firstname :</td>
                    <td><input type="text" name="firstname" size="50" class="add_register"/></td>
                </tr>
                <tr>
                    <td>Lastname :</td>
                    <td><input type="text" name="lastname" size="50" class="add_register"/></td>
                </tr>
                <tr>
                    <td>email :</td>
                    <td><input type="text" name="email" size="50" class="add_register"/></td>
                </tr>
                <tr>
                    <td>Password :</td>
                    <td><input type="password" name="password1" size="50" class="add_register"/></td>
                </tr>

                <tr>
                    <td>Confirm Password :</td>
                    <td><input type="password" name="password2" size="50" class="add_register"/></td>
                </tr>
            </table>
            <p />
            <input type="button" value="Register" id="register"/>
        </form>
        ...
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          <script type="text/javascript">
      $(document).ready(function() {
        $("#register").click(function() {
        let data = [];
        let json = {};
        let address_data = {};
        $("input.add_register").each(function(){
             json[this.name] = this.value;
        });
      
        data.push(json);
        alert(JSON.stringify(data));
        $.ajax({
            url: '/register-controller',
            type: 'POST',
            data: data,
            dataType: 'json',
            contentType: 'application/json',
            mimeType: 'application/json',
        });
    });
});

RegisterController.java

@WebServlet(name = "registerController", value = "/register-controller")
public class RegisterController extends HttpServlet {
private UserJsonUtils jsonUtils;
private User data;
private UserDao userDao;
final String userPath = "src/model/user/users.json";
public void init() {
    jsonUtils = new UserJsonUtils();
    userDao = new UserDao();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    data = jsonUtils.parseJson(request.getParameter("data"));
    register();
}
public void register() {
    User user = new User(data.getFirstName(),
            data.getLastName(),
            data.getEmail(),
            data.getPassword());
    userDao.save(user);
}

}

UserDao.java

public class UserDao implements IUser {
@Override
public void save(User user) {
    Connection con = dbConnection.makeConnection();
    PreparedStatement ps = null;
    try {
        ps = con.prepareStatement("insert into users (first_name,last_name, email, password) " +
                        "values(?,?,?,?);",
                Statement.RETURN_GENERATED_KEYS);
        ps.setString(1, user.getFirstName());
        ps.setString(2, user.getLastName());
        ps.setString(3, user.getEmail());
        ps.setString(4, user.getPassword());

        int k = ps.executeUpdate();
        if (k > 0) {
            ResultSet rs = ps.getGeneratedKeys();
            while (rs.next()) {
                user.setId(rs.getLong(1));
            }
        }

    } catch (SQLException e) {
        e.printStackTrace();
    }

    dbConnection.closeConnection(ps);
    dbConnection.closeConnection();
}

UserJsonUtils.java

public class UserJsonUtils {
private User data;

public UserJsonUtils() {
}

public User getData() {
    return data;
}

public void setData(User data) {
    this.data = data;
}

public User parseJson(String params) {
    Gson gson = new Gson();
    data = gson.fromJson(params, User.class);
    if (data != null)
        return data;
    else
        return null;
}
public void writeToFile(String path, User user) throws IOException {
    Gson gson = new Gson();
    gson.toJson(user, new FileWriter(path));
}

}

我添加了输出图像以显示单击按钮时没有任何反应。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>test-register</artifactId>
<version>1.0-SNAPSHOT</version>
<name>test-register</name>
<packaging>war</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
    <junit.version>5.8.2</junit.version>
</properties>

<dependencies>
    <dependency>
        <groupId>jakarta.servlet</groupId>
        <artifactId>jakarta.servlet-api</artifactId>
        <version>5.0.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.9.1</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.33</version>
    </dependency>
 </dependencies>

 <build>
     <plugins>
         <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-war-plugin</artifactId>
             <version>3.3.2</version>
         </plugin>
     </plugins>
  </build>
</project>
java json servlets
1个回答
0
投票

它无法识别您的 servlet,因为您使用了对

jakarta.servlet-api
的依赖,但您的 Tomcat 服务器版本是 9。您应该将 Tomcat 版本更改为 10.x 及更高版本才能运行您的应用程序。

<dependency>
        <groupId>jakarta.servlet</groupId>
        <artifactId>jakarta.servlet-api</artifactId>
        <version>5.0.0</version>
        <scope>provided</scope>
</dependency>
© www.soinside.com 2019 - 2024. All rights reserved.