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:



Hello World!

Auto created web.xml:




  Archetype Created Web Application

Auto created pom.xml:


  4.0.0
  tutorialspointexamples
  MavenWebProject
  war
  0.0.1-SNAPSHOT
  MavenWebProject Maven Webapp
  http://maven.apache.org
  
    
      junit
      junit
      3.8.1
      test
    
  
  
    MavenWebProject
  

Add dependencies in auto created pom.xml:


  4.0.0
  tutorialspointexamples
  MavenWebProject
  war
  0.0.1-SNAPSHOT
  MavenWebProject Maven Webapp
  http://maven.apache.org
  
    
    
        javax.servlet
        javax.servlet-api
        3.1.0
        provided
    
    
    
        javax.servlet.jsp
        javax.servlet.jsp-api
        2.3.1
        provided
    
    
    
      junit
      junit
      3.8.1
      test
    
  
  
    MavenWebProject
  


Update the auto created index.jsp



Maven JSP and Servlet Hello World

Enter Name:

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("

Hello "+name+ "

"); 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.




  Archetype Created Web Application
  
  	HelloWorld
  	/HelloWorld
  
  
  	helloWorld
  	/helloWorld
  

Please follow and like us:
Content Protection by DMCA.com