Saturday, 18 May 2013
Interfaces and Classes in Servlet
Interfaces in javax.servlet package:
- Servlet
- ServletRequest
- ServletResponse
- RequestDispatcher
- ServletConfig
- ServletContext
- SingleThreadModel
- Filter
- FilterConfig
- FilterChain
- ServletRequestListener
- ServletRequestAttributeListener
- ServletContextLitstener
- ServletContextAttributeListener
- GenericServlet
- ServetInputStream
- ServletOutputStream
- ServletRequestWrapper
- ServletResponseWrapper
- ServletRequestEvent
- ServletContextEvent
- ServletRequestAttributeEvent
- ServletContextAttributeEvent
- ServletException
- UnavailableException
- HttpServletRequest
- HttpServletResponse
- HttpSession
- HttpSessionListener
- HttpSessionAttributeListener
- HttpSessionBindingListener
- HttpSessionActivationListener
- HttpSessionContext (deprecated now)
- HttpServlet
- Cookie
- HttpServletRequestWrapper
- HttpServletResponseWrapper
- HttpSessionEvent
- HttpSessionBindingEvent
- HttpUtils (depricated now)
History of Java
James Gosling, Mike Sheridan, and Patrick Naughton initiated the java language project in June 1991.
Originally designed for small. embedded systems in electronic appliances like set-top boxes. Initially called Oak was renamed as "Java". Java is just a name not an acronym.
Originally developed by james Gosling at SUN Microsystems( which is now a subsidiary of oracle Corporation) and released in 1995. JDK 1.0 released in (January 23,1996).
Originally designed for small. embedded systems in electronic appliances like set-top boxes. Initially called Oak was renamed as "Java". Java is just a name not an acronym.
Originally developed by james Gosling at SUN Microsystems( which is now a subsidiary of oracle Corporation) and released in 1995. JDK 1.0 released in (January 23,1996).
Servlet Interface
Servlet Interface provides common behavior to all the servlet Servlet interface needs to be implemented for creating any servlet (either directly or indirectly). It provides 3 life cycle methods that are used to initialize the servlet, to service the requests and to destroy the servlet and 2 non-life cycle methods.
Methods of servlet Interface: There are 5 methods in servlet interface. The init, service and destroy are the life cycle method these are invoked by the web container.
Methods of servlet Interface: There are 5 methods in servlet interface. The init, service and destroy are the life cycle method these are invoked by the web container.
- public void init(ServletConfig config) initializes the servlet. It is the life cycle method invoked by the web container only once.
- public void service(ServletRequest request, ServletResponse response) provides response for the incoming request. It is invoked at each request by the web container.
- public void destroy() is invoked only once and indicates that servlet is being destroyed.
- public ServletCofig getServletConfig() returns the object of ServletCofig.
- public String getServletInfo() returns information about servlet such as writer, copyright,version etc.
import java.io.*;
import javax.servlet.*;
public class First implements Servlet{
ServletConfig config=null;
public void init(ServletConfig config){
this.config=config;
System.out.println("servlet is initialized");
}
public void service(ServletRequest req, ServletResponse res)throws IOException, ServletException{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.print("<html>");<body>");
out.print("<b>Hai</b>");
out.print("</html></body>");
}
public void destroy(){
System.out.println("Servlet is destroyed");
}
pulbic ServletCofig getServletConfig(){
return config;
}
public String getServletInfo(){
return "copyrith @ futureimpact"
}
Thursday, 16 May 2013
HTML 5 TAGS Browser support
HTML 5:
The below tags which are comes with HTML5 and which are supported in All Major browsers and not supported with.
The below tags which are comes with HTML5 and which are supported in All Major browsers and not supported with.
Tags/Attributes | Browser support |
---|---|
contenteditable | All Browsers |
contextmenu | present not supported any browser |
accessKey | Not supported by Opera |
draggable | IE8 and earlier versions, safari 5.1.2 Not supported |
Contenteditable in HTML
Contenteditable : It is new in HTML 5. This Attribute is supported in all major browsers. The contenteditable attribute specifies whether the content of an element is editable or not.
Syntax: < element contenteditable="true|false|inherit">
The attribute values of this contenteditable are true, false, inherit ( inherit specifies that the element is editable if its parent is)
Syntax: < element contenteditable="true|false|inherit">
The attribute values of this contenteditable are true, false, inherit ( inherit specifies that the element is editable if its parent is)
Access Key in HTML
Access Key: To specify a shortcut key to access/focus a specific element in your HTML page use <accessKey>
This accessKey attribute works in all major browsers except Opera.
In HTML 5 the accessKey attribute can be used to any HTML element (it will validate on any HTML element.).
In HTML 4 the accessKey attribute can be used with <a> , <area> , <button> , <input> <label> , <legend> , <textarea>
The syntax: <element accessKey="character">
This accessKey attribute works in all major browsers except Opera.
In HTML 5 the accessKey attribute can be used to any HTML element (it will validate on any HTML element.).
In HTML 4 the accessKey attribute can be used with <a> , <area> , <button> , <input> <label> , <legend> , <textarea>
The syntax: <element accessKey="character">
Thursday, 9 May 2013
Cascading Order
Multiple Styles Will Cascade into One
Styles can be specified:
Styles can be specified:
- inside an HTML element
- inside the head section of an HTML page
- in an external CSS file
Cascading order:
What style will be used when there is more than one style specified for an HTML element?
Generally speaking we can say that all the styles will "cascade" into a new "virtual" style sheet by the following rules, where number 4 has the highest priority:
Generally speaking we can say that all the styles will "cascade" into a new "virtual" style sheet by the following rules, where number 4 has the highest priority:
- Browser default
- External style sheet
- Internal style sheet (in the head section)
- Inline style (inside an HTML element)
So, an inline style (inside an HTML element) has the highest priority, which means that it will override a style defined inside the tag, or in an external style sheet, or in a browser (a default value).
Design Patterns Intro
The Design Pattern is not a framework and is not directly deployed via code.
Design Pattern have two main usages:
Design pattern are based on the base principles of object orientated design.Design Pattern have two main usages:
- Common language for developers: they provide developer a common langauage for certain problems. for example if a developer tells another developer that he is using a singleton, the another developer (Should) know exactly what is singleton.
- Capture best practices: Design patterns capture solutions which have been applied to certain problems. By learning these patterns and the problem they are trying to solve a un experienced developer can learn a lot about software design.
- Program to an interface not an implementation
- Favor object composition over inheritance
- Creational Patterns
- Structural Patterns
- Behavioral Patterns
Creational Patterns:
- Factory Pattern
- Abstract Factory Pattern
- Singleton Pattern
- Builder Pattern
- Prototype Pattern
- Adapter Pattern
- Bridge pattern
- Composite Pattern
- Decorator Pattern
- Facade Pattern
- Flyweight Pattern
- Proxy Pattern
- Chain of responsibility Pattern
- Command Pattern
- Interpreter Pattern
- Iterator Pattern
- Mediator Pattern
- Momento Pattern
- Observer Pattern
- State Pattern
- Template Pattern
- Visitor Pattern
Factory Pattern
Factory of what? of classes. In simple words, if we have super class and n subclasses, and based on data provided, we have to return the object of one of the sub-classes, we use a factory pattern.
Let's take an example to understand this pattern.
Example: Let's suppose an application asks for entering the name and sex of a person. If the sex is male(M), it displays welcome message saying Hello Mr. and if the sex is Female (F), it displays message saying hello Ms.
The skeleton of the code can be given here.
public class Person{
//name string
public String name;
private String gender;
public String getName() {
return name;
}
public String getGender(){
return gender;
}
}
This is a simple class person having methods for name and gender. Now we will have two sub-classes, Male and Female which will print the welcome message on screen.
public class Male extends Person{
public Male(String fullName){
System.out.println("Hello Mr."+fullName);
}
}
Also the class Female
public class Female extends Person{
public Female(String fullName){
System.out.println("Hello Ms. "+fullName);
}
}
Now, we have to create a client, or a salutationFactory which will return the welcome messgae depending on the data provided.
public class SalutationFactory{
public static void main(String args[]){
SalutationFactory factory=new SalutationFactory();
factory.getPerson(args[0],args[1]);
}
public Person getPerson(String name, String gender){
if(gender.equals("M"))
return new Male(name);
else if(gender.equals("F"))
return new Female(name);
else
return null;
}
}
This class accepts two arguments from the system at runtime and prints the names.
Running the program:
After compiling and running the code on my computer with the arguments Srinivas and M:
java Srinivas M
the result returned is : "Hello Mr. Srinivas".
when to use a Factory Pattern?
the factory patterns can be used in following cases:
Let's take an example to understand this pattern.
Example: Let's suppose an application asks for entering the name and sex of a person. If the sex is male(M), it displays welcome message saying Hello Mr.
The skeleton of the code can be given here.
public class Person{
//name string
public String name;
private String gender;
public String getName() {
return name;
}
public String getGender(){
return gender;
}
}
This is a simple class person having methods for name and gender. Now we will have two sub-classes, Male and Female which will print the welcome message on screen.
public class Male extends Person{
public Male(String fullName){
System.out.println("Hello Mr."+fullName);
}
}
Also the class Female
public class Female extends Person{
public Female(String fullName){
System.out.println("Hello Ms. "+fullName);
}
}
Now, we have to create a client, or a salutationFactory which will return the welcome messgae depending on the data provided.
public class SalutationFactory{
public static void main(String args[]){
SalutationFactory factory=new SalutationFactory();
factory.getPerson(args[0],args[1]);
}
public Person getPerson(String name, String gender){
if(gender.equals("M"))
return new Male(name);
else if(gender.equals("F"))
return new Female(name);
else
return null;
}
}
This class accepts two arguments from the system at runtime and prints the names.
Running the program:
After compiling and running the code on my computer with the arguments Srinivas and M:
java Srinivas M
the result returned is : "Hello Mr. Srinivas".
when to use a Factory Pattern?
the factory patterns can be used in following cases:
- when a class does not know which class of objects it must create.
- a class specifies its sub-classes to specify which objects to create.
- In programmers language( very raw form), you can use factory pattern where you have to create an object of any one of sub-classes depending on the data provided.
Thursday, 2 May 2013
Error: Your content must have a ListView whose id attribute is android.R.id.list
When you are creating A ListVIew Item in
Android and want to display your selected item on Toast message. When you are
running that app and if you get an error like below.
"Your content must have a ListView whose
id attribute is android.R.id.list"
For the above problem answer is.
If your are using ListActivity then you must have ListView in your
xml of layout and must be id of
ListView is
android.R.id.list
So must add listview in your layout like below code
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/android:list"
/>
In The Java Code it’l
be get like this.
lv = (ListView)
findViewById(android.R.id.list);