Monday, February 20, 2012

JAR to JAVA converter

I went through many websites,but i did not find any converter for jar file to .java files at one go.
So, I thought of writing a script for this conversion. This script also keeps the package structure same as in the jar file

Following are the prerequisites
  • This is restricted to windows platform
  • Unzip utility should be installed in the system
  • jad decompiler should be installed.
    If you don't have jad installed, you can find from the following link
    click here to download
  • Unzip the jad.zip downlaoded in step2,set the jad.exe in the systems path variable.
  • Run the command prompt in the administrator mode

Steps to run the batch file
  • cd "path to which batch file is placed"
  • jar2java.bat "jar file name/jar file path with jar file name"
  • Note: Give jar file name without extension
Download the batch file from here

Note:Remove the extention .txt to make it .bat file.

Example:
Step 1) >cd D:\jartojava (Copy the above downloaded batch file in this folder)
Step 2) jartojava> jar2java.bat abc (copy abc.jar in this folder)
Here a folder named abc is created with all the java files found in abc.jar.

Friday, December 16, 2011

Difference between ServletConfig and ServletContext

Following table lists down the main differences and others things are discussed afterwords:
ServletConfigServletContext(It should have been ApplicationContext)
One ServletConfig object is created per servlet One ServletContext object is created per Application
Can be used to pass deployment time information to the Servlet. Information is stored as parameters in the deployment descriptor(web.xml) Can be used to share Application level information across servlets and is this information is also stored in the web.xml as well as parameters (So it kind of acts as GlobalContext or Application Context)
Provides a set of methods to set and get the initialization configuration information Provides a set of methods to the servlet so that servlet can communicate with the Container
------------------------Servlet can access a ServletContext object only through ServletConfig object

Working with ServletConfig:

We will explaing here how to declare the Servlet init Parameters in in the Deployment Descriptor(web.xml) and how to retrieve them in the servlet also how to get the object of Servlet Context from ServletConfig.
web.xml: Declaring init parameters for a specific servlet

    
    SampleServ
    SampleServ
    in.javaespresso.web.SampleServ
    
     Sample Init Parameters
     servParamservValue
  
Servlet Code: Getting init parameters inside the servlet
getServletConfig().getInitParameter("servParam");
Servlet Code: Getting ServletContext object using ServletConfig
ServletContext context = getServletConfig().getServletContext();

Working with ServletContext:

Let us get the context paramteres declared in the web.xml in the servlet and jsp
web.xml: Declaring context parameters

    Sample context parameters
    emailjava.espresso@gmail.com
Servlet Code: Accessing context params in Servlet
getServletContext().getInitParameter("email")
JSP code: Accessing context params in JSP
<%
getServletContext().getInitParameter("email");
%>

Please find the code for the article attached here