This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

CSS Drop Down Menu

Thursday, 20 February 2014

Filter

A filter dynamically intercepts requests and responses to transform or use the information contained in the requests or responses. Filters typically do not themselves create responses, but instead provide universal functions that can be "attached" to any type of servlet or JSP page.
In Other words , A filter is an object that is used to perform filtering tasks such as conversion, log maintain, compression, encryption and decryption, input validation etc.
A filter is invoked at the preprocessing and postprocessing of a request. It is pluggable, i.e. its entry is defined in the web.xml file, if we remove the entry of filter from the web.xml file, filter will be removed automatically and we don't need to change the servlet. So it will be easier to maintain the web application.

Usage of Filter:-

Filters are used for different functions as below:-
1-Authentication-Blocking- requests based on user identity.
2-Logging and auditing-Tracking users of a web application
3- Image conversion-Scaling maps, and so on.
4- Data compression-Making downloads smaller.
5- Localization-Targeting the request and response to a particular locale
6- XSL/T transformations of XML content-Targeting web application responses to more that one type of client.
7- to encapsulate recurring tasks in reusable units
8- encryption and decryption
9- mime-type chaining, and caching

Difference Between Servlet And Filter:-

FilterServlet
1-Filter is used for filtering the request and perform some action like authenticity of session, user is valid or not for that request, etc.
1-Servlet is used for performing the action which needs to be taken for particular request like user login, get the response based on user role, interacts with database for getting the data, business logic execution etc
2-Filters differ from web components in that filters usually do not themselves create a response.
2-Servlet technology is used to create web application where it creates response of client’s request themselves.
3- For authorization, a Filter is the best suited This is because they can be configured to run for all pages of a site. So you only need one filter to protect all your pages.
3-Not as much as comparison to Filter.

Note:-

Authentication is the process of determining whether someone or something is, in fact, who or what it is declared to be. In private and public computer networks (including the Internet), authentication is commonly done through the use of logon passwords.
Authorization is the function of specifying access rights to resources, which is related to information security and computer security in general and to access control in particular.

Filter API

Like servlet filter have its own API.The javax.servlet package contains the three interfaces of Filter API
1. Filter
2. FilterChain
3.FilterConfig

How It works:-

1-When You define a filter by implementing the Filter interface. A filter chain, passed to a filter by the container, provides a mechanism for invoking a series of filters. A filter config contains initialization data.
2-The most important method in the Filter interface is the doFilter method, which is the heart of the filter. This method usually performs some of the following actions:-
[a]-Examines the request headers
[b]-Customizes the request object if it wishes to modify request headers or data or block the request entirely
[c]-Customizes the response object if it wishes to modify response headers or data
[d]-Invokes the next entity in the filter chain. If the current filter is the last filter in the chain that ends with the target servlet, the next entity is the resource at the end of the chain; otherwise, it is the next filter that was configured. It invokes the next entity by calling the doFilter method on the chain object (passing in the request and response it was called with, or the wrapped versions it may have created). Alternatively, it can choose to block the request by not making the call to invoke the next entity. In the latter case, the filter is responsible for filling out the response.
[e]-Examines response headers after it has invoked the next filter in the chain
[f]-Throws an exception to indicate an error in processing

Note:-

In addition to doFilter, you must implement the init and destroy methods. The init method is called by the container when the filter is instantiated. If you wish to pass initialization parameters to the filter you retrieve them from the FilterConfig object passed to init.

Tuesday, 11 February 2014

Servlet:Life Cycle

Life Cycle Of Servlet

Web container maintains the life cycle of the Servlet.
1-Servlet classes are loaded
2-init method is invoked.
3-service method is invoked.
4-destroy method is invoked.

Life Cycle in Detail:-

1-When a server loads a servlet, it runs the servlet's init method. Even though most servlets are run in multi-threaded servers, there are no concurrency issues during servlet initialization. This is because the server calls the init method once, when it loads the servlet, and will not call it again unless it is reloading the servlet. The server can not reload a servlet until after it has removed the servlet by calling the destroy method. Initialization is allowed to complete before client requests are handled (that is, before the service method is called) or the servlet is destroyed.
2-After the server loads and initializes the servlet, the servlet is able to handle client requests. It processes them in its service method. Each client's request has its call to the service method run in its own servlet thread: the method receives the client's request, and sends the client its response.
3-Servlets can run multiple service methods at a time. It is important, therefore, that service methods be written in a thread-safe manner. If, for some reason, a server should not run multiple service methods concurrently, the servlet should implement the SingleThreadModel interface. This interface guarantees that no two threads will execute the servlet's service methods concurrently.
4-Servlets run until they are removed from the service. When a server removes a servlet, it runs the servlet's destroy method. The method is run once; the server will not run it again until after it reloads and reinitializes the servlet.
5-When the destroy method runs, however, other threads might be running service requests. If, in cleaning up, it is necessary to access shared resources, that access should be synchronized. During a servlet's lifecycle, it is important to write thread-safe code for destroying the servlet and, unless the servlet implements the SingleThreadModel interface, servicing client requests.

Steps to create Servlet Without Using any IDE:-

1- First of All Create the project folder structure as given below in Image. Here, we can put any name (project name) in place of web-app(context root).
2-Now , Create servlet class and keep inside the Classes folder.Here created SampleHello.java as Below:-
3- Now ,Create a welcome file ,Here Created index.jsp as below:-
4- Now Configure web.xml ,as given below:-
5- Now ,compile the servlet class what you have created here is SampleHello.java using command prompt for compilation of class as below:-
javac –d “dir_of_servletclass_if_not_in_same_dir” –cp “path_of_jar; You_can_add_another_jar_also_after_putting_semicolon” classname.java
since,Here I am in same directory of servlet class so-
javac –cp “D:\customtagjsp\servlet-api-2.5-6.1.5.jar” SampleHello.java
6-Now can see the .class file after compilation,Put it inside the classes folder of project structure and put projectfolder in side the webapps folder of Tomcat server of any version.
Here I am using apache-tomcat-7.0.47, D:\apache-tomcat-7.0.47\webapps\projectfolder
7-Run the server and then run the project as below:- http://localhost:8080/samplehello/ Now we can see the output as below:-




after submitting the form:-

Monday, 10 February 2014

Servlet:Introduction and Architecture Overview

What is Servlet??

A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. For such applications, Java Servlet technology defines HTTP-specific servlet classees. The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines lifecycle methods. When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.
Java Servlets often serve the same purpose as programs implemented using the Common Gateway Interface (CGI) script. But Servlets offer several advantages in comparison with the CGI.

Advantages Of Servlet Over CGI:-

Initially, Common Gateway Interface (CGI) server-side scripts were the main technology used to generate dynamic content. Although widely used, CGI scripting technology had many shortcomings, including platform dependence and lack of scalability. CGI Technology enables webserver to call an external program and pass Http request information to external program to process the request .For Each request , it starts with new Process. To address these limitations, Java Servlet technology was created as a portable way to provide dynamic, user-oriented content.
In case of servlet. The web container create threads for handling the multiple request to the servlet.since threads have a lots of benefits over process. For more information about thread and process click here

Some Example Applications where servlets could be used:-

1-A few of the many applications for servlets include,Processing data POSTed over HTTPS using an HTML form, including purchase order or credit card data. A servlet like this could be part of an order-entry and processing system, working with product and inventory databases, and perhaps an on-line payment system.
2-Allowing collaboration between people. A servlet can handle multiple requests concurrently; they can synchronize requests to support systems such as on-line conferencing.
3- Forwarding requests. Servlets can forward requests to other servers and servlets. This allows them to be used to balance load among several servers that mirror the same content. It also allows them to be used to partition a single logical service over several servers, according to task type or organizational boundaries.

Architecture Overview:-

All servlets must implement the Servlet interface, which defines lifecycle methods. When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.You can be clear by given below image:-
The Servlet interface provides the following methods that manage the servlet and its communications with clients.
1- destroy() Cleans up whatever resources are being held and makes sure that any persistent state is synchronized with the servlet's current in-memory state.
2- getServletConfig() Returns a servlet config object, which contains any initialization parameters and startup configuration for this servlet.
3- getServletInfo() Returns a string containing information about the servlet, such as its author, version, and copyright.
4- init(ServletConfig) Initializes the servlet. Run once before any requests can be serviced.
5- service(ServletRequest, ServletResponse) Carries out a single request from the client.
Servlet writers provide some or all of these methods when developing a servlet.

How Servlet Works:-

1-When a servlet accepts a service call from a client, it receives two objects, ServletRequest and ServletResponse. The ServletRequest class encapsulates the communication from the client to the server, while the ServletResponse class encapsulates the communication from the servlet back to the client.
2-The ServletRequest interface allows the servlet access to information such as the names of the parameters passed in by the client, the protocol (scheme) being used by the client, and the names of the remote host that made the request and the server that received it. It also provides the servlet with access to the input stream, ServletInputStream, through which the servlet gets data from clients that are using application protocols such as the HTTP POST and PUT methods. Subclasses of ServletRequest allow the servlet to retrieve more protocol-specific data. For example, HttpServletRequest contains methods for accessing HTTP-specific header information.
3-The ServletResponse interface gives the servlet methods for replying to the client. It allows the servlet to set the content length and mime type of the reply, and provides an output stream, ServletOutputStream, and a Writer through which the servlet can send the reply data. Subclasses of ServletResponse give the servlet more protocol-specific capabilities. For example, HttpServletResponse contains methods that allow the servlet to manipulate HTTP-specific header information.
The classes and interfaces described above make up a basic Servlet. HTTP servlets have some additional objects that provide session-tracking capabilities. The servlet writer can use these APIs to maintain state between the servlet and the client that persists across multiple connections during some time period.

Servlet API

Two packages:
1-javax.servlet-represents interfaces and classes for the Servlet API.This is not specific for any protocol.
2-javax.servlet.http -represents interfaces and classes for the Servlet API.But this is specific for only http request .

Servlet API Package as per jdk1.6

javax.servlet package:--

The javax.servlet package contains a number of classes and interfaces that describe and define the contracts between a servlet class and the runtime environment provided for an instance of such a class by a conforming servlet container.
Interface
AsyncContext
AsyncListener
Filter
FilterChain
FilterConfig
RequestDispatcher
Servlet
ServletConfig
ServletContext
ServletRequest
ServletResponse
Class
GenericServlet
ServletContextEvent
ServletInputStream
ServletOutputStream
ServletRequestAttributeEvent
ServletRequestWrapper
ServletSecurityElement

javax.servlet.http package:--

Interface
HttpServletRequest
HttpServletResponse
HttpSession
HttpSessionAttributeListener
HttpSessionContext
Class
Cookie
HttpServlet
HttpServletRequestWrapper
HttpServletResponseWrapper
HttpSessionBindingEvent
HttpSessionEvent

Sunday, 9 February 2014

JSP:Custom Tags

Custom Tags:-

A custom tag is a user-defined JSP language element. When a JSP page containing a custom tag is translated into a servlet, the tag is converted to operations on an object called a tag handler. The Web container then invokes those operations when the JSP page's servlet is executed.
Advantages of Custom Tags:-
1-Elimenates the need of scriptlate tags which is considered as bad programming approach.

2-Sepeartion of business logic from the jsp so tat it may be easy to maintain.

3-Reusability:-It makes the possibility to make reusability of business logic.

Syntax of Custom Tag:-

There are two ways to use the custom tags:-

1-

2-

Example Of Custom Tag:-

For creating the Custom Tag,We need to follow the following steps:-
1:- Create The tag Handler Class and perform action at the start or at end of tag.
2-Create The tag library descriptor file and define tags
3-Create the JSP file that uses the Custom Tag defined in TLD file.
1:- Create a dynamic project in your IDE(Here I am using Spring Tool Suite™) you can use eclipse or any IDEBut in case of Eclipse you will have to add jsp-api 2.0 jar
The JSP 2.0 specification introduced Simple Tag Handlers for writing these custom tags. To write a customer tab you can simply extend SimpleTagSupport class and override the doTag() method, where you can place your code to generate content for the tag.
2:- Create The tag Handler Class and perform action at the start or at end of tag--
here we create the MyTagHandler.java as given below

3-Now Create tag library file

here we create mytags.tld and put inside the following as mentioned below:- directory Dyanamic_Project_Name\WebContent\WEB-INF\mytags.tld


4-Create the JSP file that uses the Custom Tag defined in TLD file.Here we create index.jsp and in web.xml set as welcome page as given below

1:-web.xml:-



2:-index.jsp:-



Here prefix I have taken as prefix="Sample" and taglib uri="WEB-INF/mytags.tld"
when I run this dynamic project will get the value what tried to print like here will print Hello Custom Tag.
created custom tag without body content like fashion.

Note:-

Spring Tool Suite™ is an Eclipse-based development environment that is customized for developing Spring applications. It provides a ready-to-use environment to implement, debug, run, and deploy your Spring applications, including integrations for Pivotal tc Server, Pivotal Cloud Foundry, Git, Maven, AspectJ, and comes on top of the latest Eclipse releases.It is freely available for development and internal business operations use with no time limits, fully open-source and licensed under the terms of the Eclipse Public License.