1

I am learning java maven project, and i just want to know how i can properly connect my post form with my servlet when i submit the form. i tried to search but can't get the right answer that i need.

I'm working with eclipse and this my folder organization!

enter image description here

this is my servlet:

package servlet;

import java.io.IOException;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.RequestDispatcher;
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 javax.servlet.http.HttpSession;


@WebServlet(name="Init")
public class Init extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
    
    
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        /*Creation et initialisation du message */
    
        
     //creation de la session
     HttpSession session=request.getSession();
     
     //pseudo user
     String name=request.getParameter("pseudo");
     session.setAttribute("usersession", name);
    
     
     //redirection après application du servlet INIT
     this.getServletContext().getRequestDispatcher("/interface.jsp").forward(request, response);
     
    }
}

this is my web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
  <display-name>Archetype Created Web Application</display-name>
  
  
  
<servlet>
    <servlet-name>Init</servlet-name>
    <servlet-class>src.main.java.servlet.Init</servlet-class>
</servlet>


<servlet-mapping>
    <servlet-name>Init</servlet-name>
    <url-pattern>/test</url-pattern>
</servlet-mapping>
  
</web-app>

this is my index.jsp which contains my form:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <title>Chatons !</title>
</head>
<body>
<h1>Bienvenue sur Chatons.org</h1>
<form method="post" action="/java/servlet/Init">
    <p>
        Entrez votre pseudo :
        <input type="text" name="pseudo">
        <input type="submit" value="Connexion">
    </p>
</form>
</body>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
nothiern
  • 9
  • 1
  • 4

2 Answers2

1

Annotation (@WebServlet in your case) represents the metadata. If you use annotation, web.xml file (deployment descriptor) is not required but you should have Tomcat 7 as it will not run in previous versions of Tomcat server. @WebServlet annotation is used to map the servlet with the specified name.

The web application deployment descriptor web.xml has become optional in Servlet 3.0. Instead, the container at run time will process the annotations of the classes in WEB-INF/classes.

Use @WebServlet(value="/init") instead of yours with the 'name' and get rid of your web.xml descriptor if you prefer annotations.

If you have your src/servlet/Init.java this class is compiled and it will be stored as .class in the following hierarchy:

build/classes/servlet/Init.class

When the container sees the annotation above the class-definition it will check for the .class file in the specified folder and it resolves the call for the required Servlet.

If you have the <form action="init" method="post"> when you hit the submit button the container then searches for the url pattern in the annotation and rest works as said before.

dcalap
  • 1,048
  • 2
  • 13
  • 37
  • I tried to apply what u said, so i remove the servlet mapping from web.xml but it does'nt work. – nothiern Dec 02 '18 at 17:39
  • You are not reading well... I haven't said just remove the servlet mapping, I said "...web.xml file (deployment descriptor) is not required" maybe you are not applying correctly my solution. Regards. – dcalap Dec 02 '18 at 18:25
0

For servlet 3.0 api you don't need a web.xml, you can use annotations instead, have you corrected the action in html

Rohit Sidhwani
  • 104
  • 1
  • 8