Vous êtes sur la page 1sur 3

TP JSP

RACHDI Youssef

TP JSP
Dans cet exercice, nous allons faire une authentification avec un login et un mot de passe en utilisant un Java Bean reprsent par la classe User. Cette authentification va tre faite avec deux mthodes. La premire utilise les scriptlets JSP et la deuxime utilise les actions JSP. 1. Classe User.java
package a.b; public class User { private String login; private String passwd; public User(){ } public void setLogin(String login) { this.login = login; } public String getLogin() { return login; } public void setPasswd(String passwd) { this.passwd = passwd; } public String getPasswd() { return passwd; } }

2. Premire mthode : Les scriptlets JSP : a. Formulaire dauthentification : login.htm


<html> <head> <title>Autentification</title> </head> <body bgcolor="#FFFFFF"> <form method="post" action="authent1.jsp"> Login:<input type="text" name="login"/><br/> Mot de passe:<input type="password" name="passwd"/><br/> <input type="submit" name="Envoyer" value="Envoyer"/> </form> </body> </html>

b. Page JSP : authent1.jsp :


<%@ page language="java" %> <%@ page import="a.b.*" %> <% String l=request.getParameter("login"); String p=request.getParameter("passwd");

1/3

TP JSP
User u=new User(); u.setLogin(l); u.setPasswd(p); session.setAttribute("user",u); response.sendRedirect("aff1.jsp"); %>

RACHDI Youssef

c. Page JSP : aff1.jsp :


<%@ page language="java" %> <%@ page import="a.b.*" %> <html> <head> <title>Affichage</title> </head> <body bgcolor="#FFFFFF"> <% User u=(User)session.getAttribute("user"); %> <h1 align='center'> Votre Login est:<%=u.getLogin()%><br/> Votre mot de passe:<%=u.getPasswd()%><br/> </h1> </body> </html>

3. Deuxime mthode : Les action JSP : a. Formulaire dauthentification : login2.htm (le mme que le prcdent)
<html> <head> <title>Authentification</title> </head> <body bgcolor="#FFFFFF"> <form method="post" action="authent2.jsp"> Login:<input type="text" name="login"/><br/> Mot de passe:<input type="password" name="passwd"/><br/> <input type="submit" name="Envoyer" value="Envoyer"/> </form> </body> </html>

b. Page JSP : authent2.jsp :


<%@ page language="java" %> <%@ page import="a.b.*" %> <jsp:useBean id="u" class="a.b.User" scope="session"/> <jsp:setProperty name="u" property="login" param="login"/> <jsp:setProperty name="u" property="passwd" param="passwd"/> <jsp:forward page="aff2.jsp" />

2/3

TP JSP c. Page JSP : aff2.jsp :


<%@ page language="java" %> <%@ page import="a.b.*" %> <html> <head> <title>Affichage</title> </head> <body bgcolor="#FFFFFF">

RACHDI Youssef

<h1 align='center'> Votre Login est:<jsp:getProperty name="u" property="login"/><br/> Votre mot de passe:<jsp:getProperty name="u" property="passwd"/><br/> </h1> </body> </html>

4. Rsultat:
On devrait obtenir le mme rsultat pour les deux solutions :

3/3

Vous aimerez peut-être aussi