0

I am building a simple dynamic web app that displays the weather of a given place. Initially, It was running fine but when I created the servlet, the tomcat server did not get started. I am attaching all the files in my project. any help would be appreciated.

  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>

2 Answers2

0

Note: Class name should start with an uppercase character.

You should map the custom servlet class in web.xml where the request should be sent.

Proper web.xml should be :

<?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>    
Anish B.
  • 9,111
  • 3
  • 21
  • 41
  • 1
    No, the same problem persists !! – Vipin Chhillar Jun 22 '20 at 11:18
  • @VipinChhillar How this is possible ? Are you getting any exceptions ? – Anish B. Jun 22 '20 at 11:19
  • java.lang.IllegalArgumentException: The servlets named [Servlet1] and [pack1.servlet1] are both mapped to the url-pattern [/servlet1] which is not permitted – Vipin Chhillar Jun 22 '20 at 11:28
  • @VipinChhillar Just copy the contents of my web.xml and replace with your's and just replace package by your package in which the class exists. – Anish B. Jun 22 '20 at 11:30
  • 1
    actually I figured out the mistake...it was in web.xml as well as the json parser so yes the issue is partially resolved.. the server is running but now I need to parse the json object thanks!! – Vipin Chhillar Jun 22 '20 at 12:13
0

Check the names of servlet you have used in <form action = "/abc"> and in the annotation it is @WebServlet("/servlet1"). Either map the servlet to the form action in web.xml file or use the Servlet name in the HTML form.

helvete
  • 2,455
  • 13
  • 33
  • 37
arcane_J
  • 1
  • 3