Saturday, November 21, 2015

A simple custom tag

Defining a simple custom tag: To demonstrate the life cycle of a custom tag practically, I am creating a simple Tag class, its TLD file and a JSP page. This tag doesn’t do anything fancy, its purpose is just to let you understand how a custom tag works. Here is the Source code of the […]
Read Complete Post...

Tag Library Descriptor

Tag Library Descriptor: A Tag Library Descriptor (TLD) is a XML file crated by the tag programmer to associate custom tags to their Tag classes. With the help of a TLD file, a Tag programmer groups the logically related tags to a library. It is used by the Tag Processor to identify the Tag classes […]
Read Complete Post...

TagSupport class of Tag API

The TagSupport class: TagSupport is a helper class provided by Tag API. It implements IterationTag interface which is a sub interface of Tag. It is used as a super class by the custom tag classes. As an application programmer defines a Servlet by extending HttpServlet class similarly he/she can define a Tag by extending the […]
Read Complete Post...

Wednesday, November 18, 2015

Role of PageContext in custom tags

What is PageContext? PageContext is an interface of the JSP API. Implementation of it, is provided by server vendors. An object of type PageContext is created by the server for each request of a JSP page to store the references of the Servlet & JSP API objects which participate in the processing of the request. […]
Read Complete Post..

Tuesday, November 17, 2015

Tag life cycle methods

Tag life cycle: Tag life cycle describes how and when a tag object is created, initialized, and is used by the tag processor for processing a custom tag. Tag life cycle is defined by javax.servlet.jsp.tagext.Tag interface with the help of following methods: setPageContext(): It is the first method which is invoked by the tag processor […]
Read Complete Post...

Introduction to JSP Custom Tags

Introduction to JSP custom tags: Custom tags are application programmers’ defined actions which represent presentation logic as reusable components. The facility of defining custom tags makes the JSP extensible. To understand the use of custom tags, lets consider the following scenario. Let there be a web application in which 10 pages displays user data in […]
Read Complete Post...

Wednesday, October 28, 2015

JSP Page Directive

JSP page directive: The page directive in JSP is used by programmers to get the structure of the auto generated servlet modified according to their requirements. It has following syntax: Commonly used attributes of the page directive are: 1. import: This attribute is used to get the specified packages imported in the auto generated servlet […]
Read Complete Post...

Tuesday, October 27, 2015

JSP include directive

JSP include directive? include directive in JSP, is used by application programmers to get the contents of a page included to the current JSP at the time of translation. It has following syntax: You may recall that <jsp:include …/> action also has the same functionality. Now you should be wondering why are both the action […]
Read Complete Post...

JSP Directive

What is a directive? A directive in JSP, represents an instruction to the translator to modify the structure of the auto generated servlet at the time of translation on behalf of the programmer. You have been already told that for each JSP, a servlet class is auto generated. Sometimes modifications are required in this servlet […]
Read Complete Post...

Wednesday, October 7, 2015

Practical example of external objects in JSP

Practical use of external objects in JSP: To demonstrate the use of external objects with the help of useBean, setProperty & getProperty actions, I have created a simple web application. Following diagram describes the flow of application: Description of the diagram: Source code of the application: First, the home page index.html. <b>Rectangle Home</b> <form action="rect.jsp"> […]
Read Complete Post...

Tuesday, October 6, 2015

JSP getProperty action tag


getProperty action of JSP: <jsp:getProperty /> action is used to write the value a bean property to the output stream. It is used when a property value is to be sent as response. It has following syntax: Let following be an external object in a JSP page. To write the value of mailId property of […]
Read Complete Post...

JSP useBean action tag

External objects in JSP: In a JSP page, two types of objects can be used: 1. implicit objects 2. external objects Implicit objects are Servlet & JSP API objects which are made available by default in the _jspService() method. External objects are objects of user defined or library classes which are created in the _jspService() […]
Read Complete Post...

Action tags of JSP

The Action tags of JSP: Action tags in JSP facilitate automation of common operations such as creation of objects, setting objects’ properties, writing objects’ property values to the output stream, including the contents of a component to the response of current request and forwarding request to another component etc. An action tag has following syntax: […]
Read Complete Post...

Monday, October 5, 2015

Expression tag of JSP

The expression tag of JSP: Expression tag in JSP has two use. 1. It is used to write the value of an expression to the output stream i.e. It provides a short hand mechanism of evaluating expressions and writing their values to the response. 2. It is used to assign the value of variables and […]
Read Complete Post...

Declaration tag of JSP

The declaration tag of JSP: declaration tag in JSP facilitate data members and methods definition in the auto generated servlet. Main use of this tag is the overriding of jspInit() and jspDestroy() methods. You have been told that for each JSP a servlet is auto generated at the time of translation. If this class was […]
Read Complete Post...

Saturday, October 3, 2015

Scriptlet tag of JSP

The scriptlet tag of JSP: Scriptlet is the main tag of JSP. It is used to add request processing logic to a JSP page. A JSP page may contain any number of scriptlet tags. It has following syntax: At the time of translation, contents of the scriptlet tags are copied as it is to the […]
Read Complete Post...

understanding JSP life cycle

The JSP life cycle: JSP life cycle is defined by JspPage and HttpJspPage interfaces. Similar to servlet, JSP life cycle has three methods: jspInit(), jspDestroy() and _jspService() . javax.servlet.jsp package contains classes and interface of JSP API. Main classes and interfaces of the JSP API are: JspPage: It extends Servlet interface and provides following JSP […]
Read Complete Post...

Introduction to JSP

Limitations of servlet: Servlet as web application development technology has following drawbacks: 1. Developing web applications using servlet is unproductive i.e. a lot of code need to written even for simple tasks. 2. A servlet is responsible for dynamic content generation as well as presentation. This intermixing of dynamic content generation logic with the presentation […]
Read Complete Post...

Servlet 3 file upload example

Steps required to upload a file: 1. In html form, request type is set to post and enctype is set to multipart/form-data. 2. <input type=”file” > element is used in the html form to facilitate selection of the file from the file system. 3. In the servlet multi part request is parsed to obtain file […]
Read Complete Post...

Practical use of @WebServlet and other annotations

To demonstrate the practical use of servlet annotations, I have created a web application which have two servlets (ProcessingServlet & ForwardingServlet), a listener (HitCounterListener), a filter (HitCounterFilter) and a HTML page (index.html). When the application is deployed, HitCounterListener sets an attribute named hitCount in the application scope to count the no. of hits of the […]
Read Complete Post...

Servlet annotations and their advantages over xml

What is an annotation? An annotation is a class member, the purpose of which is to describe other members of the class. The literal meaning of the word annotation is: a note added to describe or clarify something. In real life we all use annotations. When you write the meaning of a word over it […]
 Read Complete Post...

Filter and request dispatching

Filter and request dispatching: In addition to a direct request, a servlet can be invoked internally through forwarding or inclusion. If a filter is associated to a servlet then will it inovke when a request is internally forwarded to the servlet or its response is included to another servlet’s response? By default, a filter which […]
Read Complete Post...

Practical example of a filter

A scenario for the practical use of a filter: To demonstrate the practical use of a filter in a web application, I am creating a filter which checks the URI of each incoming request. If the request is for the home page or the servlet which authenticates, simply requested component is invoked by the filter. […]
Read Complete Post...
 

What is the role of FilterChain?

What is FilterChain? FilterChain is an interface of the servlet API. Implementation of it, is provided by server vendors. It provides following method: You may recall that the Filter interface also has doFilter() method. That method has three parameters one of which is a FilterChain object. The doFilter() method of the Filter interface is used […]
Read Complete Post...

Filter life cycle and the role of FilterConfig

Filter life cycle: Filter life cycle describes how and when a filter object is created and initialzed, how it performs pre & post processing and how and when it is destroyed by the server. Filter life cycle is defined by javax.servlet.Filter interface with the help of following methods: init(): This method is invoked by the […]
Read Complete Post...

What is a filter?

What is a filter? A filter is an optional component of a web application which is used to perform pre and post processing operations such as logging, validation, flow control etc. Filters are associated to the servlets and HTML pages of the applications. Whenever a request is received for these components, associated filter is invoked. […]
Read Complete Post...

Monday, September 28, 2015

Practical example of listeners

A scenario for the use of listeners in a web application Usually each web application has two types of interfaces, one for the users and other for the admin. Users are interested in the information or services provided by the application and the admin is interested in the statistics such as how many users are […]
Read Complete Post...

Wednesday, September 23, 2015

Listeners and web application events

What is a listener? A listener is an optional component of a Java web application which facilitate event processing. An event represents a change of application state e.g. when an application is deployed its state is changed from undeployed to deployed. Performing some operations when an event occurs, is called event processing e.g Let there […]
Read Complete Post...

Tuesday, September 22, 2015

Session tracking and URL encoding

Session tracking by the server In the last post you were told that a HttpSession object can be get created on the server for each user. Before that you were also told that the server doesn’t remember a client after a request is processed. Now the question arises, If a server doesn’t remember a client […]
Read Complete Post

Monday, September 21, 2015

state management using url rewriting

what is URL rewriting? URL rewriting is another browser independent approach of maintaining client’s state between requests. In this approach, the information which is to be maintained between requests, is dynamically appended as parameters to the URL in the response page. When a request is submitted using these URL, appended information is made available as […]
Read complete post

State management using hidden form field

what is a hidden form field A hidden form field is an invisible text field which is added to a page to persist some information between two requests. <intput type=hidden> HTML element is used to create a hidden text field. Any number of hidden text fields can be added to a form dynamically. Differences between […]
Read complete post

state management using cookie

In the last post we have understood the need of state management and the concept of cookies. In this post we will cover a practical example of state management using cookie in a web application. For this we need to learn how cookies are represented and managed in Java. Object representation of cookies In a […]
Read complete post

What is a cookie and why is it needed?

What is a cookie? A cookie is a token which is generated by the server or a servlet and is sent as part of the response to be received back from the client as part of the subsequent requests. This token contains some information of the client in the form of key-value pair. What is […]
Read complete post

file downloading example of servlet

Steps required for file downloading using a servlet? File downloading is a two step process. In the first step, links of the files to be downloaded are obtained. In the second, a file is downloaded using the link. Both the steps require basic knowledge of Java input/output. The only important things are: 1. content type […]
Read complete post

file uploading example of servlet

Steps required to upload a file: 1. In html form, request type is set to post and enctype is set to multipart/form-data. 2. <input type=”file” > element is used in the html form to facilitate selection of the file from the file system. 3. In the servlet multi part request is parsed to obtain file […]
Read complete post

reading unknown parameters in a servlet

When unknown parameters are used in a servlet? Sometimes input forms are not available to the servlet programmers i.e. the programmer doesn’t know how many parameters are submitted by the user as part of the request . In such a case, the servlet need to defined in such a way that all the parameter name […]
Read complete post

Reading multi valued parameters in a servlet

Need of multi valued parameters in a servlet? Sometimes input forms have check boxes and combo boxes which allow selection of multiple values. The parameters which are used for such input controls have multiple values. How multi valued parameters can be used in servlet? ServletRequest interface provides getParameterValues() method for reading all the values of […]
Read complete post

Creating servlet objects at the time of deployment

Can servlet objects be created at the time of deployment? In servlet, it is taught that a servlet object is created by the server when first request is received for it. This is not the complete truth, actually it is the default behavior of the server which can be overridden i.e. Sevlets objects can be […]
Read complete post

Creating more than one servlet objects

Can more than one servlet objects be created by the server? Most of the people reply no. They assume that only one object of a servlet can be created by the server in an application. This is the default behavior which can be overridden i.e. we can ask the server to create as many objects […]
Read complete post

Invoking destroy() method of a servlet explicitly?

Can destory() method of a servlet be invoked explicitly? This is the most tricky question which is asked in interviews. Usually following answers are given of this question: All these answers are wrong and represents ignorance of the concept. The purpose of the destroy() method is not to destroy the servlet but to perform clean […]
Read complete post

what is request redirection in servlet?

Request redirection vs Request Forwarding Two understand the difference between request redirection and forwarding, lets take the following analogy. Lets assume that in Tech Mentro, Mr. X is the receptionist, Mr. Y is the manager and Mr. Z was the former manager who now works in some other company. A person calls to talk to […]
Read complete post

Inter application forwarding using ServletContext

Usually web applications have different modules such as end user module, admin module, service module etc. These modules can be packaged in a single application or can be deployed as separate applications on the same server. If they are deployed as separate applications then they need to forward requests between them. Such forwarding of request […]
Read complete post

Reading initialization parameters from ServletContext

Let’s understand the need of initialization parameters: In the practical example of RequestDispatcher, we used DividerServlet & HandlerServlet. Lets have another look at the them. DividerServlet code snippet: package com.techmentro.learningpad; … public class DividerServlet extends HttpServlet { public void doPost(HttpServletRequest request,HttpServletResponse response) { try { … //RequestDispatcher is created for index.html RequestDispatcher rd=request.getRequestDispatcher("index.html"); … }catch(Exception […]
Read complete post

What are initialization parameters?

Often servlets need external resources such as property files, folders, database drivers etc, for request processing. Hard coding these resources into the Servlets creates maintenance problem because if their name, value or location is changed, the Servlets need to be modified. To facilitate the use of external resources in servlets in such a way that […]
Read complete post

Practical example of RequestDispatcher.

As before starting a journey, we prepare an itinerary which states where we will go and how we will reach there. Similarly we should have a clear roadmap stating our objective and plan of action i.e. what we are going to do and how we will do it. The following diagram represents the roadmap of […]
Read complete post

What are the differences between attributes and parameters?

In the last post, you were told that more than one servlet can participate in the processing of a request. In such a case participating servlets may need to share their processing result with each other. Attributes facilitate this sharing of intermediate state of servlets during request processing. Differences between attributes and parameters: Following analogy […]
Read complete post

What is RequestDispatcher?

Before we move on the the RequestDispatcher, lets take an analogy to understand request processing. A servlet as you know is a web application component which processes request and generates dynamic contents. In real life any party which does the same can be taken as servlet. In Tech Mentro, we have receptionists who receive customer […]
Read complete post

How a request is mapped to doGet() or doPost()?

Have you ever thought how a request is mapped to doGet() and doPost() methods that we define in our Servlet classes. The Servlet interface which defines the life cycle of servlets provides service() method for request processing. The server doesn’t know doGet() and doPost() methods. It simply invokes service() method whenever a request is received […]
Read complete post

java web application deployment modes and formats

Understanding java web application deployment Deployment is the process of uploading a web application on a web server in such a format that it can be managed and executed by the server. Servlet API, defines standard formats for packaging and distributing java web applications. A Java web application can be deployed in the following two […]
Read complete post

A simple java web application

When you learned programming chances are that the first meaningful program you wrote was to add two numbers. To continue the convention, we will be doing the same in our first java web application. Usually requests are submitted to web applications from a browser which supports HTML interface hence to receive two numbers from the […]
Read complete post

What is the difference between web and application server?

In order to execute a Java application, A web or application server having a servlet container is required. A web server knows only how to intercept HTTP requests and how to return contents of static web pages. It doesn’t know how to execute servlets. A servlet container extends the functionality of a web server by […]
Read complete post

Difference between get and post request?

Get request is designed to fetch (read) contents  from the server and the Post request is designed to submit (save or update) contents on the server. This perspective of receiving and giving provides the base for understanding all their differences. Difference between get and post request: In get request, data are sent as part of the […]
Read complete post

what is the difference b/w static & dynamic website?

In the current information age, web sites have become an important tools in the hands of governments, business organizations and individuals to reach out to their target audiences beyond geographical boundaries. In a simple, technical jargon free and easy to understand way: A web site is a collection of web pages which are stored on […]
Read complete post

How a Servlet is defined?

A servlet class can be defined by directly implementing the Servlet interface or by extending a helper class javax.servlet.GenericServlet provided by servlet API. It is an abstract class, which implements Servlet interface and defines all its methods except service() i.e. In its sub class, an application programmer need to define only the service method. Following […]
Read complete post

Servlet life cycle

Servlet life cycle describes how and when a servlet object is created and initialzed, how it processes requests and how and when it is destroyed by the server. Servlet life cycle is defined by javax.servlet.Servlet interface. Life cycle methods of Servlet interface: init(): This method is invoked by the web server only once, just after […]
Read complete post

What is Servlet?

The term Servlet has two different meaning in two different contexts. In the broader context, it represents an API of dynamic web application development and in the narrow context, it represent a Java class which is defined using this API for processing requests in a web application. As API, Servlet contains interfaces and classes which […]
Read complete post

What is a cookie and why is it needed?

What is a cookie? A cookie is a token which is generated by the server or a servlet and is sent as part of the response to be received back from the client as part of the subsequent requests. This token contains some information of the client in the form of key-value pair. What is […]