How could I get the name of session of current request?
The default name of tomcat session is "jsessionid". Given that I have modified the sessionCookieName configuration in context.xml of tomcat as below to gives multiple tomcat instance run on a same server
<Context sessionCookieName="appSessionId">
<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
</Context>
My web app need to know what the name of session of current request is. I have tried such as:
request.getServletContext().getSessionCookieConfig().getName()
request.getSession().getServletContext().getSessionCookieConfig().getName()
But both of them are null.
My pom.xml as below
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>8.0.23</version>
</dependency>
The tomcat I have used is also 8.0.23, any help is appreciated.
This is pretty confusing, because from what I can tell the following always return null, so I'm not sure what the point in them is!
request.getServletContext().getSessionCookieConfig().getName()
request.getSession().getServletContext().getSessionCookieConfig().getName()
You need to do:
SessionConfig.getSessionCookieName(request.getContext())
Related
title says it all. How do I setup a log4j2 configuration in my Open-liberty project? I've added my log4j2.xml file in the resources folder and I use the following dependency in my pom.xml:
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
This is my server.xml:
<server description="Intake Server">
<featureManager>
<feature>servlet-4.0</feature>
<feature>mpConfig-1.4</feature>
<feature>cdi-2.0</feature>
</featureManager>
<variable name="default.http.port" defaultValue="9080"/>
<variable name="default.https.port" defaultValue="9443"/>
<variable name="app.context.root" defaultValue="message"/>
<httpEndpoint httpPort="${default.http.port}"
httpsPort="${default.https.port}" id="defaultHttpEndpoint" host="*" />
<library id="log4jConfig">
<folder dir="/var/log/intake" scanInterval="5s" />
</library>
<webApplication id="intake" location="intake.war" contextRoot="${app.context.root}">
<classloader commonLibraryRef="log4jConfig"/>
</webApplication>
</server>
You'll also need to add log4j to the classpath, and there's several ways you can do that (with pros/cons to each, but I would suggest #2, unless you have more then one app that's going to be making use of log4j, in which case 3 may be the better approach):
Package it with your app (inside the war)
Add it in the server.xml via the classloader attribute under your webApplication
Drop it in the global shared library folder: ${shared.config.dir}/lib/global or ${server.config.dir}/lib/global
Set it via JVM argument: -Dlog4j.configurationFile=file:/path/to/log4j2.xml
I'll mention that there is also an archived repo regarding using log4j with Open Liberty that you may find helpful, but keep in mind that it is archived so it's likely incomplete and with no dev support.
How to deploy the same application to multiple contexts, each with a different database.
Using Tomcat7.0.54. We have 3 different environments like Development, Quality, Production. All the 3 environments have 3 different database configurations.
Normally apache-tomcat-7.0.54\conf\context.xml file. Sample D config.
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Resource name="java:jboss/datasources/letterDatasource"
auth="Container"
type="javax.sql.DataSource"
username="admin"
password="admin"
driverClassName="oracle.jdbc.OracleDriver"
url="xxx xxxx xxxxx xx"
maxActive="8"
maxIdle="4"/>
How to do the above programmatically or efficiently instead of changing in every server context.xml file.?
i am not using spring. just normal web application using seam 2.3 with jsf and richfaces.
Thank you.
Keep your data base properties in an environment.property file, which will be different for each stage. Specified environment will take their specific environmental properties.
I am trying to run appengine for Java project by following steps mentioned in https://cloud.google.com/java/getting-started/using-forms?authuser=3
To run the app on local machine I gave the command:
mvn -Plocal clean jetty:run-exploded -DprojectID=[YOUR-PROJECT-ID]
But I am getting following exception:
java.lang.IllegalStateException: Invalid storage type. Check if bookshelf.storageType property is set.
at com.example.getstarted.basicactions.ListBookServlet.init(ListBookServlet.java:62)
at javax.servlet.GenericServlet.init(GenericServlet.java:244)
at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:637)
at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:421)
at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:744)
I tried the same thing GCP Shell but I got the same exception.
What could be going wrong here?
snippet of web.xml
<!-- [START config] -->
<context-param>
<param-name>bookshelf.storageType</param-name>
<param-value>${bookshelf.storageType}</param-value>
</context-param>
snippet of pom.xml
<properties>
<!-- [START config] -->
<projectID>myProjectID</projectID> <!-- set w/ -DprojectID=myProjectID on command line -->
<bookshelf.storageType>datastore</bookshelf.storageType> <!-- datastore or cloudsql -->
<sql.dbName>bookshelf</sql.dbName> <!-- A reasonable default -->
<!-- Instance Connection Name - project:region:dbName -->
<!-- -Dsql.instanceName=localhost to use a local MySQL server -->
<sql.instanceName>${projectID}:us-central1:${sql.dbName}</sql.instanceName>
<sql.userName>root</sql.userName> <!-- A reasonable default -->
<sql.password>myRootPassword1234</sql.password> <!-- -Dsql.password=myRootPassword1234 -->
<!-- [END config] -->
Please clarify.
Thanks.
This issue was due to an error in Github repo (https://github.com/GoogleCloudPlatform/getting-started-java).
It is solved now. If you run into this, update to the last version of the repo.
for example app.war. I want to change root url for ruuning program on it.
I am absolutely confused. I tried out to specify root url in context.xml(in my app folder, and I've tried to add ROOT.xml. But it doesn't work(in general, I don't know where I make mistake of changing parametrs, I've looked over all tips. If i change in /web-inf context.xml, then tomcat removes all changing. Tomcat doesn't see ROOT.xml in specific folder. Please, may be someone can write more explicitly these steps and explain some things, which happened with me preventing my Tomcat from working correctly.
this is C:\Users\user\Desktop\psc\ProductCatalog\psc-ui\target\tomcat\conf\context.xml below
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Environment name="SQL.log" value="DEBUG" type="java.lang.String"/>
this is C:\Users\Alexander.Luchko\Desktop\psc\ProductCatalog\psc-ui\target\ProductCatalog\META-INF\context.xml below
<Context>
<Parameter name="javax.faces.PROJECT_STAGE" value="Production" override="false"/>
........
<Resource ....
sorry for my bad english! in advance
This problem is proving very tricky to resolve. Usual story, inherited a very poorly maintained java web application with no unit tests and a variety of ancient jar dependencies with no version info dumped in a lib directory built using Ant. In order to get better maintenance and understanding of the dependencies I migrated over to Maven. Subsequently, I realised the version of Spring was quite old (Spring 2.x and Spring Security 2.0.3). I upgraded successfully to Spring 2.5. I have now started migrating Spring up to 3.1.2.RELEASE, and Spring Security up to 3.1.3.RELEASE.
Everything compiles, I don't get any namespace issues either (declared in headers of Spring XML configs), but fails when being deployed as a WAR file into Tomcat Container. The log file reports:
Could not instantiate bean class [com.mydomain.repository.ReportDaoImpl]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/security/Authentication
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
I checked, and org.springframework.security.Authentication belongs to the old Spring Security jar (2.0.4)
My current Maven dependencies are as follows:
<spring.version>3.1.2.RELEASE</spring.version>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- SPRING SECURITY -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-acl</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-crypto</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
My security.xml is minimal:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<http auto-config='true'>
<intercept-url pattern="/**" access="ROLE_Admin" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="admin" password="password" authorities="ROLE_Admin" />
</user-service>
</authentication-provider>
</authentication-manager>
All my other Spring Config files use the following namespace headers in their configs:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
As far as I know this application contains NO annotations regarding Spring.
So how does the Spring Security 2.0.4 class org.springframework.security.Authentication get requested by a Spring 3.1 class org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory when instantiating a DAO object (which has nothing to do with Spring security settings). It may be that the DAO is picked because it is the first bean in order to get instantiated in the applicationContext.xml by the classloader (via Spring dependency injection container) but i cannot see how there is still a reference lurking somewhere in this application to an old 2.0.4 class. Since everything compiles ok, and the Maven pom only references 3.1 my take is that there must be some configuration somewhere still trying to pull in an old class. Anyone with Spring Security knowledge (in particular upgrading a large app from a version 2 to version 3) might have hit this problem before but I couldn't find exact match via google. Thanks for any suggestions on this. Currently stumped.
Quick update:
The applicationContext.xml has the namespace header as given above and the DAO is simply referenced as follows:
<bean id="reportDao" class="com.mydomain.repository.ReportDaoImpl">
<property name="dataSource" ref="myDataSource" />
</bean>
There is really nothing more to it. The applicationContext pulls in another context file applicationContext-datasource which declares (same namespace header again as above):
<bean id="parentDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
</bean>
<bean id="myDataSource" parent="parentDataSource">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
The ReportDao is over 1500 lines of poorly written procedural code. It is a POJO and implements rg.springframework.context.ApplicationContextAware. It also uses org.springframework.jdbc.core.support.JdbcDaoSupport for performing CRUD operations to the database.
Running mvn -X the output for dependencies (have switched to Spring [main] 3.0.7):
org.springframework:spring-webmvc:jar:3.0.7.RELEASE:compile
org.springframework:spring-asm:jar:3.0.7.RELEASE:compile
org.springframework:spring-beans:jar:3.0.7.RELEASE:compile
org.springframework:spring-expression:jar:3.0.7.RELEASE:compile
org.springframework:spring-web:jar:3.0.7.RELEASE:compile
org.springframework:spring-aop:jar:3.0.7.RELEASE:compile
aopalliance:aopalliance:jar:1.0:compile
org.springframework:spring-context:jar:3.0.7.RELEASE:compile
org.springframework:spring-context-support:jar:3.0.7.RELEASE:compile
org.springframework:spring-core:jar:3.0.7.RELEASE:compile
org.springframework:spring-tx:jar:3.0.7.RELEASE:compile
org.springframework:spring-jdbc:jar:3.0.7.RELEASE:compile
org.springframework.security:spring-security-acl:jar:3.1.3.RELEASE:compile
org.springframework.security:spring-security-config:jar:3.1.3.RELEASE:compile
org.springframework.security:spring-security-core:jar:3.1.3.RELEASE:compile
org.springframework.security:spring-security-taglibs:jar:3.1.3.RELEASE:compile
org.springframework.security:spring-security-web:jar:3.1.3.RELEASE:compile
org.springframework.security:spring-security-crypto:jar:3.1.3.RELEASE:compile
Looks ok I believe.
The only spring jars (grepped to be certain) in the WEB-INF/lib directory of the deliverable WAR file are:
./spring-aop-3.0.7.RELEASE.jar
./spring-asm-3.0.7.RELEASE.jar
./spring-beans-3.0.7.RELEASE.jar
./spring-context-3.0.7.RELEASE.jar
./spring-context-support-3.0.7.RELEASE.jar
./spring-core-3.0.7.RELEASE.jar
./spring-expression-3.0.7.RELEASE.jar
./spring-jdbc-3.0.7.RELEASE.jar
./spring-security-acl-3.1.3.RELEASE.jar
./spring-security-config-3.1.3.RELEASE.jar
./spring-security-core-3.1.3.RELEASE.jar
./spring-security-crypto-3.1.3.RELEASE.jar
./spring-security-taglibs-3.1.3.RELEASE.jar
./spring-security-web-3.1.3.RELEASE.jar
./spring-tx-3.0.7.RELEASE.jar
./spring-web-3.0.7.RELEASE.jar
./spring-webmvc-3.0.7.RELEASE.jar
Again, looks sensible.
Grepping the source code for "Authentication" did not help. This looks like a transitive runtime dependency issue. It goes unnoticed at compilation and isn't declared anywhere as a first level dependency. But somehow at runtime (in the Tomcat 6 container) when deployed a rogue reference to an old library file is requested.
Going to delete my Tomcat instance and start from scratch just as a precaution.
Ok, finally solved this particular problem. NoClassDefFoundError is exactly what it says on the tin. As Luke Taylor asked "Are you absolutely sure you recompiled?" Well, yes and no. I definitely recompiled, cleaned target (using Maven) etc. And the first time I used a decompiler to see the source for the generated class "XXXDao" I saw my changes. But I also noticed that when I added / removed a couple of lines from the java source, the error in the stacktrace remained on the same (random) line number. That explained I was getting a stale .class file somehow. Turns out, Maven and / or Eclipse (m2eclipse plugin) somehow managed to compile class files into the WEB-INF/classes directory of my core project (not target) and was non-deterministically overwriting the WEB-INF/classes directory in target with some classes. Even stranger, it ALWAYS deployed the old classes when compressing the classes into .WAR. So you have a case of the exploded contents looking correct but actually the compresssed WAR file with different classes.
Lesson learnt - if you get this sort of behaviour, double check the compilation steps, and try to find out how perhaps an old class file (referencing the old Authentication class) has gotten into your new build. Thanks for the pointers from those that contributed to the post!