0% found this document useful (0 votes)
45 views5 pages

Corrigé

The document describes how to create a form to collect user information in HTML, validate the form data with a servlet, and display the user information on a JSP page. The HTML form uses inputs like text, date, select, radio buttons, and checkboxes to collect a user's name, date of birth, country, gender, email, password and programming languages. The servlet verifies that required fields are filled out correctly before storing the user data in a BeanUser object and session. The JSP page then displays the user information using JSP tags to access the session attribute or EL expressions to loop through languages.

Uploaded by

Nasser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views5 pages

Corrigé

The document describes how to create a form to collect user information in HTML, validate the form data with a servlet, and display the user information on a JSP page. The HTML form uses inputs like text, date, select, radio buttons, and checkboxes to collect a user's name, date of birth, country, gender, email, password and programming languages. The servlet verifies that required fields are filled out correctly before storing the user data in a BeanUser object and session. The JSP page then displays the user information using JSP tags to access the session attribute or EL expressions to loop through languages.

Uploaded by

Nasser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1- HTML pour remplir les informations d'un utilisateur:

html

<form action="submitForm" method="post">

<label>Nom:</label>

<input type="text" name="lastName" required>

<br>

<label>Prénom:</label>

<input type="text" name="firstName" required>

<br>

<label>Date de naissance:</label>

<input type="date" name="birthdate" required>

<br>

<label>Lieu de naissance:</label>

<input type="text" name="birthplace" required>

<br>

<label>Pays de naissance:</label>

<select name="country" required>

<option value=""></option>

<option value="Niger">Niger</option>

<option value="Senegal">Senegal</option>

<option value="Mali">Mali</option>

</select>

<br>

<label>Sexe:</label>

<input type="radio" name="gender" value="male" required> Male

<input type="radio" name="gender" value="female" required> Female

<br>

<label>Email:</label>

<input type="email" name="email" required>

<br>

1
<label>Mot de passe:</label>

<input type="password" name="password" required>

<br>

<label>Confirmation du mot de passe:</label>

<input type="password" name="passwordConfirmation" required>

<br>

<label>Langages de programmation préférés:</label>

<input type="checkbox" name="programmingLanguages" value="Java"> Java

<input type="checkbox" name="programmingLanguages" value="Python"> Python

<input type="checkbox" name="programmingLanguages" value="C++"> C++

<br>

<input type="submit" value="Soumettre">

</form>

-2-servlet qui vérifie si les champs saisis sont corrects :

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

public class SubmitFormServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response) throws


IOException, ServletException {

// Récupération des données du formulaire

String lastName = request.getParameter("lastName");

String firstName = request.getParameter("firstName");

String birthdate = request.getParameter("birthdate");

String birthplace = request.getParameter("birthplace");

String country = request.getParameter("country");

String gender = request.getParameter("gender");

String email = request.getParameter("email");

String password = request.getParameter("password");

2
String passwordConfirmation = request.getParameter("passwordConfirmation");

String[] programmingLanguages = request.getParameterValues("programmingLanguages");

// Vérification des données saisies

if (lastName != null && firstName != null && lastName.length() > 0 && firstName.length() > 0

&& birthdate != null && birthdate.length() > 0 && birthplace != null

&& birthplace.length() > 0 && country != null && country.length() > 0

&& gender != null && gender.length() > 0 && email != null

&& email.length() > 0 && password != null && password.length() > 0

&& passwordConfirmation != null && passwordConfirmation.length() > 0

&& password.equals(passwordConfirmation) && programmingLanguages != null

&& programmingLanguages.length > 0) {

// Création d'un BeanUser

BeanUser user = new BeanUser(lastName, firstName, birthdate, birthplace, country, gender,


email, password, programmingLanguages);

// Stockage du BeanUser en session

request.getSession().setAttribute("user", user);

// Redirection vers la page JSP qui affiche les informations de l'utilisateur

request.getRequestDispatcher("userInfo.jsp").forward(request, response);

} else {

// Redirection vers la page d'accueil

response.sendRedirect("index.html");

3- code JSP qui affiche les informations de l'utilisateur de deux façons :

comment afficher les informations d'un utilisateur dans une page JSP en utilisant les balises JSP

a. La balise jsp :

<%@ page import="com.mypackage.BeanUser" %>

3
<% BeanUser user = (BeanUser) session.getAttribute("user"); %>

<p>Nom: <%= user.getLastName() %></p>

<p>Prénom: <%= user.getFirstName() %></p>

<p>Date de naissance: <%= user.getBirthdate() %></p>

<p>Lieu de naissance: <%= user.getBirthplace() %></p>

<p>Pays de naissance: <%

b- code pour afficher les informations d'un utilisateur dans une page JSP en utilisant les expressions
EL :

%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE html>

<html>

<head>

<title>Informations d'utilisateur</title>

</head>

<body>

<h1>Informations d'utilisateur</h1>

<p>Nom : <%= user.getName() %></p>

<p>Prénom : <%= user.getFirstName() %></p>

<p>Date de naissance : <%= user.getBirthDate() %></p>

<p>Lieu de naissance : <%= user.getBirthPlace() %></p>

<p>Pays de naissance : <%= user.getBirthCountry() %></p>

<p>Sexe : <%= user.getGender() %></p>

<p>Email : <%= user.getEmail() %></p>

<p>Langages de programmation préférés :</p>

<ul>

<c:forEach items="${user.getPreferredLanguages()}" var="language">

<li>${language}</li>

</c:forEach>

4
</ul>

</body>

</html>

You might also like