Using Jsp in a Jersey JAX-RS RESTful application

Posted: décembre 29th, 2011 | Author: | Filed under: Dev, Glassfish, Java, Java EE, JAX-RS, Jersey, Tools, Tutorial | Tags: , , , , , , , , , , , | 30 Comments »

So, ok we could easily produce some RESTful applications with Jersey.
But sometimes, the output could be very big to put in a method and a template could be useful.

Jersey provides MVC support for JSP pages.
There is a JSP template processor that resolves absolute template references to processable template references that are JSP.


1 – Configure web.xml

<filter>
	<filter-name>jersey</filter-name>
	<filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
	<init-param>
		<param-name>com.sun.jersey.config.property.packages</param-name>
		<param-value>com.ezakus.web</param-value>
	</init-param>
	<init-param>
		<param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
		<param-value>/WEB-INF/jsp</param-value>
	</init-param>
	<init-param>
		<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
		<param-value>/(resources|(WEB-INF/jsp))/.*</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>jersey</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

Instead of a servlet mapping you need to have a filter.

With the JSPTemplatesBasePath param, you choose your jsp folder

	<param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
	<param-value>/WEB-INF/jsp</param-value>

And with the WebPageContentRegex you are able to serve static resources.
In the previous example, static resources are on the /resources/ or /WEB-INF/jsp/ path but you can put what you want :

	<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
	<param-value>/(resources|js|css|images)/.*</param-value>

2 – Return Viewable or Response

Now you can use the Viewable class with your jsp path

package com.ezakus.web;

import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import com.sun.jersey.api.view.Viewable;

@Stateless
@Path("/")
public class MyController {

    @GET
    @Produces("text/html")
    public Viewable index() {
    	return new Viewable("/index");
    }

}

Note : /index assume that you have a /WEB-INF/jsp/index.jsp on your path

You can also use the Response class

package com.ezakus.web;

import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import com.sun.jersey.api.view.Viewable;

@Stateless
@Path("/")
public class MyController {

    @GET
    @Produces("text/html")
    public Response index() {
    	return Response.ok(new Viewable("/index")).build();
    }

}

3 – Using Viewable’s model

The Viewable object could be created with a model :

package com.ezakus.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import com.sun.jersey.api.view.Viewable;

@Stateless
@Path("/")
public class MyController {

    @GET
    @Produces("text/html")
    public Response index() {
    	Map<String, Object> map = new HashMap<String, Object>();
        map.put("user", "usul");
        List<String> l = new ArrayList<String>();
        l.add("light saber");
        l.add("fremen clothes");
        map.put("items", l);
    	return Response.ok(new Viewable("/cart", map)).build();
    }

}

Note : Jersey will assign the model instance to the attribute « it » in the jsp. (Yes, life is hard)

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>Welcome!</title>
</head>
<body>
  <h1>Welcome ${it.user}!</h1>
  <p>
  	items in your cart :<br />
    <c:forEach var="item" items="${it.items}">
    	${item}<br />
    </c:forEach>
  </p>
</body>
</html>

How to change the Glassfish server log level from command line

Posted: décembre 28th, 2011 | Author: | Filed under: Admin, asadmin, Glassfish, Java, Java EE, Tools, Tutorial | Tags: , , , , , , , , , , , | 1 Comment »

When deploying applications with glassfish, you have sometimes (just sometimes) to read the server log :) You can change, mostly on the fly (no restart needed), the log levels, that could be very useful. First, list the log levels :

asadmin list-log-levels

Don’t forget to give instance name if you want those for this instance

asadmin list-log-levels instance1

Here is an example :

asadmin list-log-levels instance1

ShoalLogger	<CONFIG>
com.sun.enterprise.server.logging.GFFileHandler	<ALL>
java.util.logging.ConsoleHandler	<FINEST>
javax.enterprise.resource.corba	<INFO>
javax.enterprise.resource.javamail	<INFO>
javax.enterprise.resource.jdo	<INFO>
javax.enterprise.resource.jms	<INFO>
javax.enterprise.resource.jta	<INFO>
javax.enterprise.resource.resourceadapter	<INFO>
javax.enterprise.resource.sqltrace	<FINE>
javax.enterprise.resource.webcontainer.jsf.application	<INFO>
javax.enterprise.resource.webcontainer.jsf.config	<INFO>
javax.enterprise.resource.webcontainer.jsf.context	<INFO>
javax.enterprise.resource.webcontainer.jsf.facelets	<INFO>
javax.enterprise.resource.webcontainer.jsf.lifecycle	<INFO>
javax.enterprise.resource.webcontainer.jsf.managedbean	<INFO>
javax.enterprise.resource.webcontainer.jsf.renderkit	<INFO>
javax.enterprise.resource.webcontainer.jsf.resource	<INFO>
javax.enterprise.resource.webcontainer.jsf.taglib	<INFO>
javax.enterprise.resource.webcontainer.jsf.timing	<INFO>
javax.enterprise.system.container.cmp	<INFO>
javax.enterprise.system.container.ejb	<INFO>
javax.enterprise.system.container.ejb.mdb	<INFO>
javax.enterprise.system.container.web	<INFO>
javax.enterprise.system.core.classloading	<INFO>
javax.enterprise.system.core.config	<INFO>
javax.enterprise.system.core	<INFO>
javax.enterprise.system.core.naming	<INFO>
javax.enterprise.system.core.security	<INFO>
javax.enterprise.system.core.selfmanagement	<INFO>
javax.enterprise.system.core.transaction	<INFO>
javax.enterprise.system	<INFO>
javax.enterprise.system.ssl.security	<INFO>
javax.enterprise.system.tools.admin	<INFO>
javax.enterprise.system.tools.backup	<INFO>
javax.enterprise.system.tools.deployment	<INFO>
javax.enterprise.system.util	<INFO>
javax.enterprise.system.webservices.registry	<INFO>
javax.enterprise.system.webservices.rpc	<INFO>
javax.enterprise.system.webservices.saaj	<INFO>
javax	<INFO>
javax.org.glassfish.persistence	<INFO>
org.apache.catalina	<INFO>
org.apache.coyote	<INFO>
org.apache.jasper	<INFO>
org.eclipse.persistence.session	<INFO>
org.glassfish.admingui	<INFO>
org.jvnet.hk2.osgiadapter	<INFO>
Command list-log-levels executed successfully.

After that, you just have to set it, for example :

asadmin set-log-levels javax.enterprise.system.container.ejb=<level>
# Here with the instance
asadmin set-log-levels <instance-name> javax.enterprise.system.container.ejb=<level>

As you could see in the asadmin documentation :
Log level values are SEVERE, WARNING, INFO, CONFIG, FINE, FINER, and FINEST.
The default setting is INFO.

When you perform this, i think you are watching the logs.
So you could view the changes applied, if not, you have to restart the instance (or server).
Hope it would be useful for you