Skip to main content

NetBeans, Glassfish, J2EE Apllication.... I have a problem

1 reply [Last post]
maurotj84
Offline
Joined: 2010-06-05

Hi, I want to explain my problems.. I create a little project for test something to use in a big project. I have problem with persistence. It is impossible for me to make connection with DB.. I have a simply entity, a session bean, a jsp for call a servlet. When i try to make a persist(object) option they ever return a nullPointerException.. what is wrong on my project? thank you

@Entity
@Table(name="UTENTI")
public class Utente implements Serializable {
@Id
@Column(name="IDUTENTE")
private String id;
@Column(name="NOME")
private String nome;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}

A session bean

@Stateless(name="NewSessionBean")
public class NewSessionBean implements NewSessionBeanRemote {
@PersistenceContext(name="EA1-ejbPU")
private EntityManager em;

public void Creazione(Utente u) throws IOException{
try{
em.persist(u);
}catch (Exception ex) {
ex.printStackTrace();
}finally {
if(em != null) {
em.close();
} } }

SERVLET

public class CreaUtenteServ extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
Utente u=new Utente();
String id=request.getParameter("id");
String nome=request.getParameter("nome");
u.setId(id);
u.setNome(nome);

gestore.NewSessionBean cr=new gestore.NewSessionBean();
cr.Creazione(u);

}catch(Exception ex) {
throw new ServletException(ex);
} }

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Bobby Bissett

> [b]SERVLET[/b]
>
> public class CreaUtenteServ extends HttpServlet {
> protected void processRequest(HttpServletRequest request,
> HttpServletResponse response) throws ServletException, IOException {
> try{
> Utente u=new Utente();
> String id=request.getParameter("id");
> String nome=request.getParameter("nome");
> u.setId(id);
> u.setNome(nome);
>
> gestore.NewSessionBean cr=new gestore.NewSessionBean();
> cr.Creazione(u);
>
> }catch(Exception ex) {
> throw new ServletException(ex);
> } }

You can't use the constructor of the session bean in this case. That
will just give you an instance of the class but it won't be managed by
the container. Instead, try having the session bean injected into your
servlet. Something like:

@EJB
private NewSessionBean bean;

Then in the processRequest() method you can use the bean.

Cheers,
Bobby

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net