Skip to main content

Posts

Showing posts with the label J2EE

How to create Pagination Taglib in Java, Spring

Points To Remember Bootstrap is one of the major UI componnet used worldwide, So you may want to implemnt pagination using bootstrap in java , j2ee or spring application. So all you need to do is Create a Class that will behave like a taglib. Create a Tld file that will map the tablib class with the jsp's. Use the taglib on jsp using the tld file. Create Pagination Taglib for Jsp with Bootstrap. Following is an example of the a custom taglib built for Bootstrap . This taglib takes in uri -> action to be hit when clicked. offset -> the offset of pagination. count -> total number of elements to be shown. max -> maximum number of pages to be shown in the pagination bar. steps -> maximum number of elements to be shown per page. previous -> text to be shown for previous page link. next -> text to be shown for next page link. PaginationTaglib.java This is a java class that will behave like a taglib on jsp's. package com.ekiras.taglib; import java.io.Writer...

How to create Custom 404 page in Web Application

Points To Remember You can define your custom error pages in web.xml.  This is the best way to define custom 404 and other error pages. Configure Custom error pages Many times we want to create custom error pages in our web applications like for a 404 error, resource not found, 500 internal server error and other errors that we see commonly. So we may want to redirect such errors to a better looking custom error page. If you want to redirect to custom error pages in a web application you can add these custom error pages in web.xml like following. web.xml <error-page> <error-code>400</error-code> <location>/error/400.html</location> </error-page> <error-page> <error-code>404</error-code> <location>/error/404.html</location> </error-page> <error-page> <error-code>500</error-code> <location>/error/500.html</location> </error-page> <error-page> <e...

What is ServletConfig in Java J2EE application

Points To Remember Each Servlet has its own ServletConfig object. ServletConfig is defined in web.xml ServletConfig is a way of giving init parameters to a servlet during initialization.  ServletConfig is a predefined interface. ServeltConfig object is created by the container when a servlet is initialized and this object remains with the servlet throughout the life cycle of the servlet. Example : How to get ServletConfig Object in a Servlet. Lets take an example where we will add a few init params in the servlet config and get the config object from the servlet config object within the servlet. package com.ekiras; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletConfigTest extends HttpServlet { private static final long ...

Can a Servlet have a Constructor

Points To Remember Servlet is a java class that implements javax.servlet.Servlet interface. Servlets are initialized by the servlet containers using init() method . Servlets like any other java class can have constructors . Servlet must have a no argument constructor in case it have a non zero arguments constructor. Servlet needs an object of ServletConfig for initialization . Interfaces cannot have constructors. Can we have Constructors in a Servlet ? Yes we can, please read How a servlet is initialized by the servlet container.  So this article will make clear, why we cannot use a constructor for initializing a servlet. However there is no problem in having a constructor in a servlet. We can have both argumented and no arguments constructor in a servlet. Example What we are trying to do is to create a simple servlet with a constructor and assign a value to a String variable named as "msg". Servlet : ServletConstructor package com.ekiras.demo; import java.io.IOException; im...

How a Servlet is initialized by Servlet Container

Points To Remember Servlets are  initialized by the servlet containers . Servlets like any other java class  can have constructors . Servlet needs an object of ServletConfig for initialization . Interfaces cannot have constructors. Why a Servlet is initialized using init() method ? A servlet is a java class that implements javax.servlet.Servlet interface.A servlet can be given some init parameters in web.xml, called the  ServelConfig  and some global init parameters called the ServletContext. Thus they both are required at the initialization time of a servlet. Also Servlet is an interface so it cannot have a constructor, so how will we pass the ServletConfig  object to the servlet for its initialization. This is the reason the Servlet interface have a init(ServletConfig config) method and the implementing class needs to override this method to pass a ServletConfig object to it. GeneralServlet is an abstract class that implements the Servlet interface for us and ...

What is a JSP in Java J2EE.

Points To Remember JSP stands for Java Server Pages. Jsp at compilation time is a servlet and also behaves like a servlet. Jsp serves as views  in MVC architecture. Jsp can contain HTML code inside them. They are compiled everytime they are requested. What is a JSP ? JavaServer Pages (JSP) is a technology for developing dynamic web pages with dynamic data. You can write java code within a jsp along with the Html using special Jsp tags most of which start with <% and end with %>. A JavaServerPage component is a type of Java servlet that is designed to fulfill the role of a user interface for a Java web application. Web developers write JSPs as text files that combine HTML or XHTML code, XML elements, and embedded JSP actions and commands. Using JSP, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically. JSP tags can be used for a variety of purposes, such as retrieving information from a da...

Difference between ServletConfig and ServletContext

Difference : ServletContext vs ServletConfig ServletContext ServletConfig Servlet Context is a global configuration for servlets in a an application i.e this is same for all the servlets.  Servlet Config is a local configuration for each servlet in an application i.e each servlet has its own servlet configuration that may differ from the servlet config of another servlet. ServletContext is one per application. ServletConfig is one per servlet.  It is created at the time of deployment of the web application. It is initialized when a servlet is initialized.  Its scope is as long as the application is up and running, it will be destroyed when the application is removed from the server.  Its scope isand is destroyed when a servlet is destroyed by the servlet container.  In web.xml it should be declared as <context-param> tag and it should be under <web-app> tag. In web.xml it should be declared as <init-param> under <servlet-class> tag. Servl...

What are Filters in Java J2EE applications.

Points To Remember A filter is hit before the servlet or a controller is hit and before the response is sent back to the client. Filters can be used to apply some action on application level. A filter implements the javax.servlet.Filter interface and the primary filter functionality is implemented by the doFilter() method of the filter. Filters can be used to manipulate both requests from client and response from server. What is a Filter in Java Web Applications ? A filter is a useful way of performing filtering operations in java web applications bith before a request reaches the backend server and after the response is received from the backend server. A filter is basically used to filter things. Suppose you want to make your application accessible to a particular country, then you can apply IP filtering in your application, similarly we can have various types of filters like Authentication Filters Data compression Filters Encryption Filters Filters that trigger resource access event...