所以我正在编写一个简单处理我的表单的servlet。表单实际上是一个注册页面,只有附加的文件输入。因此,我需要将其作为multipart和method post。 servlet使用doPost方法来检索信息。另一方面,servlet还从其他包中的另一个类检索信息。该类是读取属性文件。我已经完成了我的研究,浏览互联网以及官方页面上的文档。任何人都可以帮助我吗?
servlet代码
package net.codejava.upload;
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.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import readConfig.readConfig;
@WebServlet("/uploadServlet")
@MultipartConfig(maxFileSize = 16177215) // upload file's size up to 16MB
public class FileUploadDBServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
readConfig readcfg= new readConfig();
// database connection setting
String dbURL = readcfg.getProperties("conUrl");
String dbUser = readConfig.getProperties("dbUser");
String dbPass = readConfig.getProperties("dbUser");
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// gets values of text fields
String name = request.getParameter("name");
String address = request.getParameter("address");
String city = request.getParameter("city");
String email = request.getParameter("email");
String password = request.getParameter("password");
System.out.println(dbURL);
System.out.println(dbUser);
System.out.println(dbPass);
InputStream inputStream = null; // input stream of the upload file
// obtains the upload file part in this multipart request
Part filePart = request.getPart("file");
if (filePart != null) {
// prints out some information for debugging
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
// obtains input stream of the upload file
inputStream = filePart.getInputStream();
}
Connection conn = null; // connection to the database
String message = null; // message will be sent back to client
try {
// connects to the database
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
// constructs SQL statement
String sql = "INSERT INTO ssl_user (name, address, password,city,email,file) values (?, ?, ?, ?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, name);
statement.setString(2, address);
statement.setString(3, password);
statement.setString(4, city);
statement.setString(5, email);
if (inputStream != null) {
// fetches input stream of the upload file for the blob column
statement.setBlob(6, inputStream);
}
// sends the statement to the database server
int row = statement.executeUpdate();
if (row > 0) {
message = "File uploaded and saved into database";
}
} catch (SQLException ex) {
message = "ERROR: " + ex.getMessage();
ex.printStackTrace();
} finally {
if (conn != null) {
// closes the database connection
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
// sets the message in request scope
request.setAttribute("Message", message);
// forwards to the message page
getServletContext().getRequestDispatcher("/Message.jsp").forward(request, response);
}
}
}
读取属性的类
package readConfig;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class readConfig {
//call this method from jsp
public static String getProperties(String key) throws IOException
{
InputStream inputStream=readConfig.class.getClassLoader().getResourceAsStream("config.properties");
Properties myproperties = new Properties();
/////////////load the properties file
myproperties.load(inputStream);
return myproperties.getProperty(key);
}
}
答案 0 :(得分:0)
您应该在错误日志中分享您所获得的内容,以便我们可以更好地帮助您。我可以假设您在尝试连接到数据库时必须获得NullPointerException。 1>在config.properties文件中,您应该为键设置值,如下所示(这只是一个示例)
dbURL=jdbc:mysql://localhost:3306/mydb
dbUser=root
dbPass=root
2>在你的Servlet类中,我假设你在变量dbPass中设置了错误的值
从
更改String dbPass = readConfig.getProperties("dbUser");
到
String dbPass = readConfig.getProperties("dbPass");
您在密码变量中设置用户名
3>这只是一个建议 类的名称应以大写字母开头(在您的代码类readConfig中)
答案 1 :(得分:0)
所以我想出了我的代码的错误。由于getProperties类是实现IOException,因此默认构造函数无法读取它。因此,解决方案只是移动doPost类中的数据库连接设置。哈哈,谢谢。