Deployment descriptor is an xml document named web.xml that is deployed with application and contains configuration settings for application.In this tutorial we are discussing some of the mandatory elements that need to defined in web.xml. Continue reading →
1) Java Servlets are efficient and powerful solution given by sun for creating dynamic content for the web.
2) Servlets are the main and central processing unit for web application
3) Java Servlets are designed to respond with dynamic content to client requests.
In this tutorial we will do Servlet HelloWorld example with Eclipse IDE and Tomcat Server.
1) Open Eclipse IDE workspace and Open File->New.It opens the following wizard .
2) Search for dynamic web project in the above wizard as follows :
3) Click on the Next button in the above wizard and click on the Next button which opens the below screen.
In the above screen give the name of the project in Project name screen as ServletsExamples and in Target runtime select the tomcat server version installed by clicking on the New Runtime button and click on Finish button.
4) It creates the dynamic web project with the following project structure.
5)In the above Project structure Create a new package ->com.servlets under src and create a new class HelloWorldServlet in above created package as shown below:
6) After creating project make sure servlet-api.jar is in your build path of your project .If not add servlet-api.jar to your build path as follows :
1) Right click on ServletExamples project and select Properties and then Select Buildpath option.
2) After that click on Libraries tab and click on AddExternal jars and select the servlet-api.jar which will be available in tomcat-server downloads.Whenever you download tomcat server we will get servlet-api.jar with tomcat server under lib folder.
3) If we don’t add servlet-api.jar to build path we get compilation errors.
7) In this step we will write a servlet class HelloWorldServlet by extending HelloWorldServlet.
Servlet interface has different doxxx methods to handle HTTP GET,POST,PUT,DELETE requests.In this example we are overriding doGet method.doGet method has two parameters :
1) HttpServletRequest and HttpServletResponse are used to capture the request and response of web cient.
2) Developer has to populate HTTPResponse and send the response to client/browser.
In the above example we overrided doGet method
HelloWorld Servlet extends HttpServlet and override get method and wrote the implementation which sends Hello World to the browser.
Every servlet application will have deployment descriptor. Web.xml is an xml document which contains mapping information of servlet and configuration settings of servlet.Make sure web.xml is placed in the project class path.
In java when a field is declared and not initialized,java compiler will be set the default value to the field value based on datatype of the field.Here are the list of datatypes and default values. Continue reading →