gpt4 book ai didi

java - 2 个 Servlet 之间的通信不适用于数据库

转载 作者:行者123 更新时间:2023-11-30 00:56:09 25 4
gpt4 key购买 nike

大家好,我现在正在学习如何自己使用 Servlet 和数据库,并且单个部分正在工作。但所有这些都不起作用,因为我的 DatabaseConnection 类没有从我的 LoginServlet 获取数据。

所以我通过 Getter 从 LoginServlet 获取数据,如果我运行 DatabaseConnection,它会在我的数据库中用“null”填充一个新行,因为我没有通过我的注册页面发送任何内容。我的 LoginServlet 接收来自注册的数据并检查它们是否设置正确,如果正确,用户将被发送到新页面。这些是单个部分。如果我运行整个项目,我不会收到任何错误,并且 glassfish 告诉我 LoginServlet 收到了数据,DatabaseConnection 正在运行,但在控制台中没有显示任何内容,并且 MySQL 没有收到任何内容。

我想知道的第二件事是为什么我无法正确设置时间戳和 SimpleDateFormater(解析字符串诞生)

感谢您的帮助。

数据库连接.java

import java.sql.Connection;
import java.sql.DriverManager;

public class DatabaseConnection {

static boolean connection;
public LoginServlet ls = new LoginServlet();

public static void main(String[] args) {
LoginServlet ls = new LoginServlet();
Connection conn = null;

System.out.println("Im here");//not showing up
try {
String ausgabe = "SELECT * FROM accounts";
String url = "jdbc:mysql://127.0.0.1:3306/project";
Class.forName("com.mysql.jdbc.Driver");

conn = DriverManager.getConnection(url, "root", "");
System.out.println("Database connection wurde hergestellt ");

// Manuelle Eingabe der Daten
// conn.createStatement().execute("INSERT INTO `accounts` (`name`, `password`, `salt`,`createdat`,`birthday`,`gender`) VALUES ('Test4', 'bla','123','12-11-13 12:12:12','1994-11-12','1')"); //Insert a row
System.out.println("DB: " + ls.username);
conn.createStatement().execute("INSERT INTO `accounts` (`name`, `password`, `salt`,`createdat`,`birthday`,`gender`) VALUES ('" + ls.username + "', '" + ls.password + "','123','12-11-13 12:12:12','1337-11-11','" + ls.gend + "')"); //Insert a row


System.out.println("Clientinfo: " + conn.getClientInfo());
System.out.println("Datenbank: " + conn.getCatalog());
System.out.println("MetaData: " + conn.getMetaData());
System.out.println("Warnungen: " + conn.getWarnings());



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

} finally {
if (conn != null) {

try {
conn.close();
System.out.println("Database connection beendet");

} catch (Exception e) { /* ignore close errors */ }
}
}
}
}

LoginServlet.java

    import java.io.IOException;
import java.io.PrintWriter;
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("/loginServlet")
public class LoginServlet extends HttpServlet {

String username, password, cpassword, email, birth, gender;
int gend;

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

// read form fields
username = request.getParameter("username");
password = request.getParameter("password");
cpassword = request.getParameter("cpassword");
email = request.getParameter("email");
birth = request.getParameter("birth");
gender = request.getParameter("gender");

System.out.println("username: " + username);
System.out.println("password: " + password);
System.out.println("");


CharSequence mySet = "@";
CharSequence mys = "m";
CharSequence wys = "w";

boolean e = false;

if (gender.contains(wys) || gender.contains(mys)) {
e = true;

} else if (gender.contains(wys)) {
gend = 0;
} else {
gend = 1;
}

// get response writer
//PrintWriter writer = response.getWriter();
if (password.equals(cpassword) && e) {
PrintWriter writer = response.getWriter();

// build HTML code
String htmlRespone = "<html>";
htmlRespone += "<h2>Dein Benutzername ist: " + username + "<br/>";
htmlRespone += "Dein Password ist: " + password + "<br/>";
htmlRespone += "Deine Email ist: " + email + "<br/>";
htmlRespone += "Dein Geburtstag ist: " + birth + "<br/>";
htmlRespone += "Dein Geschlecht ist: " + gender + "</h2>";

htmlRespone += "</html>";

// return response
writer.println(htmlRespone);
} else if (password != cpassword) {
PrintWriter writer = response.getWriter();

String htmlResponse = "<html>";
htmlResponse += "Dein Passwort stimmt nicht überein";
htmlResponse += "</html>";
htmlResponse += "<a href=\"Registration.jsp\" target=\"links\"> Zurück</a>";



writer.println(htmlResponse);

} else if (e == false) {
} else if (birth == null) {
System.out.println("Geburtstag falsch formatiert");
}

}

public String getUsername() {
System.out.println("getter: " + username);
return username;
}

public String getPassword() {
return password;
}

public String getCpassword() {
return cpassword;
}

public String getEmail() {
return email;
}

public String getBirth() {
return birth;
}

public int getGender() {
return gend;
}

}

注册.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Registration</title>
</head>
<body>

<h1>Hello World!</h1>

<a href="index.jsp" target="links">Startseite</a>

<div align="center">
<br/>
<form name="reg" method="POST" action="loginServlet"><p>
<table width="370">
<tr>
<td style="float: left;">Benutzername&nbsp;&nbsp;</td>
<td style="float: right;"><input type="text" name="username" class='input' maxlength="12" placeholder="Benutzername" autocomplete="off" required autofocus>
</td>
</tr>
<tr>
<td style="float: left;">Passwort:&nbsp;&nbsp;</td>
<td style="float: right;"><input type="password" name="password" class='input' maxlength="12" placeholder="Passwort" required></td>
</tr>
<tr>
<td style="float: left;">Passwort Bestätigen:&nbsp;&nbsp;</td>
<td style="float: right;"><input type="password" name="cpassword" class='input' maxlength="12" placeholder="Passwort Bestätigen" required></td>
</tr>
<tr>
<td style="float: left;">Email:&nbsp;&nbsp;</td>
<td style="float: right;"><input type="text" class='input' name="email" placeholder="Email" required></td>
</tr>
<tr>
<td style="float: left;">Geburtstag:<br><small>(YYYY-MM-DD)</small>&nbsp;&nbsp;</td>
<td style="float: right;"><input id="datepicker" type="text" maxlength="10" class='input' name="birth" placeholder="Geburtstag" required></td>
</tr>
<tr>
<td style="float: left;">Geschlecht:<br><small>(m oder w)</small>&nbsp;&nbsp;</td>
<td style="float: right;"><input type="text" maxlength="1" class='input' name="gender" placeholder="Geschlecht" required></td>
</tr>
</table>
<input type="submit" value="Bestätigen" class="doVote button" style="" name="register">
</p></form>
</div>

最佳答案

有一件事你没有理解,那就是你不能创建 Servlet 的 Request 和 Response 对象。这是 Web 容器的工作,所以,自己创建 Servlet 实例是错误的。 Please study the Servlet life cycle .

此代码不会给出错误,因为 Servlet 也是普通类。

关于java - 2 个 Servlet 之间的通信不适用于数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20531886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com