如何在JEE中调用servlet中的实体类。初学者JEE程序员

时间:2017-06-08 04:20:19

标签: jsp servlets java-ee session-bean entity-bean

我试图在我的代码上使用实体类,但我得到“异常 java.lang.NoClassDefFoundError:“exception。

我的学生实体课程:

package entity;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


@Entity
public class student implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private int pass;

public student (){       

}
public student(Long id, String name, int pass) {
    this.id = id;
    this.name = name;
    this.pass = pass;
}
public student(String name, int pass) {        
    this.name = name;
    this.pass = pass;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    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 student)) {
        return false;
    }
    student other = (student) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "entity.student[ id=" + id + " ]";
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getPass() {
    return pass;
}

public void setPass(int pass) {
    this.pass = pass;
}

}

我的学生Facade:实体类的会话bean

package beans;

import entity.student;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


 @Stateless
 public class studentFacade extends AbstractFacade<student> {

@PersistenceContext(unitName = "Testing-ejbPU")
private EntityManager em;

@Override
protected EntityManager getEntityManager() {
    return em;
}

public studentFacade() {
    super(student.class);
}

public student findStudent(String x){
    Query query = em.createNamedQuery("Student.findByName");
    query.setParameter("name", x);
    student user = null;
    List<student> result = query.getResultList();
    for(Iterator it=result.listIterator();it.hasNext();){  
        user = (student)it.next();
    }  
    return user;        
}

public student getLoginDetails(String x, int y){
    student s = findStudent(x);
    if(s!=null){
        if(s.getPass()==y){
            return s;
        } 
    }
    return null;
 }

}

我的servlet:当我在我的servlet上调用学生班时,我得到一个例外。

   package brain;

import beans.studentFacade;
import entity.student;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Register extends HttpServlet {

@EJB
private studentFacade studentFacade;

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
   String name=request.getParameter("userName");          
    int password=Integer.parseInt(request.getParameter("password"));


    student s = new student(name,password);
    studentFacade.create(s);

    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.print("Registration done. Login to proceed!<br><br>");  
        request.getRequestDispatcher("login.jsp").include(request, response);
    }
}

Glassfish服务器输出:      警告:StandardWrapperValve [Register]:Servlet的Servlet.service()寄存器抛出异常      java.lang.NoClassDefFoundError:entity / student      在brain.Register.processRequest(Register.java:43)      在brain.Register.doPost(Register.java:79)      在javax.servlet.http.HttpServlet.service(HttpServlet.java:707)      在javax.servlet.http.HttpServlet.service(HttpServlet.java:790)

0 个答案:

没有答案