Create and run a servlet example (Eclipse,GlassFish)

Servlet is a java based server side web technology to develop dynamic web resource programs . Basic requirements are given below to create and run a servlet example .
Softwares : |
Java | (Download). |
GlassFish server | (Download). |
Eclipse | (Download). |
Jar files: |
javax.servlet.jar | (Download). |
javax.servlet.jsp.jar | (Download). |
Source files : |
welcomeServlet.class(servlet) | |
web.xml |
![]() |
Directory Structure :
Deployment : Right click on server , select Add and Remove , select the project and click finish button.
1. Web-client request comes to the Web-server , First it looks for the web application.If exists , it will search for url-pattern (/welcomeApp) in the web.xml from the WEB-INF folder.
2. Later it identifies the servlet-name (Logical Name) ,In our example it is ServletExe .
3. Next it checks for the exact match of the servlet-name (ServletExe) inside the <servlet> tag.
4. Next it checks the servlet-class (servletApp.welcomeServlet) .
5. Loads the servlet class (welcomeServlet.class) from WEB-INF/classes to the container.
6. The doGet() method will execute (see the Servlet Lifecycle).
7. Finally the response goes to the client window.
Example code to create and run a servlet :
welcomeServlet.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
/* * Author : java2db.com */ package servletApp; import java.io.IOException; import java.io.PrintWriter; import java.util.GregorianCalendar; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class welcomeServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { response.setContentType("text/html"); PrintWriter writer = response.getWriter(); writer.println("<h2><font color=green>Welcome to the Servlet World...</font></h2>"); writer.print("<font color=red>The local Date and time is :<font> "+new GregorianCalendar().getTime()); writer.close(); } catch(Exception exception) { exception.printStackTrace(); } }//End doGet }//End class |
web.xml
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>servletExe</servlet-name> <servlet-class>servletApp.welcomeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>servletExe</servlet-name> <url-pattern>/welcomeApp</url-pattern> </servlet-mapping> </web-app> |
URL to Run the Application :
http://localhost:8080/welcomeServletApp/welcomeApp
http :- The Hypertext Transfer Protocol
localhost :- Host name (if the server is located in a remote machine , use the IP(Ex :192.164.2.3) address).
8080 :- Port number of the server that can be hear the client requests.
welcomeServletApp :- Deployed web application name.
welcomeApp :- Url pattern of the servlet program (It is in web.xam).
Execution Result

Related Posts : |
please upload very very basic and small project like jsp+mysql , spring+mysql, Struts+mysql and so on. am very proud of your work