Pre-requisites:
1. Download and install maven 2
2. Download and install eclipse
3. Install maven 2 eclipse plugin from http://m2eclipse.codehaus.org/ update site from eclipse. Also, eclipse needs to know where maven repository is, which can be set using following command:
mvn -Declipse.workspace=eclipse:add-maven-repo
3. Download and install apache tomcat 5.5.23 (make sure you have jdk 1.5 or higher installed before installing tomcat)
Steps :
1. Now, go to eclipse-workspace directory and create a web application maven project using following command.
mvn archetype:create -DgroupId=org.test.webapp -DartifactId=HelloWorldWebApp -Dpackagename=org.test.webapp -DarchetypeArtifactId=maven-archetype-webapp
2. Go to HelloWorldWebApp directory
-open pom.xml add any necessary dependencies. for e.g
<dependency>
<groupid>jdbc</groupid>
<artifactid>oracle</artifactid>
<version>1.4</version>
</dependency>
- run following commands from command line to create an eclipse project :
mvn install
mvn eclipse:eclipse
3. Start a eclipse project with HelloWorldWebApp and now, you are ready to go.
You may add following in your .classpath file
<classpathentry kind="src" path="src/main/java">
4. Create a new class name it HelloWorldServlet:
Write your code :
5.Modify wrc\main\webapp\WEB-INF\web.xml
package org.test.webapp;
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 HelloWorldServlet extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.write("Hello World:doPost");
out.flush();
out.close();
return;
}
public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.write("Hello World:doGet");
out.flush();
out.close();
return;
}
}
For e.g :
<web-app>
<display-name>Hello World </display-name>
<description>
Provides Portlet Application
</description>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>org.test.webapp.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
6. To deploy :
compile using maven :
mvn clean compile war:war
deploy on tomcat
start tomcat
open a browser point to localhost:8080/manager/html
deploy the war file
try in your browser url : http://localhost:8080/hello
No comments:
Post a Comment