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 […]