Posts

Showing posts with the label Tomcat

Tomcat Introduction and Purpose of Different Folders

Image
If you are planning to develop web application in Java. The Java EE technology used to develop web applications is Servlets. Servlets are java classes that process the web requests and deliver response to browser. Putting these classes directly in just JVM is not possible, nor it would start processing web requests. We need some server to receive the HTTP requests (HTTP is the protocol the browser and server talks in), convert it into an object oriented representation (from stream of bytes written on server socket into objects understandable by our java classes i.e. Servlet) and pass the request to our servlets. So that we don't have to deal with lower level details of network programming like programming HTTP protocol to receive the requests and sending response to user browser. But we could focus on building business logic of our projects. Tomcat Tomcat is a server that receive web requests over the network, converts them into refined object oriented representation and dispatch t...

Java Web Programming - Running Tomcat and Creating Servlet to Handle a Form

Following topics are explained with demonstration: Downloading Tomcat 6 and extracting it  Running Tomcat and accessing default page from web browser to verify the installation  Making Java web project using IntelliJ IDEA and configuring Tomcat with IntelliJ  Creating an HTML form to submit data and a Servlet to receive the data. Don't forget to maximize the video screen (see bottom-right white arrows) to view the contents in good quality.   This is my first video lecture, so don't mind if you find some mistakes, as I am just testing how to create and publish video lectures.

How to Get Spring Managed Service Instance Outside the Action Class in Struts2

In Struts 2, we can configure Spring to inject service instanced using Dependency Injection into Action classes. But sometime we need these service class instances in other components who are are Spring aware. For example you need AccountsService instance in StudentService class. For such purpose I have createds util class which can return me reference to any service instance at run time, in any class. I call is SpringContext, here is its definition: package com.bitspedia.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class SpringContext implements ApplicationContextAware { private static ApplicationContext CONTEXT; public void setApplicationContext(ApplicationContext context) throws BeansException { CONTEXT = context; } public static Object getBean(String beanName) { return CONTEXT.getBean(beanName); } } Now getting the r...

How to Run a Code Immediately After the Web Application or Context is Deployed in Tomcat

If are writing a web app using Java (JSP or Servlets), sometime we want to execute a code section immediately after the application context has deployed. For examples to: Initialize Caching system Initialize some scheduling services Put some data is application scope that will be needed frequently. Before Servlets 2.4, such code was put in a normal Serlvet, which was configured in web.xml to run at startup. But in Servlets 2.4, there is explicit mechanism to do so. Here is how you can do it: Make a class MyListener that implements ServletContextListener Write the code you want to run only once (after the context is loaded, i.e. when the web application is deployed in Tomcat) in contextInitialized(ServletContextEvent cse) method. package mypackage; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class MyListener implements ServletContextListener { public void contextInitialized(ServletContextEvent cse)...