Http status 500实例化servlet类时出错

时间:2016-03-31 17:44:23

标签: java servlets

我正在使用Tomcat8服务器而且我遇到了以下错误。

After deployment

Project Explorer

它的网址是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文件。请检查一下。

2 个答案:

答案 0 :(得分:2)

错误显示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 {
    }

} 

答案 1 :(得分:0)

请在您的 web.xml 中提供完全限定的类名。我遇到了类似的问题,这就是解决它的方法。

  <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>org.akshayrahar.WeatherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/CurrentWeather</url-pattern>
    </servlet-mapping>