3

I'm using Tomcat8 server and i'm getting following error.

After deployment

Project Explorer

It's url is http://localhost:8080/WeatherWebApp When i'm submitting the details then it's giving this error. Here is WeatherServlet.java class

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 {
    }

}

Here is web.xml file

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

Here is index.html file too:-

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

I've also mentioned stylesheet.css file in comment. Please check it.

akshay_rahar
  • 1,661
  • 2
  • 18
  • 20

2 Answers2

5

The error shows that Tomcat is unable to create an instance of your WeatherServlet class.

You should make its constructor and other methods public too. You can even make use of the default constructor by removing the less accessible constructor:

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 {
    }

} 
Kedar Mhaswade
  • 4,535
  • 2
  • 25
  • 34
  • @Peter Maybe he is trying to learn this should be a supportive environment for newbies. The reason you need to make it public is answerd. [link] (https://stackoverflow.com/questions/9117331/why-to-use-public-modifier-in-servlet) – slm12slm slm12slm Dec 25 '20 at 17:48
2

Please provide a fully qualified class name on your web.xml. I was facing a similar issue and this is what fixed it.

  <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>
Eric Aya
  • 69,473
  • 35
  • 181
  • 253