Mayank Kumar Thakur
Mayank Kumar Thakur

Reputation: 704

Create a servlet in VScode maven project

I have created a maven-archetype-webapp in VScode and created a folder Servlets in src/main/. This folder Servlets contain servlets. And I have also added tomcat server but when I am trying to run this server error - [Tomcat 9.0]: 'C:\Program' is not recognized as an internal or external command, operable program or batch file is keep coming on console.

Also when I try to run the project with tomcat server it is showing - this folder is not a valid webapp

[![It is the tree structure of my project][1]][1] [1]: https://i.sstatic.net/YPV3K.png

Code of FirstServlet.java -

import javax.servlet.http.HttpServlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;

public class FirstServlet extends HttpServlet{
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException  {
        
        System.out.println("this is get method....");
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        out.println("<h1> this is get method of servlet</h2>");
        
        
    }

}

This is my web.xml file -

<!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>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>


  <servlet>
    <servlet-name>first</servlet-name>
    <servlet-class>FirstServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>first</servlet-name>
    <url-pattern>/first</url-pattern>
  </servlet-mapping>

</web-app>

In this file, web-app tag is showing a red line with error message - The web-app element is the root of the deployment descriptor for a web application.

Source: web-app_2_3.dtd

The content of element type "web-app" must match "(icon?,display-name?,description?,distributable?,context-param*,filter*,filter-mapping*,listener*,servlet*,servlet-mapping*,session-config?,mime-mapping*,welcome-file-list?,error-page*,taglib*,resource-env-ref*,resource-ref*,security-constraint*,login-config?,security-role*,env-entry*,ejb-ref*,ejb-local-ref*)".xml(MSG_CONTENT_INVALID)

Upvotes: 0

Views: 3798

Answers (1)

Molly Wang-MSFT
Molly Wang-MSFT

Reputation: 9521

Java: Create Java Project ---> Maven ---> maven-archetype-webapp:

enter image description here

Upvotes: 1

Related Questions