Thursday, July 24, 2008

How to resolve problem with X11GraphicsEnvironment in unix

Are you planning to display report or graphics in web application running in unix operating system? Are you getting error similar to below error?

java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable. at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method) at sun.awt.X11GraphicsEnvironment.(X11GraphicsEnvironment.java:175)

This error is very common in unix with NO X environment. Here, I am trying to give couple of troubleshooting steps. Those are as follows:

  1. Set up headless mode: To set up headless mode, set the appropriate system property by using the setProperty() method. This method enables you to set the desired value for the system property that is indicated by the specific key.
    System.setProperty("java.awt.headless", "true");
  2. Set Java Environment Variable: Djava.awt.headless=true
  3. In JVM Process Definition of WAS, we need to create variable DISPLAYwith port # on which X11 is running, the Value field (default is:0.0).
    example :
    DISPLAY=0.0
  4. X11 server is running: If running please note the port number by issuing following command
    ps -ef egrep X11
  5. Setting the DISPLAY variable using script or in cell by issuing,setenv DISPLAY=:0.0 export DISPLAY
  6. Use the xhost + command to make this variable to global scope with above

Follow above steps one at a time and carry a test.

Regards

Monu



Wednesday, July 23, 2008

Implementation of SaveAs functionality in JSP as well as Servlet

Implementing SaveAs functionality in java/j2ee may not be a big deal. However for developers like me it is sounding difficult.
Every complex problem has a simple solution - atleast I believe that.
For those folks who found implementing SaveAs functionality in web application is difficult, this article will crack it.

Description: Implementing save as functionality in jsp as well as using servlet.
Assumptions: I assume that content of the file is already generated or file is generated in the server. Also assumes that we know the file type. Now browser should ask the user to save the file in desired location or open the file in appropriate editor.

Implementation in JSP:
FileType: excel
Code: response.setContentType("application/vnd.ms-excel")
FileType: pdf
Code: response.setContentType("application/pdf")

FileType: text
Code: response.setContentType("text/plain")

FileType: csv
Code: response.setContentType("application/octet-stream")
FileType: csv
Code: response.setContentType("application/octet-stream")

Additionally you can use following statement which will set the filename
response.setHeader ("Content-Disposition", "attachment;filename=\""+ fileName +"\"");

Implementation in Servlet: In servlet you can do something like below:
//set the content type based on file type
if(fileType.equalsIgnoreCase("PDF")){
response.setContentType ("application/pdf");
}
if(fileType.equalsIgnoreCase("TEXT")){
response.setContentType ("text/plain");
}
if(fileType.equalsIgnoreCase("CSV")){
response.setContentType ("application/octet-stream");
}

//set the file name
response.setHeader ("Content-Disposition", "attachment;filename=\""+ fileName +"\"");

//get the file content

File file = new File(filePath);
// Create the byte array to hold the data
byte[] bytes = null;
InputStream is;
try {
is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
}
bytes = new byte[(int)length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < numread="is.read(bytes,">= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset <>
response.setContentLength(bytes.length);
response.getOutputStream().write(bytes);
response.getOutputStream().close();


Common Mistake: I have seen developers try to write the file content into response object inside jsp which is illegal because server is already finished writing onto the response.



Regards
Monu

Wednesday, July 16, 2008

EGL: Handling session timeout

Session Handling is one of the most important task in web application. Same is true for EGL generated Java/j2ee code. In this article we will discuss about session handling in EGL targetting to Java/J2ee code. Also we will see how to deal with the scenerio when user tries to access the application pages without login.

Use Case:
1) When session expires, if the user clicks any of the link/buttons in the application, the application should log out and take the user to login page

2) When user enter the url of a page directly in the brower without validating itself, it should take you to login page.

One possible approach:

For session handling to work firstly we have to make an entry in the web.xml as session-timeout set to integer variable greater than 0.

In the application we should have a template. This template will be used by all the pages in the application except for login page. In general template is used to get a consistent look and feel through out the application. It may have header, footer, menus and so on. That means before loading individual pages it will first load template. So we have to write the session handling code in the template.


Sample code snippet:
You can do something like below in onConstructor

if(userId == null StrLib.clip(userId) == "")
urlPath string = urlType+server+":"+serverPort+contextPath;
//clear all session attributes
J2EELib.clearEGLSessionAttrs();
forward to url urlPath;
end


This way we can achieve the use case 2.

Regards
Monu