Empty JBoss Cache

I did a new Login page for my web application. I spent 3 days for the new layout.
So the only thing that separeted me from my new satisfaction was the deploy in the production environment
After the deploy in Jboss, i connect with the web application and ... oops: the login page was not changed!
OK! The first thing i I thinked was IE cache! But it wasn't...
The problem was the JBoss cache.
you've to delete the file {jboss_home}\server\{default}\tmp\deploy\tmpxxx{webApplicationName}.war
and {jboss_home}\server\{default}\tmp\deploy\tmpxxx{webApplicationName}

sendRedirect or writing to a Servlet response stream in JSF

Problem: i want to do a sendRedirect from an actionListener method of a JSF backingBean.
After reading the servlet response i do a sendRedirect but it is not sufficent, because i got this exception:

java.lang.IllegalStateException: Cannot forward after response has been committed

So, i must include this code after the sendRedirect method:

FacesContext context= FacesContext.getCurrentInstance();
context.responseComplete();


Ex.:
try {
HttpJsfUtil.getResponse().sendRedirect(url);
FacesContext context=FacesContext.getCurrentInstance();
context.responseComplete();
} catch (IOException e1) {
e1.printStackTrace();
}


The code above is the same in the case of a response stream: if you want to write in the response stream inside the actionListener method, after you close the response you have to include the above code before exit the method.
Ex.:
try{
HttpJsfUtil.getResponse().setContentType("application/x-download");
HttpJsfUtil.getResponse().setHeader("Content-Disposition", "attachment; filename="+fileName);
HttpJsfUtil.getResponse().setHeader("Cache-Control", "no-cache");
ServletOutputStream out= HttpJsfUtil.getResponse().getOutputStream();
out.write(imageBytes);
out.close();
FacesContext
context=FacesContext.getCurrentInstance();
context.responseComplete();
}
catch (IOException ex)
{
System.out.println ("Envelope error: " + ex.getMessage ());
}


where HttpJsfUtil.getResponse() is

public static HttpServletResponse getResponse()
{
return (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
}

From InputStream to byte array

Oh! And finally happen!
This is the code from converting(/writing/copying) an InputStream object into an array of byte:

InputStream is=urlc.getInputStream();
ByteArrayOutputStream outb = new ByteArrayOutputStream();
final int BUF_SIZE = 1 << 8; //1KiB buffer
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while((bytesRead = is.read(buffer)) > -1) {
outb.write(buffer, 0, bytesRead);
}
is.close();
byte[] imageBytes = outb.toByteArray();

And it works!

NT Authorization in a web application

This time i speak about the authorization microsoft windows based code in a java web application.
This is the the (servlet) code:

String auth = request.getHeader("Authorization");
if (auth == null)
{
response.setStatus(response.SC_UNAUTHORIZED);
response.setHeader("WWW-Authenticate", "NTLM");
response.flushBuffer();
return;
}
if (auth.startsWith("NTLM "))
{
byte[] msg = new sun.misc.BASE64Decoder().decodeBuffer(auth.substring(5));
int off = 0, length, offset;
if (msg[8] == 1)
{
byte z = 0;
byte[] msg1 = {(byte)'N', (byte)'T', (byte)'L', (byte)'M', (byte)'S', (byte)'S', (byte)'P', z,(byte)2, z, z, z, z, z, z, z,(byte)40, z, z, z, (byte)1, (byte)130, z, z,z, (byte)2, (byte)2, (byte)2, z, z, z, z, z, z, z, z, z, z, z, z};
response.setHeader("WWW-Authenticate", "NTLM " + new sun.misc.BASE64Encoder().encodeBuffer(msg1));
response.sendError(response.SC_UNAUTHORIZED);
return;
}
else if (msg[8] == 3)
{
off = 30;

length = msg[off+1]*256 + msg[off];
offset = msg[off+3]*256 + msg[off+2];
String domain = new String(msg, offset, length);

length = msg[off+9]*256 + msg[off+8];
offset = msg[off+11]*256 + msg[off+10];
String user = new String(msg, offset, length);

length = msg[off+17]*256 + msg[off+16];
offset = msg[off+19]*256 + msg[off+18];
String computerName = new String(msg, offset, length);
}
}

How to set a datasource in tomcat

It seems a simple thing:
create a context.xml in the PUBLIC-HTML/META-INF folder of the web app.

The content of the context.xml must be something like this:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/webAppName" docBase="webAppName">
<Resource name="jdbc/DataSourceName" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@dataBase:1521:ORCL"
username="user" password="pwd" maxActive="20" maxIdle="10"
maxWait="-1"/>
</Context>

web.xml for JAX-WS

This post must be read when your webservice, realized with JAX-WS, is ready to go on deploy, but before to specify it in the web.xml.

To specify a JAX-WS webservice in the web.xml you must set a listener and a servlet, like this:

<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>
WebServiceName
</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
<load-on-startup>
1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>
WebServiceName
</servlet-name>
<url-pattern>
/urlPatternForWebService
</url-pattern>
</servlet-mapping>

Remember a very important thing: to include in your web-inf directory the sun-jaxws.xml file that contain the class that implement the web service. Ex.:

<?xml version="1.0" encoding="UTF-8"?>
<endpoints version="2.0" xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
<endpoint implementation="package.className" name="webServiceName" url-pattern="/webServicePattern"/>
</endpoints>

Empty-cells with IE? Yes please

But not only empty-cells: show;
With IE it is not
enough!!! (but with Firefox yes, this is class!) You've to specify border-collapse:collapse;
So, if you would not empty cells in your table with InternetExplorer you must write in the CSS something like this:

table{
border-collapse:collapse;
empty-cells: show;
}

StartUp with JSF (1.1)

How to include JSF (1.1 version - i know: today it's obsolete) in your web project?
Those are the steps:
1) include jsf-impl.jar and jsf-api.jar in the web-inf/lib
2) add the jsf listner in the web.xml:
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
3) add the jsf servlet in the web.xml:
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
4) add a faces-config.xml file in the web-inf folder

That's all folks!

JVM INSTR, this obscure instruction

What is JVM INSTR?
It is a Java byte code instruction, obviously! But why i found a source with this instruction when i decompiled with jad? Beacuse the original source code has been obfuscade! Of course!

Disable eclipse plugin

Today i've to disable an ecplise plugin.
I never do it, i only installed and installed and installed new plugins.
You've to go in Help -> Software Updates -> Manage Configuration

Selecting this menu item opens the Distributions Configuration dialog. You can now expand the plugin tree, to see all the installed component. Click the mouse right button on a plugin and select disable (or uninstall, if you want to delete the plugin from your machine).

Oracle bought Sun Microsystem

Big shopping in the Silicon Valley

Oracle purchase SUN for 7,4 billion
OK! My Sun certifications become Oracle certification! Dho!

Custom 404 with jsp

First of all, you've to insert in the web xml this lines:

<error-page>
<error-code>404</error-code>
<location>error.jsp</location>
</error-page>


In this way tha application server when doesn't find a resource it will pass the ball to the error page.
When the server forward to the error page it passes same attribute in the request, like:

Attribute

Type

javax.servlet.error.status_code

java.lang.Integer

javax.servlet.error.exception_type

java.lang.Class

javax.servlet.error.message

java.lang.String

javax.servlet.error.exception

java.lang.Throwable

javax.servlet.error.request_uri

java.lang.String

javax.servlet.error.servlet_name

java.lang.String


It will be enough, in the error page, to show something a message like this:
The requested reource <%=request.getAttribute("javax.servlet.error.message")%> has not be found!

Obviously, the same things are valid for the others HTTP errors, like 500, 400, and so on.

How lookup the world in different langauges

This time we speak about the lookup service.
Why? because the problem is this: i have a datasource, say "jdbc/gifaDS". I installed this datasource in Tomcat, in JBoss and finally in Oracle Ias 10g.
I will not speak about how to define a datasource in the above application servers, but how to lookup them!
Yes, because the jndi service is different in every application server.
In poor words:
  • with Tomcat: java:comp/env and after the datasource name, so: java:comp/env/jdbc/gifaDS
  • with JBoss: only java:, so: java:/jdbc/gifaDS
  • with Oracle Ias 10g: no prefix, so: jdbc/ConanDS

web.xml warning "CHKJ4019W: Invalid res-sharing-scope; valid values are "Shareable" or "Unshareable"

what?
I have this warning from vary vary time ago. And only today i decided to investigate.
And i find a solution: adding a res-sharing-scope tag inside the resource-ref tag, and exactly:


<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/DB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

Error Messages with JSF

If your method throws an exception or, more generally, there is a controlled error or warning and you must show to user this error then you can add a FacesMessage in your JSF page in this way:

try {
yourCode...;

}
catch (YourApplicationException e) {
FacesMessage messaggio=new FacesMessage(e.getMessage());
FacesContext context=FacesContext.getCurrentInstance();
context.addMessage(null,messaggio);
return null;
}

In your jsp page you have to add this code:

<h:messages globalOnly="true" />

The globalOnly attribute because it will display all generic error messages, those messages with the first parameter of addMessage method equals to null.
If you want display errors about a single field the you must specify the "validator" attribute of that field!

Theoretically, you could specify the id field in the first parameter of "addMessage" method but with JSF 1.1 seems not work!

IBM wants Sun

IBM is buying Sun! It's not a done thing, but it seems that it will do. The price is astronomic: $6.5 billions.
I ask my self: what will be my SCEA certification: it will became ICEA certification?

Keep things together

This time i will explain a functionality of xsl-fop: "keep-together" attribute.
In a pdf page (report), that is created by xsl-fop, i don't want that a block is split between page, if it doesn't enter completely in one page. So the solution is add the keep-together attribute in the fo:block, and more specifically this is the right attribute: keep-together.within-page="always".
In this case (within-page) the block is not split between two different pages, but the block is written all in the next page.

Two Div Block Side by Side

Ok! It should be a simple thing, but if you have to do with IE and Firefox it is not a simple thing.
It seems that IE and Firefox are two gilrs and the html is the only boy! If html goes with IE, Firefox is jealous and viceversa!
It seams that html is a short blanket and IE is the feet and Firefox is the face: if you want to cover the face then you have to uncover the feet and viceversa!
So you have to add some patch!
This is the case of two div blocks that have to place side by side: if you do something like:
.left {
float:left;
background-color:yellow;
width:20%;
}
.right {
float:right
background-color:blue;
width:80%;
}
is ok with Firefox, but with IE if the text inside the right block is larger than 80% the right block goes under the left one.
If i add a clear:left instruction in the right style it is good for IE but not for Firefox.
So this is my solution:
.left {
float:left;
background-color:yellow;
width:20%;
}
.right {
position:absolute;
left:21%;
background-color:blue;
width:79%;
}

I know that if the text inside the left div goes over the 20% it will go under the right div. if i will want to show all the content of the left div i will add overflow:auto instruction.

Inducing JBoss to choose your jsf version

This is my real first post. And i choose this problem because i've a note in a recycled sheet that i want to discard.
So, the problem:
"I want to use jsf version 1.1 in a JBoss environment that, for default, wants jsf 1.2 pages"
I solved it chancing the web.xml file in the JBOSS root -> server -> default -> deploy -> jboss-web.deployer -> conf in two ways:

1) I comment out the second and third "Common Listener Configuration" listener:
JBossJSFConfigureListener and WebappLifecycleListener
2) I comment out the init-param tagLibJar0 and tagLibJar1

Obviously in this way you've to provide the jsf libraries your self!!!

My blog is born!

I announce the birth of my first blog!
As the title says, it should be a log (instance of java.util.logging.Logger class) of what happens in my days, the days of a web developer!
This will be useful first of all to me, therefore i will log all my troubles and how i will solve them, because in my job nothing is impossible!!! Maybe this blog will became a glossary of java terms or business terms, or simply an "Inter blog", because, after the family, my second love is Inter!!!
Like a child, no one knows what this blog will do when it will be major. But i hope that it will be better then father...