我正在使用 eclipse 并将 jasypt 1.9.1 jar 添加到我的类路径中。当我在 main 方法中测试 BasicPasswordEncryptor 时,它可以工作。但是,当我在另一个文件(不包含 main 方法)中使用 BasicPasswordEncryptor 时,我抛出了这个错误:
> java.lang.ClassNotFoundException: org.jasypt.util.password.BasicPasswordEncryptor
> at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
> at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
> at com.javux.servlets.RegisterServlet.processRequest(RegisterServlet.java:75)
> at com.javux.servlets.RegisterServlet.doPost(RegisterServlet.java:47)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
> at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
> at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
> at
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
> at
> org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
> at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
> at
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
> at
> org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
> at
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
> at
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
> at
> org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
> at
> org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
> at
> org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:304)
> at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
> at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
> at java.lang.Thread.run(Thread.java:722)
我的文件:
package com.javux.login;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.jasypt.util.password.BasicPasswordEncryptor;
@SuppressWarnings("serial")
public class RegisterServlet extends HttpServlet {
private Connection connection;
private PreparedStatement allUsers,insertUser;
private Pattern pattern;
private Matcher matcher;
private static final String EMAIL_PATTERN =
"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
public RegisterServlet() {
pattern = Pattern.compile(EMAIL_PATTERN);
}
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
processRequest(req,res);
}
public void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
processRequest(req,res);
}
public void init(ServletConfig config)
throws ServletException {
// attempt database connection and create PreparedStatements
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ecoplanning", "root", "");
//PreparedStatement to upload to database
allUsers = connection.prepareStatement("SELECT email FROM users");
insertUser = connection.prepareStatement("Insert INTO users (email,password,air_factor," +
"ocean_factor,land_factor) values (?,?,?,?,?)");
}catch (Exception exception) {
exception.printStackTrace();
throw new UnavailableException(exception.getMessage());
}
} // end of init method
public void processRequest(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String email = req.getParameter("email").trim();
String password = req.getParameter("password").trim();
//encrypting the password
BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
String encryptedPassword = passwordEncryptor.encryptPassword(password);
matcher = pattern.matcher(email);
boolean matchEmail = matcher.matches();
try{
boolean valid = false;
if(matchEmail==true){
//Get all user from database
ResultSet users = allUsers.executeQuery();
while (users.next()){
String allEmails = users.getString("email");
if (email.equals(allEmails)){
valid=true;
break;
}
}
}else{
valid = true;
}
//Insert into new user into database
if(!valid){
insertUser.setString(1, email);
insertUser.setString(2, encryptedPassword);
insertUser.setString(3, "746.4");
insertUser.setString(4, "9.2");
insertUser.setString(5, "93.8");
insertUser.executeUpdate();
String msg = "Your Account Has Been Successfully Created!";
req.setAttribute("message", msg);
RequestDispatcher view = req.getRequestDispatcher("login.jsp");
view.forward(req,res);
}else{
String errorMsg = "Email provided is Invalid.";
req.setAttribute("message", errorMsg);
RequestDispatcher view = req.getRequestDispatcher("registerAcc.jsp");
view.forward(req,res);
}
}catch(SQLException sqlException){
sqlException.printStackTrace();
}
}
public void destroy() {
// attempt to close statements and database connection
try {
allUsers.close();
insertUser.close();
connection.close();
} // handle database exceptions by returning error to client
catch (SQLException sqlException) {
sqlException.printStackTrace();
}
} // end of destroy method
}