Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Servlet in NetBeans IDE

In this tutorial I will guide you to create your first Servlet Application using NetBeansIDE. We will create Servlet Application in NetBeansIDE and will also deploy NetBeansIDE Web Application project in Apache Tomcat webapps folder.

Before starting with this tutorial, I would like to give some introduction about the Technologies which will be covered:

NetBeansIDE: An IDE written in Java which supports development of all Java Application types. It can run on Windows, Linux, Solaris etc and other platforms supporting compatible Java Virtual Machine (JVM).

Java: It is a programming language developed by James Gosling in 1995 which derives its syntax from C and C++.

Servlet: It is a Java programming language class developed by Sun Microsystems. It extends the capability of a server and provides dynamic content. The first Version was released in 1997 as Servlet 1.0 and current Version is Servlet 3.1

JSP (Java Server Pages): It is used to create dynamically generated web pages. In order to run and deploy JSP and Servlet, a compatible web server and web container (Servlet container) is required like Apache Tomcat, Glassfish, Jetty etc.

Apache Tomcat Server: It is an open source Web Server and Servlet Container available under Apache Software License:

Software/Technologies Used

IDE: Download NetBeansIDE 7.3

Server: Download Apache Tomcat 7

Java: Java 1.7

Create your first Servlet Application in NetBeansIDE

Step1: To create a Web Application, go to File -> New Project -> Java Web -> Web Application

Figure 1: Create new Web Application in NetBeansIDE

Step2: Click on Next and enter Project Name as “AmitServletWebApplication”, then click on Next:

Figure 2: Project Name: AmitServletWebApplication

Step3: Click on Next and select Server under Server and Settings, we are taking Apache Tomcat Server into consideration. Apache Tomcat is an open source Web Server and Servlet Container available under Apache Software License:

Figure 3: Select Server as Apache Tomcat

Step4: Click on Next, no need to select any Framework as we are creating a simple Web Application. Now, click on Finish:

Figure 4: Select frameworks to use (avoid it and click on Finish)

Step5: Project “AmitServletWebApplication” gets created; you can check Project Directory from below given screenshot:

Path: C:\Users\amit\Documents\NetBeansProjects\AmitServletWebApplication

Figure 5: AmitServletWebApplication Project Directory

Step6: Now to create Servlet File, you can Right Click on Project -> New -> Servlet:

Figure 6: Create Servlet

Step7: Enter Class Name and Location for newly created Servlet file:

Enter Class Name as ServletApplication, after that click on Next with Location as “Source Packages”:

Figure 7: Enter class name as ServletApplication

Step8: After Clicking Next as shown above, the following screenshot will be visible to Configure Servlet Deployment:

Select “Add information to deployment descriptor (web.xml)”

Servlet Name and URL-pattern can also be changed from here, change URL-pattern from “ServletApplication” to “home”. After that click on Finish:

Figure 8: Configure Servlet Deployment

Step9: After clicking on Finish above, ServletApplication.java gets created. Here, Servlet gets created by extending HttpServlet. The following changes are visible in Project Directory:

Path: C:\Users\amit\Documents\NetBeansProjects\AmitServletWebApplication\src\javaServletApplication.java

Figure 9: Changes visible under Project Directory

Step10: web.xml now consists of the following content:

Path: C:\Users\amit\Documents\NetBeansProjects\AmitServletWebApplication\web\WEB-INF\web.xml

What is a Deployment Descriptor?

web.xml is a configuration file that is used to run Servlet and JSP pages. It’s an XML document which describes the deployment of an application. Deployment Descriptor is called web.xml and is placed in WEB-INF folder.

Listing 1: web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>ServletApplication</servlet-name>
<servlet-class>ServletApplication</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletApplication</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
</web-app>

Here,

<servlet-name>: is the name given to Servlet

<servlet-class>: is the Servlet class created

<servlet-mapping>: helps in mapping internal name to URL

<url-pattern>: this url will be visible when Servlet runs


Step11: Add some code to ServletApplication.java,. Here we are printing some information related to Servlet as text using out.println:

Listing 2: ServletApplication.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
 
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
*
* @author amit
*/
public class ServletApplication extends HttpServlet {
 
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
 
}
 
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlets</title>");
out.println("</head>");
out.println("<body>");
out.println("<br /><br /><p>It extends the capability of server and provides dynamic content.</p>");
out.println("<p>The Servlet API is contained in the <i>javax.servlet</i> Java package hierarchy.</p>");
out.println("<p>First Version was released in 1997 as Servlet 1.0 and Current Version is Servlet 3.1 released
in May2013 </p>");
out.println("<br /><br /><a href=\"index.jsp\">Back</a>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
 
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
 
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
                                                                                                                                                                                        
}

Here,

setContentType

It is called before getWriter() and sets the content type of the response being sent to client

PrintWriter

This class prints formatted representations of objects to text-output stream.

getWriter()

This returns PrintWriterobject that can send character text to client.

println

This method terminates the current line by writing the line separator string.

HttpServlet

It is an implementation of theServletinterface.

Step12: Edit index.jsp

Listing 3: index.jsp

<%--
Document   : index
Created on : Jan 10, 2014, 12:24:51 PM
Author     : amit
--%>
 
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index Page</title>
</head>
<body>
<h2>Servlets</h2><br />
<hr />
<p>It is a Java programming language class developed by Sun Microsystems in 1997.</p>
<p>Click on the following link for more information:</p>
<!-- this will redirect to ServletApplication as URL-pattern for ServletApplication
was mentioned as /home  -->
<a href="home"><b>More...</b></a>
<hr />
</body>
                                              
</html>

Run the following project:

Figure 10: Running AmitServletWebApplication Project

If the project runs successfully, it will show the default jsp page index.jsp:

Figure 11: default jsp page

Now, click on “More…” to open the Servlet Page whose URL-pattern was mentioned as /home in web.xml. After clicking, the following Servlet Page will be visible:

Figure 12: Contents from ServletApplication.java visible here

Go back to index.jsp by clicking on Back hyperlink as shown above. After clicking Back, the following index.jsp will be visible again:

Figure 13: index.jsp

Deploy NetBeansIDE Web Application Project (with Servlet Application) in Apache Tomcat webapps folder:

What is a WAR File?

For deploying, we will use AmitServletWebApplication .war file which gets created whenever the project BUILDS successfully. WAR file is Web Application Archive file which is used to distribute a collection of JSP, Java Servlet, classes, static web pages etc. We can find war file in NetBeansIDE project’s dist folder.

For deploying, copy the war file i.e. “AmitServletWebApplication.war”and paste it to Apache Tomcat webapps folder. The same is explained below with proper screenshots:

Copy war file from here,

Path: C:\Users\amit\Documents\NetBeansProjects\AmitServletWebApplication\dist

Figure 14: Copy AmitServletWebApplication.war from here

Copy the above shown war file to Apache Tomcat webapps folder,

Path: C:\apache-tomcat-7.0.40\webapps

Figure 15: Copy war file to Apache Tomcat webapps folder

Let’s rename the war file placed in webapps folder to “NewServletProject.war” as shown below:

Figure 16: AmitServletWebApplication.war renamed to NewServletProject.war

Now, go to browser and type: http://localhost:8080/NewServletProject/ and then press Enter. You can now view the successful running of same project from webapps folder.

Note: Server should be in running state to perform the above operation successfully

Click on “More…” to run Servlet file with URL-pattern /home :

Figure 17: Runs successfully from Apache Tomcat webapps folder

After clicking “More…”, Servlet file opens correctly. This means that the project gets executed properly in Apache Tomcat webapps folder:

Figure 18: Contents from ServletApplication.java visible here in Apache Tomcat webapps folder

Before concluding, let us view the directory structure of NewServletProject under webapps folder. The war file copied here created NewServletProject folder with following directories and files:

Here, we can see index.jsp, META-INF and WEB-INF folder.

Path: C:\apache-tomcat-7.0.40\webapps\NewServletProject

Figure 19: NewServletProject directory

Here, we can see web.xml and classes under WEB-INF folder.

Path: C:\apache-tomcat-7.0.40\webapps\NewServletProject\WEB-INF

Figure 20: NewServletProject WEB-INF folder

Here, we can see ServletApplication.class file in WEB-INF\classes folder.

Path: C:\apache-tomcat-7.0.40\webapps\NewServletProject\WEB-INF\classes

Figure 21: NewServletProject WEB-INF classes folder

Finally, Project Directory Hierarchy can be seen here under Apache Tomcat webapps folder:

C:\apache-tomcat-7.0.40\webapps\NewServletProject NewServletProject->

---> META-INF

---> WEB-INF

-->classes

- ServletApplication.class

-->web.xml

---> index.jsp

Conclusion

Through this tutorial you can run Servlet Application in NetBeansIDE. My aim was to target NetBeansIDE users to help them in learning the following:

  • How to create WebApplication in NetBeansIDE.
  • How to create Servlet Application
  • What is Deployment Descriptor(web.xml)
  • How to edit deployment descriptor (web.xml) and change URL-pattern.
  • How to deploy NetBeansIDE Web Application project (with Servlet file) in Apache Tomcat webapps folder.
  • What is a WAR file


Have done Masters in Computer Applications (MCA) and currently working as a Website Developer, Software Developer and Technical Writer. Have successfully delivered various assignments while working as a Freelancer Technical Writer...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login