Outline:
1) Prerequesite
2) Eclipse Config
3) Create Web Project
4) Deploy Web Project to Tomcat
1. Prerequesite:
1. JDK 7 or above, environment variable config done 1) JAVA_HOME: C:\Program Files\Java\jdk1.6.0_21 2) PATH: C:\Program Files\Java\jdk1.6.0_21\bin 2. Tomcat 7, environment variable config done 1) CATALINA_HOME: D:\Program Files\apache-tomcat-7.0.47 2) PATH: %CATALINA_HOME%\bin
2. Eclipse Config
1) JDK
3. Create Web Project
1) Create Project
File --> New --> Other... --> Web --> Dynamic Web Project
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>WebProject</display-name> <servlet> <servlet-name>Welcome</servlet-name> <servlet-class>edu.xmu.webproject.WelcomeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Welcome</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3) WelcomeServlet.java
package edu.xmu.webproject; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class WelcomeServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String userAgent = req.getHeader("user-agent"); String clientBrowser = "NOT KNOWN"; if (null != userAgent) { clientBrowser = userAgent; } req.setAttribute("client.browser", clientBrowser); req.getRequestDispatcher("/showBrowser.jsp").forward(req, resp); } }
4) showBrowser.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <table border="1"> <tr> <th>Header</th> <th>Value</th> </tr> <tr> <td>user-agent</td> <td><%=request.getAttribute("client.browser")%></td> </tr> </table> </body> </html>
5) Project Overview:
4. Deploy Web Project to Tomcat
1) Run on Server
--> Will deploy our project to "D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps" directory.
--> Will add code below to server.xml
<Context docBase="WebProject" path="/WebProject" reloadable="true" source="org.eclipse.jst.jee.server:WebProject"/>
2) Change server.xml as "source" is originally not an attribute of Context tag.
<Context docBase="D:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/WebProject" path="/WebProject" reloadable="true"/>
3) Update configuration path and restart tomcat, warning below will disappear.
[SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:WebProject' did not find a matching property.
5. Start tomcat and browser. Test.
Reference Links:
1) http://www.eclipse.org/webtools/community/tutorials/BuildJ2EEWebApp/BuildJ2EEWebApp.html
2) http://hi.baidu.com/81667/item/1b791838603a6dc31b9696bb