random thoughts

HOWTO Use Threads in JEE Web Applications

We all know that , we should not use Java Threads in our web applications, but what if , there is a situation where we have to do two or more time consuming tasks simultaneoulsy.

IBM and BEA together has given a solution for this problem 3 years back , i wonder how many of us know this . I came to know about this just 6 months back.

The solution to our concurrency problem in web applications is the WorkManager API. WorkManager API helps us to execute several tasks in parallel with one another.

i am sorry , i havent used the api , so i couldn give a sample code :( But, these links will help you know more about the WorkManager API http://www.java world.com/javaworld/jw-01-2007/jw-01-workmgr.html WorkManager API — JSR 237 http://dev2dev.bea.com/pub/a/20 05/05/parallel_tasks.html

javax.servlet.ServletException: Path loginLayout Does Not Start With a “/” Character

… its been a year and a half , i have used Struts. A week back we got a project and we decided to use Struts 1.3.5 since Struts 2.0 is still in beta. While i was trying forward response to a Tile forward instead of directly redirecting to a jsp , i got an exception javax.servlet.ServletException: Path loginLayout doesnot start with a “/” character after googling for half an hour i got the solution pre-requsite : download Struts1.3.5-all.zip

Step 1. add chain-config.xml to WEB-INF folder inside your project you can find this file in /struts-1.3.5-all/struts-1.3.5/src/tiles/src /main/resources/org/apache/struts/tiles

Step 2. add the following init-param tag to Action Servlet configuration in web.xml ** chainConfig <param-value

/WEB-INF/chain-config.xml ** Step 3. add commons- chain-1.1.jar to your project library.

this should solve the problem.

HOWTO Pass Request and Response From One Servlet to Another Servlet …

…that sounds simple for a java/j2ee programmer…

RequestDispatcher rd = request.getRequestDispatcher”/jsps/login.jsp”);
rd.forward(request,response);

but the scenario is …
i have to pass the request and response from a servlet in one web application to another servlet or jsp in another web application deployed in the same server …

lets say we have two web applications SabreNews and BeanPicks

and we have to pass the request and response object from a Servlet or a jsp in SabreNews to a resource(Servlet or a jsp) in BeanPicks

how should we go about ?

here is the solution :

ServletContext beanPicksContext = getServletContext().getContext(“/BeanPicks”);
beanPicksContext.getRequestDispatcher(“/beanPicks/jsps/displayBlog.jsp”).forwa rd(request,response);

getServletContext() is the method declared in the ServletConfig object .As the method name goes by , it returns the ServletContext object with which a Servlet can communicate with the ServletContainer in which the webapplication runs.
so now we have got the line to talk with the ServletContainer which also holds other web applications and BeanPicks too .

Now , we have to ask for the BeanPicks’s context and we get the BeanPicks context by calling getContext(“/BeanPicks”)

we have got the context and the rest is to get the RequestDispathcher(getRequestDispatcher(java.lang.String path)) on a particular resource (displayBlog.jsp) and forward the request and response

i am using the code snippet i have given above , in my web application and it is working fine for me .if there is another way doing it , please blog it and let me know.

please refer to J2EE 1.4 api docs for brief information about ServletConfig , ServletResponse and GenericServlet objects