由于 web.xml 或 servlet.java,位于本地主机的服务器 Tomcat v8.5 服务器无法启动 Eclipse

Server Tomcat v8.5 Server at localhost failed to start Eclipse due to web.xml or servlet.java

我正在构建一个简单的动态网络应用程序来显示给定地点的天气。最初,运行 没问题,但是当我创建 servlet 时,tomcat 服务器没有启动。我附上了我项目中的所有文件。任何帮助将不胜感激。

  1. home.html
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    
    <h1>About</h1> <br>
    My introduction.....
    <form action="geolocation.html">
        <input type="submit" value="Go to geolocation page" />
    </form>
    
    </body>
    </html>

  1. geolocation.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>GeoLocation Page</h1> <br>
<form action="/abc" method="GET">
    Enter the place: <input type="text" name="place" > <br>  
    <input type= "submit" value="submit">
</form>

 
</body>
</html>
  1. Servlet1.java
package pack1;

import java.io.IOException;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.json.simple.JSONArray; 
import org.json.simple.JSONObject; 
import org.json.simple.parser.*;



/**
 * Servlet implementation class servlet1
 */
@WebServlet("/servlet1")
public class servlet1 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public servlet1() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/plain;charset=UTF-8");
        
        String place= request.getParameter("place");
        
        
        
        BufferedReader br = null;
        String query;

        try {

            URL url = new URL("https://api.mapbox.com/geocoding/v5/mapbox.places/"+place+".json?access_token=pk.eyJ1IjoidmlwaW5jIiwiYSI6ImNrYjlpdWs1NjBlZjczM2pwd2R6Y3o5djkifQ.ULsLgK5_7oQ8Vf3JHhnnig");
            br = new BufferedReader(new InputStreamReader(url.openStream()));

            String line;

            StringBuilder sb = new StringBuilder();

            while ((line = br.readLine()) != null) {

                sb.append(line);
                sb.append(System.lineSeparator());
            }

//              System.out.println(sb);
                query = sb.toString();
            
        } finally {

            if (br != null) {
                br.close();
            }
        }
        int index=query.indexOf("coordinates");
        
        query = query.substring(index,index+32);
        int  index1 = query.indexOf("[");
        int index2 = query.indexOf("]");
        query = query.substring(index1+1,index2);
        double latitude,longitude;
        String s1,s2;
        s1 = query.substring(0,query.length()/2);
        s2 = query.substring(query.length()/2+1,query.length());
        longitude= Double.parseDouble(s1);
        latitude = Double.parseDouble(s2);
        
        try {

            URL url = new URL("http://api.openweathermap.org/data/2.5/weather?lat="+latitude+"&lon="+longitude+"&appid=370084bf98bc2a7fd4e580302b642c42");
            br = new BufferedReader(new InputStreamReader(url.openStream()));

            String line;

            StringBuilder sb = new StringBuilder();

            while ((line = br.readLine()) != null) {

                sb.append(line);
                sb.append(System.lineSeparator());
            }

//              System.out.println(sb);
                query = sb.toString();
            
        } finally {

            if (br != null) {
                br.close();
            }
        }
        
        Object obj = null;
        try {
            obj = new JSONParser().parse(query);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        
        // typecasting obj to JSONObject 
        JSONObject jo = (JSONObject) obj; 
        
        String name = (String) jo.get("name"); 
        System.out.println("name="+name);
        
        

        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}


  1. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>foo</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>
</web-app>

注意: Class 名称应以大写字符开头。

您应该将自定义 servlet class 映射到请求所在的 web.xml 应该发送。

正确的web.xml应该是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>foo</display-name>
  <servlet>
    <servlet-name>Servlet1</servlet-name>
    <servlet-class>package.Servlet1</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Servlet1</servlet-name>
    <url-pattern>/servlet1</url-pattern>
  </servlet-mapping>
</web-app>    

检查您在 <form action = "/abc"> 中使用的 servlet 的名称,在注释中它是 @WebServlet("/servlet1")。要么将 servlet 映射到 web.xml 文件中的表单操作,要么使用 HTML 表单中的 Servlet 名称。