0
votes

i'm trying to display the result of a java function into a html page using java servlet and tomcat. I'm on Linux Mint 19.1 Cinnamon and using apache-tomcat-8.5.38 My problem is that when I try to map a servlet into the web.xml file it seems that it doesn't work :

here is my web.xml file :

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app >
  <servlet>
    <servlet-name>SearchEngineServlet</servlet-name>
    <servlet-class>SearchEngine</servlet-class>
  </servlet>

   <servlet-mapping>
    <servlet-name>SearchEngineServlet</servlet-name>
    <url-pattern>/servlet/*</url-pattern>
  </servlet-mapping>
</web-app>

here is my index.html file :

<!DOCTYPE html>
<html>
<head>
    <title>Search Engine</title>
</head>
<body>
    <div>

        <a href="search">Click to call Servlet</a>

        <form action="servlet/search" method="post">
            <input type="text" name="s">
            <input type="submit" value="Submit">
        </form>
    </div>
</body>
</html>

and here is my application named SearchEngine.java :

public class SearchEngine extends HttpServlet {

    private String mymsg;

    @Override
    public void init() throws ServletException 
    {      
        mymsg = "Http Servlet Demo";   
    }

    @Override
    public void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, 
        IOException 
    {      
        PrintWriter out = response.getWriter();
        out.println("<h1>" + mymsg + "</h1>");
    }
}

And my files are here :

web.xml -> /home/cubouyaka/web/apache-tomcat-8.5.38/webapps/SearchEngine/WEB-INF/web.xml
SearchEngine.java -> /home/cubouyaka/web/apache-tomcat-8.5.38/webapps/SearchEngine/WEB-INF/src/SearchEngine.java
index.html -> /home/cubouyaka/web/apache-tomcat-8.5.38/webapps/SearchEngine/index.html

Then I can access index.html, but when i'm trying to click the link to use the servlet I got an not file error saying that /home/cubouyaka/web/apache-tomcat-8.5.38/webapps/SearchEngine/servlet/search doen't exist, but I made the mapping in the web.xml so I do not understand why I'm getting this error.

Could someone help me please, I lost so much time already trying to fix this..

1
Where is your SearchEngine.class file?Selaron
Inside the classes reposiroty : /home/cubouyaka/web/apache-tomcat-8.5.38/webapps/SearchEngine/WEB-INF/classes/cubouyaka

1 Answers

0
votes

You can take a look at this post to get an answer when web.xml is loaded, but it is not an issue in your case.

Assuming that your context path is empty (bind to the root), the code is also correct. Just one thing is wrong:

<a href="search">Click to call Servlet</a>

The url to connect to your servlet that is mapped under servlet/* should be referenced also here.

<a href="servlet/search">Click to call Servlet</a>