Tout d'abord voci la page qui contient le formulaire :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | |
<title>Insert title here</title> | |
</head> | |
<body> | |
<form action="/DemoJsp/Maservlet"> | |
Nom | |
<input name="nom"> | |
Prenom | |
<input name="prenom"> | |
<input type="submit" value="Ok"> | |
</form> | |
</body> | |
</html> |
Voici le code de la servlet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ServletController; | |
import java.io.IOException; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
/** | |
* Servlet implementation class Maservlet | |
*/ | |
@WebServlet("/Maservlet") | |
public class Maservlet extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
/** | |
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) | |
*/ | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
// TODO Auto-generated method stub | |
String nom=request.getParameter("nom"); | |
String prenom=request.getParameter("prenom"); | |
request.setAttribute("idnom", nom); | |
request.setAttribute("idprenom", prenom); | |
getServletContext().getRequestDispatcher("/ResultatInscription.jsp").forward(request, response); | |
} | |
} |
request.getParameter("nom") et request.getParameter("prenom"):La recuperation des valeurs des champs.
request.setAttribute("idnom", nom) et request.setAttribute("idprenom", prenom): stocker les données dans des attributs
getServletContext().getRequestDispatcher("/ResultatInscription.jsp").forward(request, response): se rediriger vers la page ResultatInscription.jsp
voici le code de ResultatInscription.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" | |
pageEncoding="ISO-8859-1"%> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | |
<title>Insert title here</title> | |
</head> | |
<body> | |
Mr <%=request.getAttribute("idnom") %></br> | |
<%=request.getAttribute("idprenom") %></br> | |
est bien inscrit | |
</body> | |
</html> |
<%=request.getAttribute("idnom") %>: c'est un scriplet expression pour recupérer les attributs par leur id et les afficher.
voici le vidéo de l'exemple:
No comments:
Post a Comment