Configure EJB3 with web.xml like parameters - java

I'd like to pass some parameters to a stateless session bean. I'd like to do something like I do when I configure a Servlet initial parameters in the web.xml file. Thanks

It is traditional to use a property file for this.

Related

I want to using #WebServlet annotation and #Path annotation to same time in jetty

First, Please forgive me for my clumsy English.
[What I want to do]
I want to know #WebServlet annotation by Servlet 3.0 and #Path annotation by Jersey 2.22.2, It is able to using same time?
[That I want is to help]
Can I use two annotations at the same time?
If I can use those annotations, is that way how to use?
Thank you.
#Path annotation defines a path to a RESTful Web service so when you have #Path("/SomeService") it will translate into www.yourapp.com/baseRestUrl/SomeService. You can also define it on the methods which provides REST services. Note that baseRestUrl is defined inside web.xml or in class which extends Application class.
On the other hand #WebServlet("/SomePath") states that Servlet will be listening for request on the www.yourapp.com/SomePath, it is basically replacement of servlet-mapping element in web.xml. You can still configure servlets like this, it's up to you whether you prefer XML or annotation configuration.

Calling a method with using bean and reflection

I have some classes and i want declare these classes in bean xml file. I want to call methods from these classes with reflection. I mean i have two feature in bean tag, one 'id' and another 'class', I want when take id value to a methods in reflection, find class and call special method. anyway, i searched about java beans, reflection and etc but i cant use these and i want to see a simple tutorial because my knowledge is very basic and little :(
How can i use bean xml file in reflection for calling a method from special class that the class declare in bean file?
for using bean xml file, i should install spring framework? and where should i create xml file with beans tag?(i mean i should create this in special directory, like below src or below res...)
Thanks for advises
For spring web application,By default you can declare beans in
/WEB-INF/applicationContext.xml
You can also declare beans in someName.xml in separate folder inside /WEB-INF/ like
/WEB-INF/config/database.xml
which could be imported in /WEB-INF/applicationContext.xml or other like
<import resource="/WEB-INF/config/database.xml"/>
and register ApplicationContext using ContextLoaderListener in web.xml as follows:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
for more information look spring docummentation.
http://docs.spring.io/spring/docs/1.2.9/reference/beans.html
if you are doing android then there is spring-android library but it doesn't seems to help you.
Why should I use Spring Android?
you might need to write your bean declaration file as plain xml inside resource folder and
res/xml/
see:
http://developer.android.com/guide/topics/resources/providing-resources.html
additionally you will have to implement your logic reading beans from that xml file.
From the Spring page:
Features
A Rest Client for Android
Auth support for accessing secure APIs
Spring android does not have bean configuration. Why do you need it? What you want may be done with generics and reflection. Could you provide an example of why you want that?

Externalizing links in servlets to a constant

I have lots of servlets that redirects through
response.sendRedirect(URL)
And URL has a absolute path starting with http://localhost:8080.
Well, I need to change URL so I make this server online, but don't want to change
all instances of URL in all servlets.
Needed to know if there's a way to externalize this constant neatly.
Thanks in advance.
You can either use a Properties file or make use of context parameters in your web.xml. Using properties is preferred if you would like to later provide language translations for your web application. If there are only a few constants that need to be externalised like the server url and admin email etc. context parameters is an equally good choice.
<context-param>
<param-name>server-hostname</param-name>
<param-value>http://www.domain.com/</param-value>
</context-param>
You can access this parameter globally from any Servlet as
response.sendRedirect(
getServletContext().getInitParameter("server-hostname") + "page.php"));

Session aware spring bean

Is there any way to define a spring bean which will be notified when data in session has changed?
I would also like to know pure java solution if possible. All I want is when i add/edit/delete data in httpsession then I want one java class to be notified to do some processing on that data.
Thanks
You don't need Spring for this, the Servlet API provides that out-of-the-box, via the HttpSessionAttributeListener interface:
This listener interface can be implemented in order to get notifications of changes to the attribute lists of sessions within this web application.
You declare it as a <listener> in your web.xml file. See here for an example.
In don't know of a specific Spring-friendly way of doing this, though, I think you'll have to use the above listener approach, and notify your Spring beans from there.

Accessing context-param values from within a RESTEasy provider

I have created a MessageBodyReader/MessageBodyWriter that needs to
read a configuration value. Ideally I'd like this to be held in the
web.xml as a context-param. Is there any way that a RESTEasy provider
can access context params? Is there any type that I can inject using
#Context that will allow me to get context-param values? I haven't
been able to find one.
Alternatively, is there a better way to provide configuration values
to a provider? I'd like to avoid having to use a system property.
In answer to my own question, one solution is to use Spring.
When RESTEasy beans are created via the the Spring context, one can easily provide configuration values in the application-context.xml. It's even possible to use context-param values from the web.xml using the ServletContextPropertyPlaceholderConfigurer.

Categories