Pages

Saturday, June 30, 2012

ServletConfig and ServletContext in Java Servlets

ServletConfig
***************
ServletConfig encapsulates servlet configuration,that is used by the servlet container,used to pass information to servlet during initialix(z)ation.
ServletConfig is one per servlet.

Methods in <<ServletConfig>> interface:
i)          String getInitParameter(String)
ii)          Enumeration getInitParameterNames()
iii)         ServletContext getServletContext()
iv)         String getServletName() 

ServletContext
*****************
ServletContext is one per web application.

Methods in <<ServletContext>> interface:
i)          Object getAttribute(String)
ii)          String getInitParameter(String)
iii)         String getServletContextName()
iv)         void setAttribute(String,Object)
v)         void removeAttribute(String)
vi)         RequestDispatcher getRequestDispatcher(String path)
and many more...

A simple example

Wednesday, June 20, 2012

JSP tutorial (Java Server Pages)


JavaServer Pages (JSP) technology provides a simplified, fast way to create dynamic web content. JSP technology enables rapid development of web-based applications that are server- and platform-independent.

Steps in processong of Jsp
i)Jsp is translated to servlet(.java file)
ii)Compiled to .class file
iii)Loaded and initailized as servlet object


Jsp Elements
------------
i)Scriptlet(<%  %>)
    Eg:<% out.println("Hello Jsp");%>

Monday, June 18, 2012

Custom tag development

Tag handler : A Tag handler as opposed to a tag file, is simply Java class that does the work of the tag.

These are come in two ways.
i)Classic (pre. versions of JSP)
ii)Simple (JSP 2.0)

Making a Simple Tag handler : 
i)Write a class that extends SimpleTagSupport
ii)Override the doTag() method
iii)Create a TLD for the tag
iv)Deploy the tag handler and TLD
v)Write a JSP that uses the tag

A Simple tag example:

Tuesday, June 5, 2012

Spring Hibernate sample application

Spring Hibernate Integration Login Example using MySQL database and Tomcat Web Server.
A short description of how Spring MVC works (source : http://www.javacodegeeks.com)
When a request is sent to the Spring MVC Framework the following sequence of events happen.
  • The “DispatcherServlet” first receives the request
  • The “DispatcherServlet” consults the “HandlerMapping” and invokes the "Controller" associated with the request
  • The "Controller" process the request by calling the appropriate service methods and returns a “ModeAndView” object to the “DispatcherServlet”. The “ModeAndView” object contains the model data and the view name
  • The “DispatcherServlet” sends the view name to a “ViewResolver” to find the actual “View” to invoke
  • The “DispatcherServlet” passes the model object to the “View” to render the result
  • The “View” with the help of the model data renders the result and return it back to the user 
Eg : Employee Login:

Monday, June 4, 2012

Java Spring sample application

Spring tutorial example

The Spring Framework consists of features organized into about 20 modules. These modules are grouped into Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming),Instrumentation, and Test.

-The Core Container consists of the Core, Beans, Context, and Expression Language modules.
-The Data Access/Integration layer consists of the JDBC, ORM, OXM, JMS and Transaction modules.
-The Web layer consists of the Web, Web-Servlet, Web-Struts, and Web-Portlet modules.
-Spring's AOP module provides an AOP Alliance-compliant aspect-oriented programming.
-The Test module supports the testing of Spring components with JUnit or TestNG.

A simple example in spring (3.x)
************************

Sunday, June 3, 2012

MySQL data Encryption and Decryption using AES_ENCRYPT and AES_DECRYPT

Encryption and Decryption using AES_ENCRYPT and AES_DECRYPT

First create a table called test_encdec :

mysql> create table test_encdec(

 -> name varchar(30),

 -> password varbinary(150)

 -> );

Query OK, 0 rows affected (0.14 sec)

mysql> desc test_encdec;

+----------+----------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+----------+----------------+------+-----+---------+-------+

| name | varchar(30) | YES | | NULL | |

| password | varbinary(150) | YES | | NULL | |

+----------+----------------+------+-----+---------+-------+

2 rows in set (0.00 sec)
 mysql> insert into  test_encdec values('S L N V Praveen',aes_encrypt('praveen','
myname'));
Query OK, 1 row affected (0.01 sec)

mysql> select * from  test_encdec;
+-----------------+------------------+
| name            | password         |
+-----------------+------------------+
| S L N V Praveen | 8#sTyn ]7?รป¬+W¬2 |
+-----------------+------------------+
1 row in set (0.02 sec)

mysql> select name,aes_decrypt(password,'myname') from  test_encdec;
+-----------------+--------------------------------+
| name            | aes_decrypt(password,'myname')     |
+-----------------+--------------------------------+
| S L N V Praveen | praveen                                   |
+-----------------+--------------------------------+
1 row in set (0.00 sec)

mysql> select aes_decrypt(password,'myname') from  test_encdec;
+--------------------------------+
| aes_decrypt(password,'myname') |
+--------------------------------+
| praveen                        |
+--------------------------------+
1 row in set (0.00 sec)