As a result of my previous question I followed these steps to include Apache PDFbox without maven:
downloaded .jars from download site of PDFbox (all under Libraries of each subproject)
downloaded .jars from dependencies of PDFbox from maven repository (commons-logging and fontbox, but fontbox was already included in step 1)
placed the downloaded .jars in a folder (C:\PDFjars)
configured build path in eclispe (added all .jars as external libraries) It now looks like this:
My servlet:
package servlets;
// other imports
import org.apache.pdfbox.pdmodel.PDDocument;
#WebServlet("/print")
public class PrintServlet extends HttpServlet {
public PrintServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// From a PDFbox Tutorial
//Creating PDF document object
PDDocument document = new PDDocument();
//Saving the document
document.save("C:/PdfBox_Examples/my_doc.pdf");
System.out.println("PDF created");
//Closing the document
document.close();
doGet(request, response);
}
}
So when I start my web project and call the function the should produce a simple PDF document, I get folloing error:
Servlet.service() for servlet [servlets.PrintServlet] in context with path [/StaticsCalculator] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.apache.pdfbox.pdmodel.PDDocument
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1291)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
at servlets.PrintServlet.doPost(PrintServlet.java:44)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Solution: I had to copy the .jars in WEB-INF/lib folder as well.
Download the jar file by itself and add it to your code dependency. You can search for any Maven file on Maven Repository. If you're using Eclipse, just modify the build path to point to an external jar and give it the location of your jar. This will not take care of any dependencies your library requires, which leads into dependency hell where you'll have to read the pom and download each library manually.
You can
a) use a local maven repository and place the dependencies there.
b) add the dependencies in the classpath of the application - that's basically what maven does for you.
Related
I am working on a dynamic web project using JSP and multiple servlets with tomcat Apache and Eclipse.
I was having multiple problems with Eclipse today and I ran a clean on the project to try to fix some of the issues that I was having and ever since, I'm getting the following error every time a servlet is called.
SEVERE: Allocate exception for servlet [RegisterServlet]
java.lang.ClassNotFoundException: com.ogi.controller.RegisterServlet
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1372)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1195)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:545)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:526)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:150)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1044)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:763)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:544)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:698)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:616)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:831)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1629)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:830)
This project was working completely fine on localhost before the clean and I have all of the servlets defined in my web.xml file. CATALINA has an environmental variable and I already have the Jar files in my project.
I'm not sure why these errors are occurring and would appreciate some direction on how to attempt to fix this.
I have created a ZK web application project in Eclipse, and deployed the same in Apache Tomcat 9.0.37. And I have a properties file to be read(aws.properties) - which is located right under WEB-INF.
I am having difficulties loading the properties file - getting a NullpointeException
Eclipse ZK Project
Project Structure:
I tried all the 3 approaches below and all of them throws a java.lang.NullPointerException: inStream parameter is null at the line PROPERTIES.load(inputStream);
Code:
private static final String PROPERTIES_FILE = "aws.properties";
private static final Properties PROPERTIES = new Properties();
...
...
...
// APPROACH 1.
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
// APPROACH 2.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(PROPERTIES_FILE);
// APPROACH 3.
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(PROPERTIES_FILE);
PROPERTIES.load(inputStream);
Stacktrace:
java.lang.NullPointerException: inStream parameter is null
at java.base/java.util.Objects.requireNonNull(Objects.java:246)
at java.base/java.util.Properties.load(Properties.java:404)
at in.net.cadentic.core.database.AmazonDynamoDBClientHandler.createNewClient(AmazonDynamoDBClientHandler.java:75)
at in.net.cadentic.core.dao.UsermasterDao.findBypassword(UsermasterDao.java:27)
at in.net.cadentic.composers.LoginComposer.onClick$loginButton(LoginComposer.java:71)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.zkoss.zk.ui.event.GenericEventListener.onEvent(GenericEventListener.java:80)
at org.zkoss.zk.ui.AbstractComponent.onEvent(AbstractComponent.java:3184)
at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:3154)
at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:3096)
at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:138)
at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:353)
at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:377)
at org.zkoss.zk.ui.AbstractComponent$ForwardListener.onEvent(AbstractComponent.java:3629)
at org.zkoss.zk.ui.AbstractComponent.onEvent(AbstractComponent.java:3184)
at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:3154)
at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:3096)
at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:138)
at org.zkoss.zk.ui.impl.UiEngineImpl.processEvent(UiEngineImpl.java:1851)
at org.zkoss.zk.ui.impl.UiEngineImpl.process(UiEngineImpl.java:1623)
at org.zkoss.zk.ui.impl.UiEngineImpl.execUpdate(UiEngineImpl.java:1326)
at org.zkoss.zk.au.http.DHtmlUpdateServlet.process(DHtmlUpdateServlet.java:611)
at org.zkoss.zk.au.http.DHtmlUpdateServlet.doGet(DHtmlUpdateServlet.java:487)
at org.zkoss.zk.au.http.DHtmlUpdateServlet.doPost(DHtmlUpdateServlet.java:495)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:834)
Question:
What is being done incorrectly here?
Your properties file belongs in WEB-INF/classes. The class loader will not look for individual files in WEB-INF, because the servlet specification says it should not look for individual files there.
In addition to VGR's response a .properties file can be read from within the WEB-INF folder directly. Instead of loading it from the classpath via classloader it can be read from a ServletContext object.
That assumes you have access to a ServletContext object. The example below shows how to obtain the ServletContext from via WebApps.getCurrent() instance.
// using ZK specific WebApps util
ServletContext servletContext = WebApps.getCurrent().getServletContext();
// Servlet API
InputStream inputStream = servletContext.getResourceAsStream("/WEB-INF/aws.properties");
Properties myProps = new Properties();
myProps.load(inputStream);
A shortcut would be to call WebApps.getCurrent().getResourceAsStream("/WEB-INF/aws.properties") directly.
Still I'd prefer to keep the config file in the classpath and not to make the Dao-class dependent on the existence of a ServletContext. However your code example wasn't specific about at which layer your configuration is being read.
Edit:
I found the answer and I am posting it here in case anybody googles it in future: Turns out that Tomcat does not look up external libraries in Eclipse and they should be copied to [TomcatInstallDir]/lib.
Here is my code (which is for a servlet running on Tomcat server):
import com.google.gson.Gson;
public class AddCourse extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException{
Gson gson = new Gson();
//Some code
}
I have tried to export other libraries as well as same library with different version and had the same problem. Here is a picture of Build path:
I read that when writing a Dynamic Web Application (Which I am doing) then we should put the jars under Web-Content/lib, I did that also and it did not help.
Here is the stacktrace:
Apr 19, 2020 5:27:34 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [AddCourse] in context with path [/MTHDB] threw exception [Servlet execution threw an exception] with root cause
java.lang.Error: Unresolved compilation problems:
Gson cannot be resolved to a type
Gson cannot be resolved to a type
at servlets.AddCourse.doPost(AddCourse.java:26)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:688)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Edit:
I noticed that my MANIFEST.MF' contents looks like this:
Manifest-Version: 1.0
Class-Path:
Should not there be a mention the the JARs?
I found the answer and I am posting it here in case anybody googles it in future: Turns out that Tomcat does not look up external libraries in Eclipse and they should be copied to [TomcatInstallDir]/lib.
I want to have access to static resources outside my webapp so that they don't get destroyed every time I redeploy, but I can't seem to get them
I have a servlet that is meant to serve these images via a FileOutputStream, but it can't get the file when I deploy it on my tomcat server - it gives me a java.nio.file.AccessDeniedException.
Error from Catalina Log File
13-Feb-2020 16:53:10.621 SEVERE [http-nio-8080-exec-6] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [fileGetter] in context with path [] threw exception
java.nio.file.AccessDeniedException: C:\Users\micro\Pictures\WebsiteStuff\BlogPosts\Test Image 2020-02-12.jpg
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:89)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:235)
at java.base/java.nio.file.Files.newByteChannel(Files.java:374)
at java.base/java.nio.file.Files.newByteChannel(Files.java:425)
at java.base/java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:420)
at java.base/java.nio.file.Files.newInputStream(Files.java:159)
at java.base/java.nio.file.Files.copy(Files.java:3195)
at ImageGetterServlet.doGet(ImageGetterServlet.java:37)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:609)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:810)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1623)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:830)
This is especially strange given that I have allowed permission to the directory in my Catalina.policy file.
Catalina.Policy
grant {
...
permission java.io.FilePermission "C:${file.separator}Users${file.separator}micro${file.separator}Pictures${file.separator}WebsiteStuff", "read, write";
};
For whatever reason, this exact same code (minus the permission) works perfectly in Intellij. However, here it is not able to access the file.
Could anyone let me know why this is happening? Did I set the wrong permission/put the permission in the wrong place?
EDIT: It is NOT a permission problem
I granted everything in Catalina.policy java.security.AllPermission but to no avail, I’m getting the same exception.
I found out what was wrong. Tomcat cannot access anything inside the Users folder - probably a Windows 10 specific problem.
I just put my directory in question in the C: drive so that the path is now C:/WebsiteStuff and everything worked fine.
I am using Intellij for a servlet project and using maven
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
as far as i know, this should handle all the classpath related issues when deploying the war on the tomcat webserver.
but i m getting
08-Apr-2017 13:40:37.047 INFO [localhost-startStop-1]
org.apache.catalina.startup.HostConfig.deployDirectory Deploying web
application directory /Applications/tomcat/webapps/manager 08-Apr-2017
13:40:37.092 INFO [localhost-startStop-1]
org.apache.catalina.startup.HostConfig.deployDirectory Deployment of
web application directory /Applications/tomcat/webapps/manager has
finished in 44 ms java.lang.ClassNotFoundException:
com.mysql.jdbc.Driver at
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285)
at
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
at java.lang.Class.forName0(Native Method) at
java.lang.Class.forName(Class.java:264) at
com.saurabh.testdb.TestDBServlet.doGet(TestDBServlet.java:34) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:635) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:742) at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341)
at
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
at
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798)
at
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1441)
at
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Here is my code for loading the driver
package com.saurabh.testdb;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
#WebServlet("/TestDBServlet")
public class TestDBServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//set up connection variable
String jdbcUrl = "jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false";
String user = "springstudent";
String pass = "sringstudent";
String driver = "com.mysql.jdbc.Driver";
try {
PrintWriter out = resp.getWriter();
out.println("Connecting to database: " + jdbcUrl);
Class.forName(driver);
Connection myConn = DriverManager.getConnection(jdbcUrl, user, pass);
out.println("Connection successful!!!");
myConn.close();
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
You have to add your jdbc_connector to your tomcat server manually, you should to include your jar to this folder $TOMCAT_HOME/lib
Just reproduced your bug. Seems like the name of the driver has changed in the new version. When you are downloading maven dependencies better be taking the latest Stable version - so you can have a better and faster support from the communities. `com.mysql.cj.jdbc.Driver'
That is the error msg I've got:
Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
Answer for IntelliJ ultimate edition
You can add your JDBC connector jar file to run configuration:
Add the jar file
Or simply you can add the jar to $TOMCAT_HOME/lib
Generally, the default path is C:\Program Files\Apache Software Foundation\Tomcat 9.0\lib
Invalidate caches and restart
Click here on how to invalidate caches