Dynamic web Project with eclipse
Pre-requestic i am assuming you have already installed jdk 1.5+ and eclipse (Euroepa,Ganeymede,Galideo) if not please visit first How to Install/Configure Eclipse
and Tomcat is installed already , if you are not aware about Tomcat insallation please refer How to install Tomcat
and Tomcat is Configured with eclipse if not refer How to configure Tomcat with Eclipse
Step-1 Start Eclipse by clicking eclipse.exe or shortcut and after select workspace you will get the eclipse home and then select File->New->Dynamic Web project to create Dynamic Web Project as shown below
Step-2 Enter Project name and click finish as shown below.
Step-3 after clicking finish button , you will get next window as shown below.
Step-4 now Select window->show view->navigator to change view as shown below.
Step-5 now your project browser window looks as shown below.
Step-6 To create new Servlet Right Click on src folder new->other as shown below
Step-7 Select Servlet in Web Category as shown below
Step-8 Type Servlet Name eg: LoginServlet and java package as web and click next
Step-9 Change URL Mappings by double clicking on that as shown below, i am using /login as url-pattern as shown below.
Step-10 Change the LoginServlet code as given below
package web;
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 LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(“text/html”);
PrintWriter out=response.getWriter();
out.println(“<h1>Welcome in Dynamic Project with Servlets </h1>”);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
Step-11 open web.xml and select source tab as shown below
Step-12 now right click on server(tomcat) added in servers tab and select Add and Remove as shown below
Step-13 add delhi project by clicking Add Button as shown below
Step-14 To Run Servlet Right Click on LoginServlet -> Run As -> Run on Server
Step-15 click finish button as shown below.
Step-16 and you will get Output in Browser Window.
转载自:http://javateacher.co.in/eclipse/dynamic-web-project-with-eclipse/