Ciao a tutti!
Sto facendo una applicazione web che comprende la registrazione di un utente...
Ho creato i seguenti Enterprise JavaBeans:
Session Bean: StreetSoccerBean.java
codice:
package my.streetsoccer;
import java.util.Vector;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.ejb.EJBException;
import javax.persistence.PersistenceContext;
@Stateless
public class StreetSoccerBean implements StreetSoccerRemote {
@PersistenceContext
private EntityManager em;
public boolean registraUtente(String nome, String cognome, String email,
String username, String password) {
try {
Vector v = (Vector)em.createQuery("SELECT username " +
"FROM utente u " +
"WHERE u.username = :username")
.setParameter("username", username);
if(v.size()>0) {
return false;
}
else {
v = (Vector)em.createNamedQuery("trovaIdUtenteMax");
String s = (String)v.elementAt(0);
int idutente = Integer.parseInt(s);
Utente utente = new Utente(idutente, nome, cognome, email, username, password);
em.persist(utente);
return true;
}
} catch (Exception e) {
throw new EJBException(e.getMessage());
}
}
}
Interfaccia Remota: StreetSoccerRemote.java
codice:
package my.streetsoccer;
import javax.ejb.Remote;
@Remote
public interface StreetSoccerRemote {
public boolean registraUtente(String nome, String cognome, String email,
String username, String password);
}
Inoltre ho fatto l'Entity Bean: Utente.java
codice:
package my.streetsoccer;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
@Entity
@NamedQuery(
name="trovaIdUtenteMax",
query="SELECT MAX idutente FROM utente"
)
public class Utente implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String nome;
private String cognome;
private String email;
private String username;
private String password;
public Utente() {
}
public Utente(int id, String nome, String cognome, String email, String username,
String password) {
this.id = id;
this.nome = nome;
this.cognome = cognome;
this.email = email;
this.username = username;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getCognome() {
return cognome;
}
public void setCognome(String cognome) {
this.cognome = cognome;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int hashCode() {
int hash = 0;
hash += (int) id;
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Utente)) {
return false;
}
Utente other = (Utente) object;
if (this.id != other.id) {
return false;
}
return true;
}
@Override
public String toString() {
return "my.streetsoccer.Utente[id=" + id + "]";
}
}
Ora vorrei creare una servlet che prenda i dati(nome, cognome, email, ecc...) da un form di una pagina JSP, e li usi per fare la registrazione dell'utente.
Il punto è che non so come fare la servlet. Come faccio per chiamare il metodo registraUtente nella servlet?
Non so proprio da dove iniziare a fare questa servlet. Forse devo fare il metodo doPost, nel quale mi istanzio un oggetto di tipo StreetSoccerRemote, in modo da poter chiamare il metodo registraUtente?
Oppure esite una sintassi appropiata per chiamare gli EJB all'interno di una servlet?
Ogni aiuto è gradito! Soprattutto se mi fate la servlet in questione!
Grazie e ciao!