跳至主要内容

博文

目前显示的是标签为“java ee8”的博文

Servlet 4.0: HTTP/2 Server PUSH

Server Push One highlight feature of HTTP/2 is Server Push. Servlet 4.0 add PushBuilder to handle push. An exmaple of enable Servlet Push. @WebServlet ( urlPatterns = " " ) @ServletSecurity ( httpMethodConstraints = { @HttpMethodConstraint ( value = " GET " , transportGuarantee = CONFIDENTIAL ) }) public class PushServlet extends HttpServlet { @Override protected void doGet ( HttpServletRequest req , HttpServletResponse res ) throws IOException , ServletException { PushBuilder pushBuilder = req . newPushBuilder() . path( " main.css " ); pushBuilder . push(); res . getWriter() . println( " <html><head><title>HTTP2 Test</title><link rel= \" stylesheet \" href= \" main.css \" ></head><body>Hello Servlet Push!!!</body></html> " ); } } Run this application on Glassfish v5 in NetBeans ID...

CDI 2.0: Handle Events in desired Priority

Handle Events in desired Priority If there are multi CDI @Obsevers emthods defined, in the former CDI, there is no way to ensure they are excuted in a certain order. In CDI 2.0, this gap is filled by @Priority . Let's create a simple example to demonstrate it. Create a bean to fire a CDI Event . @ViewScoped @Named ( " eventBean " ) public class EventBean implements Serializable { private static final Logger LOG = Logger . getLogger( EventBean . class . getName()); @Inject Event< Message > event; private String message; public String getMessage () { return message; } public void setMessage ( String message ) { this . message = message; } public void fireEvent () { LOG . log( Level . INFO , " fire event async... " ); event . fire( new Message ( this . message)); } } The event payload Message . public class Message implements Serializa...

Getting started with Java EE 8 MVC(3)-Exception Handling and form validation

Exception Handling and form validation When submitting a form, it should validate the form data before it is stored in the backend database. Form binding and validation Like Spring MVC, Struts, Stripes, JSF etc. MVC provides the similiar progress to process form submission. Gather user input form data. Convert form data to the target form bean. If there are some conversion failure, it is possbile to stop the progress and notify user. Bind the converted value to the form bean. Validate the form bean via Bean Validation . If there are some constraint voilations, it is possbile to stop the progress and notify user. Continue to process form. MVC provides a BindingResult to gather all of the binding errors and constraint voilations in the request. Handling form validation Inject it in the controller class. @Inject private BindingResult validationResult; In the controller method, add @Valid annotation on the methed parameters. @ValidateOnExecution(type = Exe...

Getting started wtih Java EE 8 MVC(2)-Handling form submission

Handling form submission In the tranditional MVC applications, the mostly-used HTML methods should GET and POST . POST method is usually used in the form submission, aka form post. In order to create a new task, you should follow the flow. In the task list view, click Add Task button to enter the creating task view. Fill the fields of the task form and submit. It will return to task list view. Display the creating page Firstly display the task creating page. @GET @Path("new") public Viewable add() { log.log(Level.INFO, "add new task"); return new Viewable("add.jspx"); } Viewable is another approach to indicate a view path, I have motioned the @View annotation in the first post. The add.jspx page code snippets. <div class="row"> <div class="col-md-12"> <c:url var="formActionUrl" value="/mvc/tasks"/> <form id="form" name="form" ro...

Getting started with Java EE 8 MVC

Getting started with Java EE 8 MVC MVC is a new specification introduced in the upcoming Java EE 8. It is based on the existing JAXRS. At the moment I wrote down these posts, most of Java EE 8 specficitaions are still in the early disscussion stage, and MVC 1.0 is also not finalized, maybe some changes are included in future. I will update the Wiki pages and codes aglined with final Java EE 8 specficitaions when it is released. I will use the latest Java 8, Glassfish 4.1.1, and NetBeans IDE for these posts. Prequisition Oracle JDK 8 or OpenJDK 8 Oracle Java 8 is required, go to Oracle Java website to download it and install into your system. Optionally, you can set JAVA_HOME environment variable and add <JDK installation dir>/bin in your PATH environment variable. The latest Apache Maven Download the latest Apache Maven from http://maven.apache.org , and uncompress it into your local system. Optionally, you can set M2_HOME environment va...