gpt4 book ai didi

java - 在 servlet 中处理结果集和从 HTML 获取的其他数据的最佳方法

转载 作者:行者123 更新时间:2023-12-01 15:00:19 24 4
gpt4 key购买 nike

好吧,对于我们的 Uni 项目,我们正在制作一个网站来存储有关教堂的信息(我知道 super 有趣,对吧 -.-)。

基本上,它只需要在数据库中包含教会信息,当用户点击或搜索教会时,它需要从数据库中提取教会信息以及用户评论。

到目前为止,我有一个 HTML 表单,它向 servlet/数据库传递注释并使用结果集输出它。我只是想知道处理多个信息的最佳方法是什么,因为现在我正在尝试提供评论和教会信息,但我有点不知道如何存储它。

提前致谢!

哦,这是我的引用代码:

HTML

    <form method="get" action="dbtest" id="form1">
<fieldset>
<p>Please enter you're Name:</p>
<input type="text" name="author" size="10" id="name"/><br />
<p>Please enter you're Email here:</p>
<input type="text" name="author_email" size="10" id="email"/><br />
<p>Please enter you're comment here:</p>
<input type="text" name="comment_text" size="10" id="comment"/><br />
<p>
<input type="submit" value="Submit" name="submit_button" />
</p>
</fieldset>
</form>

Servlet

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author mxk12ycu
*/
public class dbtest extends HttpServlet {

/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

String authorName = request.getParameter("author");
String authorEmail = request.getParameter("author_email");
String commentText = request.getParameter("comment_text");


try {
//Holding SQL statement
String SQL;
String insertSQL;

//insertSQL = "INSERT INTO comments VALUES('0002')" + authorName + authorEmail + commentText + ("('29 Nov 2012','002')");
//insertSQL = "INSERT INTO comments VALUES (default,'Ryan H','Test@hotmail.com','TestComments','29 Nov 2012','1')";

//insertSQL = "INSERT INTO comments (author, author_email, comment_text, comment_date, church_id) VALUES ('" + authorName + "'," + "'" + authorEmail + "'," + "'" + commentText + "', '29 Nov 2012', '1')";
//insertSQL = "INSERT INTO comments VALUES ('Ryan Holder', 'test@hotmail.com', 'Comment here', '30 Nov 2012', 'default', '1')";
insertSQL = "INSERT INTO comments VALUES (default, " + "'" + authorName + "'," + "'" + authorEmail + "'," + "'" + commentText + "', '29 Nov 2012', '1')";
SQL = "SELECT * FROM churches, comments";

Class.forName("org.postgresql.Driver");

Connection connection = DriverManager.getConnection(
"jdbc:postgresql:ChurchSearch",
"postgres",
"*****");

Statement statement = connection.createStatement();

statement.executeUpdate(insertSQL);

ResultSet resultSet = statement.executeQuery(SQL);


/* TODO output your page here. You may use following sample code. */
out.println("<html>");
out.println("<head>");
out.println("<title>Test Comment Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet test at " + request.getContextPath() + "</h1>");
out.println("<h2>Comment Form</h2>");

while (resultSet.next()) {
out.println("<p>");
out.println(resultSet.getString("author"));
out.println("</p>");
out.println("<p>");
out.println(resultSet.getString("author_email"));
out.println("</p>");
out.println("<p>");
out.println(resultSet.getString("comment_text"));
out.println("</p>");
}
out.println("</body>");
out.println("</html>");

connection.close();
}
catch (Exception e) {
System.err.println("Error: " + e);
}
finally {
out.close();
}
}

最佳答案

您可以使用MVC pattern 。另一方面,您必须确保关闭与数据库的连接。

Connection conn = null;
try {
conn = // get connection;

// do database operations

} catch (final SQLException e) {
throw new DAOException(e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (final SQLException ex) {
LOG.warning("message warning to log");
}
}
}

这会释放连接使用的所有资源。

关于MVC,您可以在 Integrating Servlets and JSP: The Model View Controller (MVC) Architecture 找到更多信息。您还可以使用 Struts、Spring MVC 等框架。

关于java - 在 servlet 中处理结果集和从 HTML 获取的其他数据的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13749592/

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