怎样写一个javaee企业应用实验,体系结构中包含web层,ejb层与jpa

2023-05-17 综合 34阅读

转载1.         1.本文主要技术采用了Servlet + JSP + EJB 3.0 + JPA,开发环境用的是EclipseHelios + JDK 1.6 + JBOSS 5.1 + MySQL 5.1数据库。

2.         2. 示例场景:用户通过Web页面对Employee进行创建、查找和删除。这个小例子重点是演示JPA的使用。JPA是JavaPersistence API的缩写,主要是将液正关系晌埋尘数据库的操作对象化,它的j2ee的标准规范,由于其自身简单易用的优点,也是SAP推荐使用的数据库技术。

3.      3.准备工作:

(1)   安装JBOSS 5.1

(2)   安装MySQL 5.1

(3)   给Jboss配置数据源

首先启动mysql后,创建一个数据库叫ejbtest,然后创建一个用户jboss,密码是password,并将ejbtest的权限赋给它。

然后在路径jboss-5.1.0.GA/server/default/deploy下放入mysql-ds.xml文件。

Mysql-ds.xml:
<datasources> 
    <local-tx-datasource> 
       <jndi-name>MySqlDS</jndi-name> 
       <connection-url>jdbc:mysql://localhost:3306/ejbtest</connection-url>
       <driver-class>com.mysql.jdbc.Driver</driver-class> 
       <user-name>jboss</user-name> 
       <password>password</password> 
    </local-tx-datasource> 
</datasources>

4.       4.开发

    4.1先创建一个EJB Project,名字叫EmployeeEJB。

然后在META-INF目录下新建一个persistence.xml文件

   4.2开始创建我们的Entity Bean:Employee类,它是一个简单的Java Bean。

package com.sap.demo.ejb.employee.model;

 

import javax.persistence.Entity;

import javax.persistence.Id;

 

@Entity

public class Employee {

      @Id

      privateint id;

     

      publicint getId() {

            returnid;

      }

 

      publicvoid setId(int id) {

            this.id= id;

      }

 

      publicString getName() {

            returnname;

      }

 

      publicvoid setName(String name) {

            this.name= name;

      }

 

      privateString name;

}

      4.3  在ejb项目中创建一个Local的SessionBean,名字叫EmployeeServicel,它的主要功能是对employee进行数据的操作,宴禅包括创建、查找、删除。

接口声明:

package com.sap.demo.ejb.employee.ejb;

 

import javax.ejb.Local;

 

importcom.sap.demo.ejb.employee.model.Employee;

 

@Local

public interface EmployeeServiceLocal {

      publicEmployee createEmployee(int id, String name);

 

      publicEmployee findEmployee(int id);

 

      publicvoid deleteEmployee(int id);

}

 

这里是接口的实现:

package com.sap.demo.ejb.employee.ejb;

 

import javax.ejb.Stateless;

import javax.ejb.TransactionAttribute;

import javax.ejb.TransactionAttributeType;

import javax.persistence.EntityManager;

import javax.persistence.PersistenceContext;

 

importcom.sap.demo.ejb.employee.model.Employee;

 

@Stateless

public class EmployeeService implementsEmployeeServiceLocal {

      @PersistenceContext(unitName= "EmployeeDS")

      privateEntityManager em;

 

      @Override

      publicEmployee createEmployee(int id, String name) {

            Employeeemp = new Employee();

            emp.setId(id);

            emp.setName(name);

            em.persist(emp);

            returnemp;

      }

 

      @Override

      @TransactionAttribute(TransactionAttributeType.SUPPORTS)

      publicEmployee findEmployee(int id) {

            returnem.find(Employee.class, new Integer(id));

      }

 

      @Override

      publicvoid deleteEmployee(int id) {

            Employeeemp = findEmployee(id);

            if(emp != null) {

                  em.remove(emp);

            }

 

      }

}

      这样EJB项目就创建完毕了。

4    4.4 然后创建Web Project,名字叫EmployeeWeb。这个项目很简单,包含一个Servlet和两个页面index.jsp和result.jsp。用户在index.jsp对employee进行创建、查找、删除,然后由result.jsp负责展示结果。

Index.jsp

<%@ pagelanguage="java" contentType="text/html;charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<!DOCTYPE htmlPUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type"content="text/html; charset=ISO-8859-1">

<title>Insert titlehere</title>

</head>

<body>

<FORM ACTION="ShowResult">

 Id:

 <INPUTTYPE="text" NAME="id"/><P>

 Name: 

 <INPUTTYPE="TEXT" NAME="name"/>

 <INPUTTYPE="hidden" NAME="method"VALUE="create" />

 <INPUTTYPE="SUBMIT" VALUE="Createemployee and show result.">

</FORM>

 

<FORM ACTION="ShowResult">

 Id:

 <INPUTTYPE="text" NAME="id"/>

 <inputTYPE="hidden" NAME="method"VALUE="find" />

 <INPUTTYPE="SUBMIT" VALUE="Find employeeand show result.">

</FORM>

 

<FORM ACTION="ShowResult">

 Id:

 <INPUTTYPE="text" NAME="id"/>

 <inputTYPE="hidden" NAME="method"VALUE="delete" />

 <INPUTTYPE="SUBMIT" VALUE="Deleteemployee and show result.">

</FORM>

</body>

</html>

result.jsp

<%@ pagelanguage="java" contentType="text/html;charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<!DOCTYPE htmlPUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type"content="text/html; charset=ISO-8859-1">

<title>Insert titlehere</title>

</head>

<body>

<jsp:useBean id="employee"type="com.sap.demo.ejb.employee.model.Employee"scope="request"/>

id:<jsp:getProperty name="employee"property="id"/>,

name:<jsp:getProperty name="employee"property="name"/>

</body>

</html>

最后是Servlet ShowResult:

package com.sap.demo.ejb.servlets;

 

import java.io.IOException;

 

import javax.ejb.EJB;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

 

import com.sap.demo.ejb.employee.ejb.EmployeeServiceLocal;

importcom.sap.demo.ejb.employee.model.Employee;

 

 

public class ShowResult extends HttpServlet {

      privatestatic final long serialVersionUID = 1L;

   

      @EJB

      privateEmployeeServiceLocal service;

    

   public ShowResult() {

       super();

       // TODO Auto-generated constructor stub

    }

 

      

      protectedvoid doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

 

            Stringmethod = request.getParameter("method");

            if(method.equals("create")) {

                  Stringname = request.getParameter("name");

                  Stringids = request.getParameter("id");

                  intid = Integer.parseInt(ids);

                  Employeeem = service.createEmployee(id, name);

                  request.setAttribute("employee",em);

            }elseif (method.equals("find")) {

                  Stringids = request.getParameter("id");

                  intid = Integer.parseInt(ids);

                  Employeeem = service.findEmployee(id);

                  request.setAttribute("employee",em);

            }else if (method.equals("delete")){

                  Stringids = request.getParameter("id");

                  intid = Integer.parseInt(ids);

                  Employeeem = service.findEmployee(id);

                  if(em != null){

                        service.deleteEmployee(id);

                        request.setAttribute("employee",em);

                  }

            }

            RequestDispatcherdispatcher = request.getRequestDispatcher("result.jsp");

            dispatcher.forward(request,response);

      }

 

      

      protectedvoid doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

            //TODO Auto-generated method stub

      }

 

}

声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com