-2

Im getting a servlet not found error. Im using WIldFly. My directory structure looks like this:

root --> app, converter.html, src

app --> WEB-INF

WEB-INF--> classes, lib, web.xml

src --> servlet.java

I have been looking over it for awhile and can't pin point the problem. I think I have the mapping down correctly in the web.xml and the form action seems to be sent to the right place as well in the .html file.

Servlet Class:

import java.io.IOException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class servlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException{
    String username = request.getParameter("username");
    String email = request.getParameter("email");
    response.getWriter().println("<html>");
    response.getWriter().println("<head>");
    response.getWriter().println("<title>Title</title>");
    response.getWriter().println("</head>");
    response.getWriter().println("<body>");
    response.getWriter().println("Convert. ");
    response.getWriter().println("</body>");
    response.getWriter().println("</html>");
}


}

web.xml

<web-app 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 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  version="2.4">

  <servlet>
    <servlet-name>servlet</servlet-name>
    <servlet-class>servlet</servlet-class>
  </servlet>  

  <servlet-mapping>
    <servlet-name>servlet</servlet-name>
    <url-pattern>/servlet</url-pattern>
  </servlet-mapping>

</web-app>

converter.html

<!DOCTYPE html>
<html>
<head>
        <title> Test form </title>
</head>
<body>
    <form action="http://localhost:8080/root/src/servlet" method="get">
        Name: <input type="text" name="username"><br>
        Email: <input type="text" name="email"><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>
Justin T.
  • 149
  • 2
  • 9

1 Answers1

-3

You need to fix the converter.html in the following ways:

  1. Change the action to action="servlet"
  2. Change the method from GET to POST since you want to send data to the server. The HTTP GET method is sued for retrieving data from the server.

You can read more on this link below: http://www.tutorialspoint.com/servlets/servlets-form-data.htm

user207421
  • 305,947
  • 44
  • 307
  • 483
  • He needs to change the URL to `"/servlet"` as per the `servlet-mapping` entry. If he changes to POST he will also have to change `doGet()` to `doPost()`. – user207421 Apr 17 '16 at 13:15
  • Asian-originated codesnippet-scraping sites full of advertisement banners who show off themselves as "tutorials" must be taken with a huge bag of salt (roseindia, tutorialspoint, javabeat, etc..etc..). They do this all purely for advertisement incomes. Please ignore and blacklist them. Instead, rely on Oracle's own tutorials and authoritative books (and of course highly voted answers on Stack Overflow). – BalusC Apr 17 '16 at 13:20
  • When you say change the URL to "/servlet", are you referring to the form action? so
    – Justin T. Apr 17 '16 at 13:22
  • Justin, your answer is in the duplicate link below your question. You made several mistakes which are all already pointed out there. – BalusC Apr 17 '16 at 13:23