-2

I'm new to JSP. I'm trying to do a basic program of JSP & MySQL connectivity using sublime and xwampp. On running the login.jsp page, I get the following:

enter image description here

enter link description here

3 Answers3

1

A ClassNotFoundException thrown by your web application means that a class you referenced was not found in the runtime classpath. In your case, that class is com.mysql.jdbc.Driver. This is a bigger risk when you load a class explicitly by name at runtime, because the Java compiler cannot then check whether the wanted class is present. Of course, for JSP, compile time is not necessarily well distinguished from runtime.

Of course, you cannot just name a class and expect Java to automagically locate it in some unspecified place, retrieve it, and load it. You need to include the needed class and all its dependencies in your web application, or among the classes that your application server (i.e. Tomcat) provides to every web application.

For a third-party class such as the one in question, the usual approach is to obtain one or more Jar files containing the class and its dependencies, and to drop them in the lib/ directory of your web application's War file or deployment directory. Make sure that their ownership and permissions allow the application server to read them. You can find the needed Jar file at MySQL's web site. You're looking for their "Connector/J".

There is a separate issue of how to ensure that your build / packaging system automatically includes the needed Jar in your War or otherwise deploys it, but you've not presented anything in the question that allows us to address that.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

Possible errors:

1) You do not have access to the class com.mysql.jdbc.driver. You need to import it in Maven: https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.6

2) You are accessing to the request directly in JSP. As @Jens mentioned, this is not a good practice.

I guess you may have some code which actually render the JSP, something like this:

@Controller
public class IndexController {

    @RequestMapping("/login")
    public ModelAndView myindex() {
        return new ModelAndView("login.jsp");
    }
}

You may do all the heavy logic before rendering the JSP and pass to it just a map with the values. Something like this:

@Controller
    public class IndexController {

        @RequestMapping("/login")
        public ModelAndView myindex(HTTPServletRequest request) {
            Map<String, Object> data = new HashMap<>();
            data.put("userId", request.getParameter("userId"));
            ...
            // call mysql driver here for whatever reason...
            ...
            return new ModelAndView("login.jsp", data);
        }
    }

Then in your login.jsp you may just display the values of the map, not doing any logic like calling the SQL driver

Juan Bermudez
  • 430
  • 3
  • 6
-1

You are using request.getParameter() in the JSP.You have to use it in java file by taking HTTPServletRequest as a parameter in that particular method. And also you will be needing MysqlConnector.jar file in your classpath if you are connecting to MySql

Harsh Kumrawat
  • 160
  • 1
  • 2
  • 16