Steps to Create a Servlet
Today we all are aware of the need of creating dynamic web pages i.e the ones which have the capability to change the site contents according to the time or are able to generate the contents according to the request received by the client. If you like coding in Java, then you will be happy to know that using Java there also exists a way to generate dynamic web pages and that way is Java Servlet. But before we move forward with our topic let’s first understand the need for server-side extensions.
Servlets are the Java programs that run on the Java-enabled web server or application server. They are used to handle the request obtained from the webserver, process the request, produce the response, then send a response back to the webserver.
Properties of Servlets are as follows:
- Servlets work on the server-side.
- Servlets are capable of handling complex requests obtained from the webserver.
Creating WelcomeServlet.java using NetBeans IDE
Firstly We are Using NetBeans IDE For Creating the Servlet. So Now, Welcome Servlet.java simply displays Welcome <Username>!. Create WelcomeServlet.java using NetBeans IDE. Right-click Sources Package directory, select New > Servlet.. as shown in Fig.1. Then, the New Servlet dialog box appears.
Now, Enter Welcome Servlet in the Class Name textbox and enter servlet in the Package textbox as shown in diagram Fig.2.
Then, click Next. Now The Configure Servlet Deployment section of the New Servlet dialog box appears. Whereas, Keep the defaults but select the option Add information to deployment descriptor (web.xml) as shown in Fig. 3.
We Want to Create a deployment descriptor web.xml to take precedence over annotations. This means a deployment descriptor web.xml will be created instead of using Annotations. Enter the information Class name for the Servlet, A Servlet Name to it, and the URL Pattern(s) within for it. In the Servlet API version 3.0 onwards, the deployment descriptor [web.xml] takes precedence over annotations. Click On Finish.
Using the Servlet package, Servlet is Created Now With The Servlet named WelcomeServlet.java is created under the servlet package. The WelcomeServlet.java is available with the default skeleton created by NetBeans IDE which needs to be modified to hold the actual code spec.