我正在尝试使用servlet和jsp以及MySQL Workbench作为数据库将值插入到数据库表中。当我填写表格并提交时,它只显示空白屏幕,没有显示输出,数据库中的值也没有更新。这些是以下详细信息:
1)Controller.java
public class Controller extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String dburl = getServletContext().getInitParameter("dburl");
String dbuser = getServletContext().getInitParameter("dbuser");
String dbpassword = getServletContext().getInitParameter("dbpassword");
String dbdriver = getServletContext().getInitParameter("dbdriver");
Connection conn = DAO.getConnectionJDBC(dburl, dbuser, dbpassword, dbdriver);
String title = request.getParameter("title");
String actor = request.getParameter("actor");
String actress = request.getParameter("actress");
String genre = request.getParameter("genre");
String year = String.valueOf("year");
String queryMessage = "INSERT INTO movies (title, actor, actress,genre,year)"
+ "values (" + title + ",'" + actor + "','" + actress + "','"
+ genre + "','" + year + "')";
Statement statement = conn.createStatement();
int result = statement.executeUpdate(queryMessage);
conn.close();
if (result > 0) {
RequestDispatcher rd = request.getRequestDispatcher("/Added.jsp");
rd.forward(request, response);
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
2)DAO.java
public class DAO {
public static Connection getConnectionJDBC(String dburl, String dbuser,String dbpassword, String dbdriver) throws IOException {
Connection connection = null;
try {
Class.forName(dbdriver);
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
}
try {
connection = DriverManager.getConnection(dburl,dbuser,dbpassword);
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
}
if (connection != null) {
System.out.println("connection established");
}
return connection;
}
}
3)AddMovie.jsp
<form action="Controller" method="post">
code to insert in text box
</form>
4)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>com.myapp.controller.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/Controller</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<context-param>
<param-name>dbuser</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>dbpassword</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>dbdriver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>dburl</param-name>
<param-value>jdbc:mysql://localhost:3306/moviedb</param-value>
</context-param>