Setup development environment with Eclipse and Tomcat

In this tutorial, we will guide you through step by step instructions how to setup and create development environment for dynamic web application using eclipse and tomcat. Unlike with static Web projects, dynamic Web projects enable you to create resources such as JavaServer Pages and servlets. Dynamic web projects can contain dynamic Java EE resources such as servlets, JSP files, filters, and associated metadata, in addition to static resources such as images and HTML files. Static web projects only contains static resources.


To create a new dynamic Web project, complete the following steps:
  • Install Tomcat 7.0 or later. Call the home directory of tomcat $TOMCAT_HOME.
  • Open Eclipse and Click File -> New -> Other and select Dynamic Web Project  and click Next as shown in below image

  • Provide the project name and location. Select 3.0 option for Dynamic web module version.

  • Click on New Runtime button under Target runtime section and select Apache Tomcat 7.0 environment under Apache option. Click Next.

  • Click on Browse and select the directory where Tomcat is installed. Provide the JRE and click Finish.

  •  Keep clicking Next button on parent window until it does not appear. Click Finish. Following structure will be created.

  • src is the java source code folder under which various java files (like servlets) will be put. WebContent is the root folder under which various web contents like jsp, html etc will be put. Put any jar files under WEB-INF/lib.
  • Put your web.xml under WEB-INF folder. Sample web.xml is given below.
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
          PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
          "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
 
 <!--
  ###########################################################################
  # # # S E R V L E T S # # #
  ###########################################################################
 -->

 
 <!--
  ###########################################################################
  # # # S E R V L E T - M A P P I N G S # # #
  ###########################################################################
 -->

 <!-- JUnitEEServlet mapping -->
 
</web-app>


  • Create login.jsp as follow and put this file under WEB-INF/jsp folder

    <html>
    <head>
        <title>Spring 3 MVC Series - Login</title>
    </head>
    <body>
    <form method="post" action="login.do"  >
        <table>
        <tr>
            <td>Username :</td>
              <td><input type="text" name="username" /></td>
        </tr>
        <tr>
            <td>Password : </td>
            <td><input type="text" name="password" /></td>
        </tr>
         <tr>
            <td colspan="2">
                <input type="submit" value="Login"/>
            </td>
        </tr>
    </table>  
    </form:form>
    </body>
    </html>
    

  • Now click Windows -> Show View -> Servers. In the empty area of server view, right click and select New -> Server. And select Tomcat v7.0 Server. Make sure Apache Tomcat v7.0 is selected in  'Server Runtime Environment' Dropdown. Click Next.

  • Select MyWebProject in the left panel and move it to right panel by clicking Add > button. And click Finish.

  • This would show as below in server view. Right click Tomcat v7.0 Server at localhost and click clean. Right click on Tomcat v7.0 Server and click on Start. Open http://localhost:8080/MyWebProject/jsp/login.jsp. And there you go. You will see login.jsp as below.






No comments:

Post a Comment