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!
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>