Apache tomcat Configuration in eclipse

Apache Tomcat Configuration in eclipse

Step 1: Go to J2EE perspective

c1

Step 2: Go to Window and select Show view and click on Server

c1

Step 3: Click link to add Server

c1

Step 4: Select Server

c1

Step 5: Select path where Extract your tomcat

c1

step 6: Now you can See server

server

Step to run Simple Static page into web server
Step 1: Create dynamic web project

c1

Step 2: Give proper name

c1

Step 3: Create html file

c1

Step 4: Give file name

c1

Step 5: index.html

c1

Step 6: Right click and select run as and Click Run on server

c1

Step 7: Click on finish

c1

Step 8: Now You can see result

c1

Note: Here we created simple static html page and run web application on web server.
Just observe web url
http://localhost:8980/JavaforLearn.com/index.html
–> We can access static web application through file name.

–> In case of dynamic web application we access it through configure web URL.

 

Simple Dynamic web Application using Servlet

Steps to create Servlet project in eclipse


  1.  Create the Dynamic web project
  2. Create Servlet in SRC folder
  3.  If you see compilation error then Download servlet.jar file and Copy that file into Webcontent/web-inf/lib Folder
  4. Configure Web URL for Servlet(in web.xml)
  5.  Start web server
  6. Now you can access web Application using web URL.

step 1: Create New Dynamic web project


e1

step 2: Give project name


e6
step 3: Expands Project folder


folder-structure

step 4: Create new package


e4

step 5: Give package name


screenshot_1

step 6: Right click on package and create new Servlet


e5

FirstServlet.java

package com.spy.javaforlearn;
 
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;
 
 
public class FirstServlet extends HttpServlet 
 
{
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
		         throws ServletException, IOException 
     {
 
    	 response.setContentType("text/html");
 
 
    	PrintWriter out= response.getWriter();
    	out.println("<h1>Welcome to javaforlearn.com</h2>");
 
     }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 
 
 <display-name>FirstServlet</display-name>
 
 <welcome-file-list>
 <welcome-file>index.html</welcome-file>
 <welcome-file>index.htm</welcome-file>
 <welcome-file>index.jsp</welcome-file>
 <welcome-file>default.html</welcome-file>
 <welcome-file>default.htm</welcome-file>
 <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
 
 
 <servlet>
 <servlet-name>FirstServlet</servlet-name>
 <servlet-class>com.spy.javaforlearn.FirstServlet</servlet-class>
 </servlet>
 
 
 <servlet-mapping>
 <servlet-name>FirstServlet</servlet-name>
 <url-pattern>/FirstServlet</url-pattern>
 </servlet-mapping>
 
</web-app>

Note:


  •  Web.xml is known as deployment descriptor for web application.
    It is an instruction sheet for web application.
  • If you have not configured web URL then Default it checks file in
    <welcome-file-list> attribute.
  • To configured Web URL We uses <servlet> and <servlet-mapping> two Attribute.
step 7: Click on run button and select tomcat server to run Servlet


e7

  • How to Configure Web url
    If we create dynamic web project using Module 2.5 then We usually configure web url in web.xml Above dynamic web project we created using module 2.5 so we configured web url in web.xml
  • Note: If we create dynamic web project using Module above 2.5 then We configure web url using annotation
How can we know which servlet is mapped to which url ?


  • <servlet> and <servlet-mapping> both have common attribute called
    <servlet-name> so we can identified through it.
Accessing web app using following web url


http://localhost:8980/FirstServlet/FirstServlet

1. http (protocol) :

  • Common language which used for communication between web browser
    and web server

2. localhost :

  • it is host name or ip address of pc

3. 8980 :

  • It is port number( It Uniquely identified web Application)

4. FirstServlet :

  • it is project name

5. FirstServlet :

  • Configured web url for Servlet