java - Http 状态 500 实例化 servlet 类时出错

标签 java servlets

我正在使用 Tomcat8 服务器,但出现以下错误。

After deployment

Project Explorer

它的 url 是 http://localhost:8080/WeatherWebApp 当我提交详细信息时,它给出了这个错误。 这是 WeatherServlet.java 类

package org.akshayrahar;
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 WeatherServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    WeatherServlet(){

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("again");
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        out.println("akshay rahar");
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

}

这里是web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee">
  <display-name>WeatherWebApp</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>WeatherServlet</servlet-class>
    </servlet>
  <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/CurrentWeather</url-pattern>
    </servlet-mapping>
</web-app>

这里还有 index.html 文件:-

<!DOCTYPE html>
<html>
    <head>
        <title>Weather App</title>
        <link rel="stylesheet" type="text/css" href="stylesheet.css">
        <script >
      function initMap() {
        var input =document.getElementById('pac-input');

        var autocomplete = new google.maps.places.Autocomplete(input);
      }
    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyACZhmkSaIz436Dt3kHt_cVEYKN-gHzfYo&libraries=places&callback=initMap"async defer></script>
    </head>


    <body>
        <h1>Find weather of any city in the world</h1>
        <h2>Enter City Name</h2>

        <form  id="form" action="CurrentWeather" method="GET">
        <input id="pac-input" type="text" name="cityname">
        </form><br>

        <div class="button1">
        <button type="submit" form="form" value="Submit">Submit</button>
        </div>

    </body>
</html>

我还在评论中提到了 stylesheet.css 文件。请检查。

最佳答案

错误表明 Tomcat 无法创建您的 WeatherServlet 类的实例。

您也应该将其构造函数和其他方法设为public。您甚至可以通过删除不易访问的构造函数来使用默认构造函数:

public class WeatherServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public WeatherServlet(){

    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("again");
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        out.println("akshay rahar");
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

} 

关于java - Http 状态 500 实例化 servlet 类时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36340202/

相关文章:

java - jsoup:删除 iframe 标签

java - 一个简单的 java classCastException

java - InnerHTML 没有给 servlet 带来值(value)

servlets - 带有嵌入式 Jetty 的异步 Servlet

java - HttpServlet 是单例吗?

java - 在 Web 应用程序中重定向或转发

java - 无法从 JAVA 连接到 Mongo SSL 副本集

java - System property http.nonProxyHosts has been set to local|*.local... 是什么意思?这意味着什么?

java - 如何逐个字符读取文件直到完成特定字符串?

java - 将控制从被调用者 servlet 发送回调用者 jsp