Monday, May 28, 2018

Introduction to Design Patterns in Java

Leave a Comment

Introduction to Design Patterns in Java



Design patterns which are like templates in programming languages to resolve complex creational, structural and behavioral challenges, these templates make coding more organized and with better code quality.

Some of the challenges such as we need only single object of class during complete life cycle of an application such requirement is well suited for database connections or logger utility code. This can be solved by using singleton design pattern.

Another example is we need to write code which is extensible tomorrow or we should be able to plug a new component or service or algorithm with the existing code. So instead of changing ten different class it should allow us to just write new class with existing implantation and there we can write new algorithm. These are some of the examples of design pattern and when application grows efforts put in design patterns really pays off. It will give you better code quality standards, low maintenance cost and less effort when your are extending your code tomorrow.

So based on usage we have three different types of design patterns:

Creational Design Pattern

This design pattern deals with class instantiation and objects creation. In simple terms creational is all about creating object and initializing classes. Class initialization make uses of inheritance to achieve the desired behavior whereas object creation uses delegation work to different components to get it done.

Structural Design Pattern

Structural design pattern make sure the organizing the classes as the application grows and make sure any new changes to code or components are accommodated with minimum changes to exiting code and architecture.

Behavioral Design Pattern

Behavioral desing pattern is all about how components such as classes, interfaces and objects interact with each other efficiently. By following this pattern we can make sure that all components are known and well tested pattern to communicate to other components.





Read More

Wednesday, May 23, 2018

Why ActionServlet is Singleton in struts framework?

Leave a Comment
org.apache.struts.action.ActionServlet in the web.xml configuration file must define ActionServlet is Singleton in struts framework and as ActionServlet provided by the struts Framework is a built-in servlet. ActionServlet nothing but a controller.



It has the following inbuilt design patterns.

  • Singleton design pattern.
  • FrontController
  • ApplicationController
  • MVC
  • Data Transfer object/value object
  • Inversion of control/Dependency injection

ActionServlet take the multiple request but it creates single instance for that requests by using singleton design pattern.
Few servers likes weblogic violates single instance multiple threads principle of servlet programming.That means these servers that will create more than once object for a class in special situation. see here

To avoid this type of situations & to follow servlet specifications, struts framework software developers have made built-in servlet called ActionServlet.

Note:

1. According to MVC principles, there should be only one servlet acting as controller and this servlet should have only one object.

2. Both the approaches have advantages & disadvantages :)
org.apache.struts.action.ActionServlet in the web.xml configuration file must defined as

ActionServlet provided by the struts Framework is a built-in servlet. ActionServlet nothing but a controller.It has the following inbuilt design patterns.

  • Singleton design pattern.
  • FrontController
  • Application Controller
  • MVC
  • Data Transfer object/value object
  • Inversion of control/Dependency injection


ActionServlet take the multiple request but it creates single instance for that requests by using singleton design pattern.
Few servers likes weblogic violates single instance multiple threads principle of servlet programming.That means these servers that will create more than once object for a class in special situation. see hereTo avoid this type of situations & to follow servlet specifications, struts framework software developers have made built-in servlet called ActionServlet.
Note:

  • According to MVC principles, there should be only one servlet acting as controller and this servlet should have only one object.
  • Both the approaches have advantages & disadvantages
Read More

Stored Procedures for Beginners

Leave a Comment

Stored Procedures for Beginners


You are reading my article because you want to learn how to write stored procedures. You are new to this, and you don’t know where to start. You are exactly where I was when I wanted to learn how to write stored procedures. The purpose of this article is to help the developer who doesn’t know where to start. 



I will give you a place to start, teach you techniques beyond the basic "hello world”, and walk you through examples. I learned how to write stored procedures alone, and have decided to let you use my experiences to your advantage. This article will consist of the following topics:
  • Input Variables
  • Input Variables with SELECT and UPDATE Statements
  • Exercise:  Pass Data to a SELECT Stored Procedure
  • Conclusion
  • Database script for SPFORBEGINNERS (the database referenced in this article)

At the end, I have also included a SQL script for creating the database used in my examples. It is a small database, and easily replicated.

What Are Stored Procedures?

Have you ever written SQL statements, like inserts, selects, and updates? Then you have already written most of a stored procedure. A stored procedure is an already written SQL statement that is saved in the database. If you find yourself using the same query over and over again, it would make sense to put it into a stored procedure. When you put this SQL statement in a stored procedure, you can then run the stored procedure from the database’s command environment.

An example is:

exec usp_displayallusers

The name of the stored procedure is "usp_displayallusers”, and "exec” tells SQL Server to execute the code in the stored procedure. (Note: "usp_” in front of the stored procedure name is used to designate this stored procedure as a user-created stored procedure.) The code inside the stored procedure can be something as simple as:

SELECT * FROM USERLIST

This "select” statement will return all data in the USERLIST table. You may think, skeptically, that stored procedures aren’t terribly useful. Just save the query and run it when you need to. Too easy, right?

Well, there is more to the story. Many queries get more complex than "select * from . . .”  Also, you may want to call the stored procedure from an application, such as an ASP page, Visual Basic application, or a Java servlet. With a stored procedure, you can store all the logic in the database, and use a simple command to call the stored procedure. Later, if you decide to use J2EE, you only need to change the application layer, which will be significantly easier. Much of the business logic will remain in the database.

Enough background—let’s write some stored procedures.

Getting Started with Stored Procedures

What do I need to get started? I have heard that question often. To begin writing stored procedures, the following are essential:

1. A database management system.

2. A database built inside the database management system (see the end of this article for a sample).

3. A text editor, such as Notepad or Query Analyzer.

Items 1 and 2 are absolutely essential. You can’t write stored procedures without a database. They would be useless. Sometimes, I write my procedures in Notepad (or another text editor), and copy them into the New Stored Procedure window in My SQL Server.

Next, you will have to decide what you want your stored procedure to do. It can be tempting to just dive right into the task at hand, but it is always prudent to sketch out some ideas first. Some considerations should be:

* Do you want to view data in the database (SELECT), insert new records (INSERT INTO), or do I want to change an existing record (UPDATE)?

* With which tables will you have to work? Does it make sense to create a VIEW first?

* How often will this procedure actually be used?

Once you have struggled with these questions (something of an exaggeration, I guess), you will be ready to start coding!

Note: Throughout this article, I will focus on stored procedures for My SQL Server. You can apply the same principles to other database management systems, but I will make clear references to working in a My SQL Server environment.

You May Consider this things also

Before creating a stored procedure, consider that:

* CREATE PROCEDURE statements cannot be combined with other SQL statements in a single batch.

* To create procedures, you must have CREATE PROCEDURE permission in the database and ALTER permission on the schema in which the procedure is being created. For CLR stored procedures, you must either own the assembly referenced in <method_specifier>, or have REFERENCES permission on that assembly.

* Stored procedures are schema-scoped objects, and their names must follow the rules for identifiers.

* You can create a stored procedure only in the current database.

When creating a stored procedure, you should specify:

* Any input parameters and output parameters to the calling procedure or batch.

* The programming statements that perform operations in the database, including calling other procedures.

* The status value returned to the calling procedure or batch to indicate success or failure (and the reason for failure).

* Any error handling statements needed to catch and handle potential errors.

Error handing functions such as ERROR_LINE and ERROR_PROCEDURE can be specified in the stored procedure.
Read More

What are the elements of JSP

Leave a Comment
As part of JSP’s we can use various kinds of  JSP elements like



1)  Template Text

2)  Script let

3)  Declarations

4)  Jsp Directives

5)  Jsp Expressions

6)  Jsp Action Tags

7)  Jsp Custom Tags
Read More

Sunday, May 20, 2018

Creating the Java Class

Leave a Comment
Writing the First Program in Java



/* This is my First Program
To Display the message
Author:……….
Version:…….
Project:……….*/

import java.lang.*;
Class First {
 // we initialize variables and methods here.
 public static void main(String args[])
  //The Java program is executed by JVM by calling the Main Method.
  // The JVM starts execution of a java program from the main method().
  {
   System.out.println(Hello World);
   System.out.println(Welcome to Java);
  }
}


output:

Hello World.
Welcome to Java.
Read More

What is Cookies

Leave a Comment
What is Cookies





Cookies Definition:
                                            
The cookie file is a file that resides on the client machine. It contains data passed from web sites, so that web sites can communicate with this file when the same client returns. The web site only has access to the part of the cookie file that represents the interaction with that particular web site. Clearly the cookie file has caused some issues with respect to privacy. Considering that as consumers, we do not know what information is being stored in the file, we should become concerned! The cookie file was first developed in order to help sites with the transaction process of the web. Without a cookie file, web sites are not able to track a single user’s path through a web site, thus a transaction that required multiple pages (as most do) would simply not be workable.
Read More

Define Cookie Poisoning

Leave a Comment



What is cookie poisoning


The modification of or theft of a cookie in a user’s machine by an attacker in order to release personal information. If the cookie contains username and password, thieves can use their own computers and confiscated cookies to enter victims’ accounts. 
Read More