Question

I'm using eclipse and have added the jasypt 1.9.1 jar into my class path. When I tested BasicPasswordEncryptor in the main method, it works. However, when I used BasicPasswordEncryptor in another file (not containing the main method), I am thrown this error:

> 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)

My file:

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
    }
Was it helpful?

Solution

I see that the other file is a servlet. It doesn't contain the main method because it's a servlet. The fact that it's a servlet is much more relevant than the fact that it doesn't contain a main method.

When you have a servlet, its CLASSPATH is constructed from WEB-INF/lib (you put jars in there) and WEB-INF/classes. This is a basic fact about servlet programming.

And of course if a jar is not on your CLASSPATH, you will get a ClassNotFoundException.

So add the jasypt jar to WEB-INF/lib in your web project.

By the way, Eclipse can do this for you if you configure things correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top