我正在尝试连接数据库并写一些记录,但我收到此错误 任何人都可以帮助我纠正它我真的很感激我是java编程的新手 这是我的jsp文件:NewFile.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="/Add" method="post" enctype="multipart/form-data">
Enter news ID: <br>
<input type="text" name="id"><br><br>
Enter title :<br>
<input type="text" name="title"><br><br>
Choose an image :
<input type="file" name="image" required="required">
<br><br>
<input type="submit" value="add news">
</form>
</body>
</html>
这是我的Add.java
package com.example.saeid;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
public class Add extends HttpServlet
{
private static final long serialVersionUID = 1L;
private String db_userName = "root";
private String db_Password = "uyhgbv098";
private String db_Name = "my_demo_database";
private String driver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/";
private Connection getConnection()
{
Connection conn = null;
try
{
Class.forName(driver);
conn = DriverManager.getConnection(
url + db_Name,
db_userName,
db_Password);
}
catch (Exception e)
{
throw new RuntimeException("Failed to obtain database connection.", e);
}
return conn;
}
public void doPost( HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String newsId = request.getParameter("id");
int newsID = Integer.parseInt(newsId);
String newsTitle = request.getParameter("title");
String body = "body";
InputStream inputStream = null;
Part part = request.getPart("image");
inputStream = part.getInputStream();
Connection conn = null;
try
{
conn = getConnection();
String query = "INSERT INTO news (id , title , subm_date , text , image ) values (?, ?, NOW() ,? ,?)";
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, newsID);
ps.setString(2,newsTitle );
ps.setString(3, body);
ps.setBlob(4, inputStream);
int row = ps.executeUpdate();
if (row > 0)
{
RequestDispatcher rd = request.getRequestDispatcher("NewFile.jsp");
rd.include(request,response);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
}
和web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID"
version="3.1">
<display-name>12</display-name>
<welcome-file-list>
<welcome-file>NewFile.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>addServlet</servlet-name>
<servlet-class>com.example.saeid.Add</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>addServlet</servlet-name>
<url-pattern>NewFile</url-pattern>
</servlet-mapping>
答案 0 :(得分:0)
404的原因之一是URL写得不正确,链接不正确。 如果在访问localhost:8080 / NewFile.jsp页面或单击Add时得到404响应,则表单操作指向/ Add但是url-pattern是NewFile。 将url-pattern更改为 /添加