Eclipse maven servlet hello world

Eclipse maven servlet hello world:

Eclipse provides m2eclipse plugin to integrate Maven and Eclipse together.

Steps to create maven java web project in eclipse:

  1. In eclipse, click on File menu → New → Maven Project. Select maven-archetype-webapp template to create java project and Click on Next button.
  2. Now provide the group Id, artifact Id and Package. Click on Finish button. Complete directory structure and all files like web.xml file, pom.xml file, test case file etc will be created automatically.

Directory structure of the maven java web project:

Auto created index.jsp:

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

Auto created web.xml:

<!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>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

Auto created pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>tutorialspointexamples</groupId>
  <artifactId>MavenWebProject</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>MavenWebProject Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>MavenWebProject</finalName>
  </build>
</project>

Add dependencies in auto created pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>tutorialspointexamples</groupId>
  <artifactId>MavenWebProject</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>MavenWebProject Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <!-- Add servlet dependencies -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <!-- Add jsp dependencies -->
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
        <scope>provided</scope>
    </dependency>
    <!-- Add JUnit dependencies -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>MavenWebProject</finalName>
  </build>
</project>


Update the auto created index.jsp

<html>
<body>
<h2>Maven JSP and Servlet Hello World</h2>
<form action="helloWorld" method="post">
    Enter Name: <input type="text" name="name" size="20">
    <input type="submit" value="Say Helllo" />
</form>
</body>
</html>

New -> Servlet. Create HelloWorld servlet in your desire package and change accordingly.

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class HelloWorld
 */
public class HelloWorld extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
        /**
         * @see HttpServlet#HttpServlet()
         */
	@WebServlet("/helloWorld")
        public HelloWorld() {
            super();
            // TODO Auto-generated constructor stub
         }
 
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, 
                                       HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name").trim();
		response.setContentType("text/html"); 
    	        PrintWriter out = response.getWriter(); 
    	        out.print("<h2>Hello "+name+ "</h2>"); 
    	        out.close();
	}
 
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, 
                                       HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
 
}

Web.xml will update automatically.

<!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>
  <display-name>Archetype Created Web Application</display-name>
  <servlet-mapping>
  	<servlet-name>HelloWorld</servlet-name>
  	<url-pattern>/HelloWorld</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
  	<servlet-name>helloWorld</servlet-name>
  	<url-pattern>/helloWorld</url-pattern>
  </servlet-mapping>
</web-app>
Content Protection by DMCA.com
Please Share