Exception when forwarding request to servlet in Java web application - java

I have a problem and I don't know how to solve it.
My web application MyApp is a Maven web application project (Java 8 and Java EE 5). To run an updated version as test instance on the same server, I changed the Context Path of the application from MyApp to MyAppTest. As servlet container, I'm using Tomcat 8.
The MyApp instance is working as expected. However the MyAppTest instance is throwing an exception when forwarding the request to one of my servlets. Even if MyApp and MyAppTest are identical except the context path, this exception occurs. The Servlets are mapped via Guice's ServletModule.
Message:
java.lang.StringIndexOutOfBoundsException: String index out of range: -4
StackTrace:
java.lang.String.substring(Unknown Source)
com.google.inject.servlet.ServletDefinition$2.getPathInfo(ServletDefinition.java:212)
org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:376)
org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318)
myapp.base.MainEntryServlet.service(MainEntryServlet.java:49)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
com.google.inject.servlet.ServletDefinition.doServiceImpl(ServletDefinition.java:286)
com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:276)
com.google.inject.servlet.ManagedServletPipeline$1.doServiceImpl(ManagedServletPipeline.java:154)
com.google.inject.servlet.ManagedServletPipeline$1.forward(ManagedServletPipeline.java:140)
myapp.auth.LogoutServlet.service(LogoutServlet.java:57)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
com.google.inject.servlet.ServletDefinition.doServiceImpl(ServletDefinition.java:286)
com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:276)
com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:181)
com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91)
com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:85)
com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:120)
com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:135)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
org.apache.coyote.ajp.AbstractAjpProcessor.process(AbstractAjpProcessor.java:873)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Unknown Source)
The Servlet forwarding the Request does this:
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
if (!response.isCommitted()) {
dispatcher.forward(request, response);
}
The URL index.jsp is mapped to another Servlet. It forwards the request to a JSP:
request.getRequestDispatcher("/theme/index.jsp").forward(request, response);
There the exception occurs. Some other JSPs are included into the index.jsp like this:
<jsp:include flush="true" page="menu.jsp"/>
The menu.jsp is located in the same folder as the index.jsp.
As mentioned before, everything works fine as long as the Context path is MyApp. When changed to MyAppTest the exception occurs. It must have something to do with the Context path itself. When changing the Path to MyAppTest, the exception message is "String index out of range: -4". When I change the Path to MyAppTesting, the exception message changes to "String index out of range: -7".
To solve this I tried the following:
Changing the Context path and the docBase to MyAppTest in the context.xml of the project.
Changing the Artifact ID of the project to MyAppTest.
Changing the Project Name to MyAppTest.
Nothing worked. And I don't know what causes the exeption: Maven, Tomcat or Guice.

Related

Running Hbase with Java Servlets

I wrote a Hbase Java basic code that works fine with Java.
public class HBaseTest {
public static String newFunc() throws IOException {
// Instantiating configuration class
Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf("customer"));
Get g = new Get(Bytes.toBytes("jsmith"));
Result result = table.get(g);
// Reading values from Result class object
byte[] value = result.getValue(Bytes.toBytes("addr"), Bytes.toBytes("city"));
return Bytes.toString(value);
}
}
So I decided to use this code and Hbase as a part of my web application development and so I opened a new Dynamic Web Project in Eclipse Java EE IDE and added this file, changed the name of function and called it from one of the servlets. I also added the required jar files.
But I am stuck with the following error:
java.lang.ClassNotFoundException: org.apache.hadoop.hbase.HBaseConfiguration
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1332)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)
at HBaseTest.newFunc(HBaseTest.java:18)
at BakwasServlet.doGet(BakwasServlet.java:16)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
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)
This error is frustrating me now. Has anyone encountered such a case before or can anyone help me on this? I don't have hbase_home variable set up. I didn't need it for the java code as well.
Thanks in advance!
Where have you added the HBase JAR file? It should be in the WEB-INF/lib folder of your web application (or in the server's lib folder, but it's better to keep it within the app).

can't change or add action in struts2

i am having a problem with struts i've been working with struts with more than 4 month now
the problem is when i add a new action i get this:
the previous actions work fine but when i modify the name of the action in struts.xml i get the error i mention above.
so for example i have the following code in struts.xml
<action name="test1" class="com.onda.beans.AfficheVolList">
<result name="success">table_vols_test_css.jsp</result>
<result name="fail">admin.jsp</result>
</action>
when i enter http://localhost:8080/Myproject/test1 it works fine
but after i change the name of the action
<action name="test2" class="com.onda.beans.AfficheVolList">
<result name="success">table_vols_test_css.jsp</result>
<result name="fail">admin.jsp</result>
</action>
and i try to enter localhost:8080/Myproject/test2
i got the error in the previous image it doesn't even take me to admin.jsp
i tried to clean the server and clean work directory but nothing work i even changed the server it works for the first time but after i change the name of the action it doesn't work
here is the log
`Jun 01, 2016 12:24:58 PM org.apache.struts2.dispatcher.Dispatcher error
SEVERE: Could not find action or result
/Onda_vol/vols2
There is no Action mapped for namespace [/] and action name [vols2] associated with context path [/Onda_vol]. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:554)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
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)`
thank you for taking time to answer my question i really appreciate it
This is probably a problem in your Apache Tomcat server configuration.
Your struts.xml looks perfectly fine and there is no reason changing the name would break the action.
Here's a few things you could consider doing and that might solve your problem:
Restart Tomcat
Depending on your linux distro and Tomcat version, this might differ.
sudo service tomcat7 restart
Restart your computer
Delete and reinstall Tomcat
I hope this helps.

servlet init exception running jersey servlet on tomcat 7 [duplicate]

This question already has answers here:
What is the reason for UnsupportedClassVersionError?
(5 answers)
Closed 6 years ago.
i'm trying to deploy a rest application on tomcat7 that is running on ubuntu. After deploying the .war file using the application manager, when i access the url, its thrown me an exception.
It's important to say that, when i threw the .war on tomcat running on windows, it worked perfetly. I was running the latest versions of JRE and JDK.
I've tried everything i could find on the internet, but none worked so far. The error stack can be found below.
HTTP Status 500 - Servlet.init() for servlet Jersey Web Application threw exception
type Exception report
message Servlet.init() for servlet Jersey Web Application threw
exception
description The server encountered an internal error that prevented it
from fulfilling this request.
exception
javax.servlet.ServletException: Servlet.init() for servlet Jersey Web
Application threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
root cause
java.lang.UnsupportedClassVersionError:
br/unicamp/ft/courseviewer/resource/TodasDisciplinasResource :
Unsupported major.minor version 52.0 (unable to load class
br.unicamp.ft.courseviewer.resource.TodasDisciplinasResource)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:3111)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1348)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1828)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1709)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:278)
org.glassfish.jersey.internal.util.ReflectionHelper$6.run(ReflectionHelper.java:374)
org.glassfish.jersey.internal.util.ReflectionHelper$6.run(ReflectionHelper.java:369)
java.security.AccessController.doPrivileged(Native Method)
org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener$AnnotatedClassVisitor.getClassForName(AnnotationAcceptingListener.java:257)
org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener$AnnotatedClassVisitor.visitEnd(AnnotationAcceptingListener.java:219)
org.objectweb.asm.ClassReader.accept(ClassReader.java:1495)
org.objectweb.asm.ClassReader.accept(ClassReader.java:425)
org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener.process(AnnotationAcceptingListener.java:169)
org.glassfish.jersey.server.ResourceConfig.scanClasses(ResourceConfig.java:883)
org.glassfish.jersey.server.ResourceConfig._getClasses(ResourceConfig.java:840)
org.glassfish.jersey.server.ResourceConfig.getClasses(ResourceConfig.java:755)
org.glassfish.jersey.server.ResourceConfig$RuntimeConfig.(ResourceConfig.java:1171)
org.glassfish.jersey.server.ResourceConfig$RuntimeConfig.(ResourceConfig.java:1144)
org.glassfish.jersey.server.ResourceConfig.createRuntimeConfig(ResourceConfig.java:1140)
org.glassfish.jersey.server.ApplicationHandler.(ApplicationHandler.java:299)
org.glassfish.jersey.servlet.WebComponent.(WebComponent.java:311)
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:169)
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:359)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
This is my jdk version.
java version "1.7.0_101"
OpenJDK Runtime Environment (IcedTea 2.6.6) (7u101-2.6.6-0ubuntu0.15.10.1)
OpenJDK 64-Bit Server VM (build 24.95-b01, mixed mode)
Both JRE_HOME and JAVA_HOME are pointing to the right place /usr/lib/jvm/jre1.7.0_79 and /usr/lib/jvm/java-1.7.0-openjdk-amd64
You have a mismatch in Java version on your local machine and your Ubuntu server. I think you have Java 8 on your local machine that you used to generate your war file. You should either update Java on your server or compile with a JDK 7 to generate your war file. You can use your IDE to compile with a different version.

Using JNI so files on a dynamic web project(Apache Tomcat Server)

I am making a dynamic web project, which requires to use an external jar file. That jar uses several JNI so files. Now I have the jar and all the required so files. I have added the jar to the java build path, but I am not sure how to introduce the so files to the tomcat server.
I tried adding path to the so file as "LD_LIBRARY_PATH":
(Properties->Run/Debug Settings->edit->Environment->Select) and also
adding the path to "java.library.path"(Run
As->RunConfigurations->Tomcat v8 Server->Arguments->VM arguments)
flag.
Still it is not working, I am getting below error, whenever the my code is trying to access functions/classes from the above mentioned jar:
SEVERE: Servlet.service() for servlet [org.iotivity.simulator.agent.LaunchSimulator] in context with path
[/SimulatorAgent] threw exception [Servlet execution threw an
exception] with root cause java.lang.ClassNotFoundException:
org.oic.simulator.server.SimulatorResource$Type at
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
at
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1139)
at
org.iotivity.simulator.agent.LaunchSimulator.doPost(LaunchSimulator.java:86)
at
org.iotivity.simulator.agent.LaunchSimulator.doGet(LaunchSimulator.java:45)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
at
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
at
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502)
at
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458)
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)
I have tried the same code with a simple Java Application, and it works fine by setting the LD_LIBRARY_PATH variable.
Any suggestion will be highly appreciated.
Your error is a ClassNotFoundException, which would suggest that the JAR isn't properly included. If there was a problem with the native library, you'd get a UnsatisfiedLinkError instead. To make the JAR available to Tomcat, you'd need to add it to the classpath (-classpath=/path/to/your.jar) or include it in your WAR. The latter depends on your build system, but if you use Maven it's rather easy using the maven-shade-plugin (this only works for the JAR though, the native code you'll have to keep on distributing separately). Hope that helps!

MySql Driver Class Autoloading

Configuration
jdk version : 1.8.0_60-b27
project : maven jsp project
Mysql connector : mysql-connector-java-5.1.38 (Copied to Tomcat's lib)
Pom.xml
<!--MySql Connector/J Dependency-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
<scope>provided</scope>
</dependency>
Code Causing Error :
try(Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/myDatabase","root","")){
connection=con;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
Error log
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/myDatabase
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at util.DBConnectivity.getDBConnection(DBConnectivity.java:16)
at controller.MyController.doGet(MyController.java:25)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2503)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2492)
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)
I tried both of the scopes [provided and compile] in pom.xml but no change in the output!!!
Tried hundred times but can't figure out why project is not able to find the driver????
What you need to do is just these two steps, and not more:
Include your dependency properly:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
Notice that scope is not configures, so by default it will be compile, which will make it available at runtime too.
The combination of manually copying this file to WEB-INF\lib then redeploy with scope=provided will not work because the folder will get recreated. In case you would like to make this jar available only at runtime, you need to place it to Tomcat\lib.
You need to manually load the driver before you attempt to connect anywhere. Make sure you do this before using DriverManager.
Class.forName("com.mysql.jdbc.Driver");
you are not getting the connection from a datasource hence you are not using the mysql driver in the tomcat lib.
Using DriverManager you need to load the jdbc library yourself using Class.forName("com.mysql.jdbc.Driver").newInstance(); and by removing the provided entry for it in the pom.xml or... use a datasource :)

Categories