When Datasource is configured in (tomcat 7) server.xml or in Context.xml, hibernate is not able to identify JNDI and throwing below excpetion but same is working fine when I tried to access datasource using JNDI in the code. What am doing wrong
Below is my configuration in hibernate.properties
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.pool_size 10
hibernate.dialect org.hibernate.dialect.MySQL5Dialect
hibernate.connection.datasource java:comp/env/jdbc/employee
hibernate.show_sql true
hibernate.format_sql true
in server.xml
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" resolveHosts="false"/>
**<Context docBase="hibernate" path="/hibernate">
<Resource name="jdbc/employee" auth="Container"
type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:45000/hibernatetest"
username="user" password="hibernate" maxActive="20" maxIdle="10"
maxWait="-1" removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true"/>
</Context>**
</Host>
In Context.xml file
**<Context docBase="hibernate" path="/hibernate">
<Resource name="jdbc/employee" auth="Container"
type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:45000/hibernatetest"
username="user" password="hibernate" maxActive="20" maxIdle="10"
maxWait="-1" removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true"/>
</Context>**
Finally below is my web.xml file
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
**<web-app >
<servlet>
<servlet-name>employee</servlet-name>
<servlet-class>edu.experiments.web.EmployeViewer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>employee</servlet-name>
<url-pattern>/employee</url-pattern>
</servlet-mapping>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/employee</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>**
org.hibernate.service.jndi.JndiException: Unable to lookup JNDI name [java:comp/env/jdbc/employee]
It is simpler than it may seem:
don't put anything about JNDI in your web.xml
in the context.xml you are using (whether global or webapp relative) use the proper name, say "jdbc/employee"
to hibernate configuration pass the name in the form "java:comp/env/jdbc/employee"
Here is the reference page:
http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html
Related
I am using apache apache-tomcat-8.5.6 for deploying my web applications. I have a webapps folder and a ROOT.war file under webapps and all my domains by default access this ROOT.war file.
Now I am planning to add two more folders similar to webapps, let's say folder1 and folder2 and planning to deploy ROOT.war under both these folders.
Now I want a certain domain to access ROOT.war under folder1 and another to access folder2. All other domains by default should access ROOT.war under webapps.
I believe that this is possible and might need some changes under the server.xml file. I am new to server configurations. Is there any good documentations or su
This is absolutely possible.
The key of this solution is that the Engine-Tag attribute "defaultHost" names the application-Host which is used by default and the Host-Tag attribute "name" contains the domain name which uses this application if it matches.
Example server.xml:
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource
auth="Container"
description="Database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
name="UserDatabase" pathname="conf/tomcat-users.xml"
type="org.apache.catalina.UserDatabase" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true" />
<Engine defaultHost="defaultHost" name="Catalina">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
<Host
name="defaultHost"
appBase="webapps-leave-empty">
<Valve
className="org.apache.catalina.valves.AccessLogValve"
directory="${catalina.base}/logs/access/"
rotatable="true"
buffered="true"
prefix=""
suffix="-access.log"
fileDateFormat="yyyyMMdd"
pattern='%a %t %v "%r" %s %b "%{Referer}i" "%{User-Agent}i" %I' />
<Context docBase="<PathToDefaultApplication>" path="" />
</Host>
<Host
name="<SpecialDomain1>"
appBase="webapps-leave-empty">
<Valve
className="org.apache.catalina.valves.AccessLogValve"
directory="${catalina.base}/logs/access/"
rotatable="true"
buffered="true"
prefix=""
suffix="-access.log"
fileDateFormat="yyyyMMdd"
pattern='%a %t %v "%r" %s %b "%{Referer}i" "%{User-Agent}i" %I' />
<Context docBase="<PathToApplication1>" path="" />
</Host>
<Host
name="<SpecialDomain2>"
appBase="webapps-leave-empty">
<Valve
className="org.apache.catalina.valves.AccessLogValve"
directory="${catalina.base}/logs/access/"
rotatable="true"
buffered="true"
prefix=""
suffix="-access.log"
fileDateFormat="yyyyMMdd"
pattern='%a %t %v "%r" %s %b "%{Referer}i" "%{User-Agent}i" %I' />
<Context docBase="<PathToApplication2>" path="" />
</Host>
</Engine>
</Service>
</Server>
I have a trouble with configure of Apache and Tomcat. My purpose is to get ability to adress to tui.org/servlet, when tui.org is my virtual host configured on local machine and /servlet/ is url-pattern which is defined at web.xml. But instead the servelet I get 404 Not Found by Tomcat but I can to adress to my server by tui.org/tui.org/servlet - it works. Structure of files on my server is the following:
Root: /var/www/tui.org
-------->/WEB-INF/classes/a
-------->/WEB-INF/classes/a/MainServlet.class
-------->/WEB-INF/web.xml
-------->/index.html
How can I achieve this behaviour?
web.xml:
<servlet-mapping>
<servlet-name>mainServlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
My server.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="/var/www/"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
Configuration of tui.org (tui.org.conf at /etc/apache2/sites-available:
<VirtualHost *:80>
ServerName tui.org
DocumentRoot /var/www/tui.org
<Directory /var/www/tui.org>
AllowOverride All
Require all granted
</Directory>
JkMount /servlet* ajp13_worker
</VirtualHost>
I use Apache2, tomcat9 and Ubuntu 18.04.
Thank you in advance!
P.S. Sorry for my english.
There are several issues here:
You must not serve the web application directory with Apache - e.g. you're declaring DocumentRoot /var/www/tui.org for Apache and <Host appBase="/var/www/"> for Tomcat.
e.g but not limited to: The servlet spec specifically disallows to serve the directory WEB-INF to ever be served to a client, and you're explicitly bypassing this limitation.
An application deployed in a Host's appBase is always deployed under that name (unless specified differently in a context.xml file). The only other exception to that rule is if a directory is called ROOT - in that case, it's deployed without a name, thus omitting the tui.org that follows your server name.
but this is secondary to the issue of serving the whole webapp through Apache httpd. Fix that one first.
One way to fix your biggest problem is to omit the DocumentRoot directive in Apache and rather serve everything from Tomcat: JkMount * ajp13_worker.
For solving problem with adressing by tui.org/servlet I had to add <Context> directive to server.xml:
<Context path="" docBase="/var/www/tui.org/"/>
and change appBase from /var/www/ to /var/www/tui.org/.
And for fix problem with access to WEB-INF (thanks to #OlafKock) I had to add next to tui.org.conf:
<Directory /var/www/tui.org>
AllowOverride All
Deny from all
</Directory>
To be precise I need to get pictures from an external directory say: C:/MWDB.
I found this link: How to config Tomcat to serve images from an external folder outside webapps? which I wanna follow up with the part where you need to add a directory in your server.xml context tag.
So Basically Based on the way it says I came with that it should be like:
<Context docBase="C:/MWDB" path="/images" reloadable="true" source="org.eclipse.jst.jee.server:Duck"/> (request: www.example.com/images/image.png) with this way while restaring the server it pops me an error saying
The server at localhost has encountered a problem
Which then I did this: <Context docBase="C:/MWDB" path="/images"/> And This I can't even access my index.html file
FULL server.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Server port="20011" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
<Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
<GlobalNamingResources>
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
</GlobalNamingResources>
<Service name="Catalina">
<Connector connectionTimeout="20000" port="20012" protocol="HTTP/1.1" redirectPort="8443"/>
<Connector port="20013" protocol="AJP/1.3" redirectPort="8443"/>
<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".txt"/>
<Context docBase="C:/MWDB" path="/images" reloadable="true" source="org.eclipse.jst.jee.server:Duck"/></Host>
</Engine>
</Service>
</Server>
I'm using Java 8, Apache Tomcat v9.0
Okay I figured out my problem.
Basically What I didn't know is that you could have multiple <context> at the same time.
So When I had something like:
<Context docBase="C:/MWDB" path="/images"/>
I could Server from only MWDB directory and not my internal Project and that is why I couldn't access my index.html file. So For to server from both directory Simply you can do:
<Context docBase="C:/MWDB" path="/images" reloadable="true" source="org.eclipse.jst.jee.server:Duck"/>
<Context docBase="C:/MWDB" path="/images"/>
Works Perfectly so far :)
With local tomcat my application url is: http://ip:port/myapp
but when deploying it to AWS Elastic Beanstalk the url becomes http://some_domain.com. I want the url to be http://some_domain.com/myapp.
How do I do that? I found something about adding some config file to .ebextensions but I wasn't sure what exactly I should do and whether it helps at all.
UPDATE:
I created an .ebextensions folder and put the the following files:
server.xml
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
<Listener className="org.apache.catalina.core.JasperListener"/>
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
<GlobalNamingResources>
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
</GlobalNamingResources>
<Service name="Catalina">
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log." suffix=".txt"/>
<Context docBase="EizeRest" path="/EizeRest" reloadable="true" source="org.eclipse.jst.jee.server:EizeRest"/>
</Host>
</Engine>
</Service>
</Server>
server-update.config
container_commands:
replace-config:
command: cp .ebextensions/server.xml /etc/tomcat7/server.xml
And when deploying I'm getting the following error:
"aws Unable to detect application deployment"
Here is a screenshot:
Another update:
I see in the events tab of the server the following error: cannot stat '.ebextensions/server.xml': No such file or directory
I believe you will need to take the following steps:
create a Tomcat server.xml file that sets path=myapp
copy this file to beanstalk during its deployment phase
Beanstalk documentation provides a relevant guide for that here:
http://aws.typepad.com/aws/2012/10/customize-elastic-beanstalk-using-configuration-files.html
Basically the guide says you need to copy the server.xml to beanstalk by creating the following file in your application root: .ebextenstions/tomcat.config:
container_commands:
replace-config:
command: cp .ebextensions/server.xml /etc/tomcat7/server.xml
don't forget to commit both the server.xml and the tomcat.config files in the .ebextensions folder.
You must place your ".ebextensions" folder at the same level as your WEB-INF folder. Doing that will remove the ".ebextensions/server.xml: No such file or directory" error.
I've just unpacked and configured Tomcat 8.0.3 on Windows 7 with a virtual host. I've tested it successfully under the default webapps, however, my virtual host is not recognizing changes in JSP files or classes.
Here is my set up:
%CATALINA_HOME%\conf\server.xml
...
<Engine name="Catalina" defaultHost="localhost">
...
<Host name="ww.virtual.com" appBase="vapps" unpackWARs="true" autoDeploy="true">
</Host>
</Engine>
...
%SystemRoot%\system32\drivers\etc\hosts
...
127.0.0.1 ww.virtual.com
%CATALINA_HOME%\vapps\ROOT\META-INF\context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiResourceLocking="true" privileged="true" reloadable="true">
<Resource name="jdbc/INVENTORY" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="sqlusr" password="secret1" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://127.0.0.1:1433;instanceName=SQLSERVER;DatabaseName=INVENTORY"/>
</Context>
%CATALINA_HOME%\vapps\ROOT\WEB-INF\web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<display-name>Test Display</display-name>
<description>Test Description</description>
<resource-ref>
<description>Test DB Connection</description>
<res-ref-name>jdbc/INVENTORY</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
I then have JSP files located in %CATALINA_HOME%\vapps\ROOT\ and classes in %CATALINA_HOME%\vapps\ROOT\WEB-INF\classes\MainPackage\ which are properly loaded when the server is started. But if I make any changes to the JSPs or .class files, the server does not recognize the update. What am I doing wrong?
This is happening because you have enabled anti-resource locking. With that option enable the web application is not deployed from the appBase but from a copy in the work directory. If you edit the copy in the work directory you'll see the changes.
Alternatively, remove the anti-resource locking options and fix whatever bug caused you to need them in the first place.