Event-driven programming can be overwhelming for beginners, which can make Node.js difficult to get started with. But don’t let that discourage you; In this article, I will teach you some of the basics of Node.js and explain why it has become so popular. To start using Node.
In this post I will explain you one simple way of using interceptors in Java EE.
We have a simple java project that only displays a name wich is setted in the Controller class by the
init method. But as you will see next, the final result it’s not the name that is setted by the controler, because we have an interceptor that changes the name behind the scenes.
The index page
index.xhtm makes a server request to display the name. But that request is going to be intercepted by our interceptor
MyInterceptor.java. Then who makes the request to the controller is the interceptor and if the response is of the type String it changes it to another string. When the page renders it will show not the response from the controller but the object that our interceptor returns to it.
And that’s it. Even without the final user knows nothing about it, the response has been manipulated by our interceptor.
Project Scheme
interceptor_project
index.xhtml
index.xhtml
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version='1.0'encoding='UTF-8'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
Note that in line 10 we have a special annotation. This marks this bean to be intercepted, as you will see next. The code in the 20th line, just sets the variable name to John Doe.
LogToConsoleAndModifyResult.java
LogToConsoleAndModifyResult.java
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
packagept.joaobrito.annotation;
importjava.lang.annotation.ElementType;
importjava.lang.annotation.Inherited;
importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
importjava.lang.annotation.Target;
importjavax.interceptor.InterceptorBinding;
@Inherited
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@InterceptorBinding
public@interfaceLogToConsoleAndModifyResult{
}
This is a simple annotation class (out of the scope of this post, for now). The important thing here is the
@InterceptorBinding annotation.
In the MyInterceptor class we have 3 important annotations:
@Interceptor (line 10), @LogToConsoleAndModifyResult (line 11) and the @AroundInvoke (line 14).
The first one makes this bean an interceptor. The second, marks this interceptor to intercept all methods in classes annotated with the the same annotation. The last one, marks the method that is going to manipulate all the data.
Note that this method must returns an
Object and as a parameter it takes the invocation context.
First we have a logger that prints out to the console a message before the interception. Then with the help of the invocation context, it lets the original method to proceed and keeps the result. Then it prints again to the console the real name returned by the controller (John Doe). After this, it changes the return value to Jane Doe, that is exactly what is going to be displayed in the page as the final result.
Finally, in order to use interceptors we must register them in the beans.xml file (line 3). Another possibility is to annotate the controller with
@Interceptors({MyInterceptor.class}) but this approach is not recommended.
The Log
Here are the three messages sent to the console.
The result page
As you can see here the displayed name isn’t John Doe, as you might expect, but the modified result: Jane Doe.
In this post I will show you how to inject an instance of Logger class using CDI (Context Dependency Injection) and SLF4J (Simple Logging Facade for Java).
To keep it simple, we only have a xhtml page (index), a managed bean (Controller.java) and the producer (LoggerProducer.java). The project configuration is as follows:
project
As we are using JSF with facelets, the index page looks like:
index.xhtml
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version='1.0'encoding='UTF-8'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
In this file we simply display the name that is setted in the controller (line 10).
Controller.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
packagept.joaobrito.controller;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.slf4j.Logger;
@Named
@RequestScoped
publicclassController{
@Inject
privateLogger logger;
privateStringname;
publicController(){
}
@PostConstruct
publicvoidinit(){
logger.info("before setting the name: name = "+name);
this.name="John Doe";
logger.info("the name was setted to: name = "+name);
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
}
In the controller we inject (@Inject) an instance of the Logger class and print to the console the name before and after the name is setted (lines 23 and 25). CDI knows which Logger to inject because we have a class that works as a factory of Loggers (LoggerProducer) with some special annotations.
In the LoggerProducer class we have an annotation (@Produces) in a method that returns a Logger instance. This is enough to CDI inject a Logger when needed. Notice that in line 13 we pass as a parameter the injection point (ip) in order to get the class where the logger is being injected (in this specific case ‘Controller.class’). This is only necessary beacuse the getLogger method needs it.
Finally, here are the results (the log and the web page):
log
As you can see here, we have two lines displaying the information that we expected: first the name is null and after the set of the variable name, name = John Doe.
the result page
That’s it. It’s just that simple!
Conclusion
First we create a factory of loggers, then we inject them whenever we need them: @Produces to indicate CDI what we are producing and @Inject to inject the instance ready to use.
Notice that this methodology works with other types rather than Logger. You may inject whatever you want by simply following the above technic.
In this article I will try to explain you how to use the flash scope in Java EE.
Introduced in JSF 2.0, the flash scope provides a short-lived conversation (flash session). It is a way to pass temporary objects (not only strings) that are propagated across a single view transition and cleaned up before moving on to another view. The flash scope can only be used programmatically as there is no annotation.
In some cases we need to redirect from one view to another view in JSF. But when we do a redirect we lost everything that is stored in the url with a GET request. One solution is to use an EL expression of the abstract class Flash #{flash.foo}. So, this is a pretty simple solution to redirect without lost anything:
Source files:
index.xhtml
index.xhtml
The special thing here is the use of #{flash.foo}, where foo is the name of the variable in the flash scope.
ExampleController.java
ExampleController.java
The goal of this controller is just to do a redirect from the index.xhtml to the target.xhtml.
target.xhtml
target.xhtml
Here we fetch the value of the foo variable by doing again #{flash.foo} to display the data.
The results:
This is the first page with the message Hello World.
And finally this is the second page (target.xhtml) wich displays the message from the previous page.
Final Considerations
We’ve learned how to pass values with the help of flash from one view to another with redirects.
Notice that if you refresh the second page after it shows up, all the data has gone. That’s because the flash scope was destroyed immediately when the rendering of the second page occurs. This is the goal of the flash scope. It just holds some values (in a flash session) between one HTTP request and the subsequent HTTP response.
This type of scope must be done programmatically. There is no annotation, like there is, for example, to the request scope (@RequestScoped).
The tech sector is booming. If you’ve used a smartphone or logged on to a computer at least once in the last few years, you’ve probably noticed this. As a result, coding skills are in high demand, with programming jobs paying significantly more than the average position.