1

I'm new in Java servlets so I'll be appreciate for understanding. I am struggling with servlet. After sending registration form from html, servlet is not called. I literally tried many things to set it up, but every time I've got error:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Jan 13 11:23:30 GMT 2020 There was an unexpected error (type=Not Found, status=404). No message available

It seems like html for is no redirecting to my servlet. I've even put simple sout do init() and anything happend.

Here's my java class: I've even tried different annotations, like instead urlPatterns just name ="/registration" or just same "/registration"

@WebServlet(urlPatterns = "/registration")
public class Register extends HttpServlet {


public void init() throws ServletException {
    System.out.println("init");
}


@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    System.out.println("post");
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    String name = request.getParameter("userName");
    String password = request.getParameter("password");
    String email = request.getParameter("email");

    System.out.println(name + password + email);

}

Here is my html file: in action="" I've tried put "registration" and result is same as with "/registration"

<!DOCTYPE html>
<html>
<head>
    <title>Enter Your Name</title>
</head>
<body>

    <form action="/registration" method="post">
    <input type="text" name="userName">
    <input type="email" name="email">
    <input type="password" name="password">
    <input type="submit">
</form>
</body>
</html>

Here's my .xml file:

<?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"
>
    <!-- Config here. -->
</web-app>

Insted of annotations I've tried to configure servlet by web.xml but it still didn't work.

EDIT:

Firstly I'm oppening http://localhost:8080/addUser.html, then passing datas. Secondly I'm redirected to http://localhost:8080/registration but the error is same.

spazzola
  • 299
  • 1
  • 3
  • 12
  • Are you sure this is a Post ? Have you tried implementing `doGet()` and calling `doPost()` in it ? – Philippe B. Jan 13 '20 at 13:48
  • I didn't. I'm not sure that here is problem, first of all the method init() should be called but it isn't. It does mean that html file isn't connected with my java class. – spazzola Jan 13 '20 at 14:20
  • Instead of trying to work with a specific url, can you first try with urlPattern as `*` or `*.*` ? This will help you to understand if problem lies with url pattern or something else. – Sabir Khan Jan 13 '20 at 14:29
  • I've tried it and result is same. – spazzola Jan 13 '20 at 15:09

0 Answers0