Enterprise Application Project项目开发

来源:互联网 发布:入门电钢琴推荐 知乎 编辑:程序博客网 时间:2024/06/12 01:44

EJB项目中的文件:

package com.ww.test.dao;

import java.util.List;

import com.ww.test.ejb.StudentInfo;

public interface StudentDAO {

    public List<StudentInfo> findStudents();

    public StudentInfo findStudent(int id);

    public int saveStudent(StudentInfo si);

    public int updateStudent(StudentInfo si);

    public int deleteStudent(int id);

}

 

 

package com.ww.test.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.ww.test.ejb.StudentInfo;

public class StudentDAOImpl implements StudentDAO {
    private Connection conn=null;
    private Statement sta=null;
    private ResultSet rs=null;
    protected static DataSource getDataSource() {
        InitialContext ic;
        DataSource ds = null;
        try {
            ic = new InitialContext();
            ds = (DataSource) ic.lookup("mypool");
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ds;
    }

    public int deleteStudent(int id) {
        int deleteId=0;
        try {
            conn=StudentDAOImpl.getDataSource().getConnection();
            sta=conn.createStatement();
            String sql="delete student_table where id="+id;
            deleteId=sta.executeUpdate(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return deleteId;
    }

    public StudentInfo findStudent(int id) {
        StudentInfo studentInfo=new StudentInfo();
        try {
            conn=StudentDAOImpl.getDataSource().getConnection();
            sta=conn.createStatement();
            String sql="select * from student_table where id="+id;
            rs=sta.executeQuery(sql);
            while(rs.next()){
                studentInfo.setId(rs.getInt("id"));
                studentInfo.setName(rs.getString("name"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return studentInfo;
    }

    public List<StudentInfo> findStudents() {
        List<StudentInfo> list=new ArrayList<StudentInfo>();
        try {
            conn=StudentDAOImpl.getDataSource().getConnection();
            sta=conn.createStatement();
            String sql="select * from student_table";
            rs=sta.executeQuery(sql);
            while(rs.next()){
                StudentInfo studentInfo=new StudentInfo();
                studentInfo.setId(rs.getInt("id"));
                studentInfo.setName(rs.getString("name"));
                list.add(studentInfo);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }

    public int saveStudent(StudentInfo si) {
        int saveId=0;
        try {
            conn=StudentDAOImpl.getDataSource().getConnection();
            sta=conn.createStatement();
            String sql="insert into student_table values("+si.getId()+",'"+si.getName()+"')";
            saveId=sta.executeUpdate(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return saveId;
    }

    public int updateStudent(StudentInfo si) {
        int updateId=0;
        try {
            conn=StudentDAOImpl.getDataSource().getConnection();
            sta=conn.createStatement();
            String sql="update student_table set name='"+si.getName()+"' where id="+si.getId();
            updateId=sta.executeUpdate(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return updateId;
    }

}

 

 

package com.ww.test.dao;

public class StudentDAOFactory {
    public static StudentDAO getDAO() {
        String className = "com.ww.test.dao.StudentDAOImpl";
        StudentDAO dao = null;
        try {
            dao = (StudentDAO) Class.forName(className).newInstance();
        } catch (Exception se) {
            se.printStackTrace();
        }
        return dao;
    }
}

 

 

package com.ww.test.ejb;

import java.rmi.RemoteException;

import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;

public class StudentInfo implements EntityBean{

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public void ejbActivate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
       
    }

    public void ejbLoad() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
       
    }

    public void ejbPassivate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
       
    }

    public void ejbRemove() throws RemoveException, EJBException,
            RemoteException {
        // TODO Auto-generated method stub
       
    }

    public void ejbStore() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
       
    }

    public void setEntityContext(EntityContext arg0) throws EJBException,
            RemoteException {
        // TODO Auto-generated method stub
       
    }

    public void unsetEntityContext() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
       
    }

}

 

 

package com.ww.test.manager;

import java.rmi.RemoteException;
import java.util.List;

import javax.ejb.EJBObject;

import com.ww.test.ejb.StudentInfo;

public interface StudentSL extends EJBObject {
//必须抛出远程异常
    public List<StudentInfo> findStudents()throws RemoteException;

    public StudentInfo findStudent(int id)throws RemoteException;

    public int saveStudent(StudentInfo si)throws RemoteException;

    public int updateStudent(StudentInfo si)throws RemoteException;

    public int deleteStudent(int id)throws RemoteException;

}

 

 

package com.ww.test.manager;

import java.rmi.RemoteException;
import java.util.List;

import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

import com.ww.test.dao.StudentDAO;
import com.ww.test.dao.StudentDAOFactory;
import com.ww.test.ejb.StudentInfo;

public class StudentSLBean implements SessionBean {
    private StudentDAO sd;

    /**
     *
     */
    private static final long serialVersionUID = 1L;
   
    public void ejbCreate(){
        sd = StudentDAOFactory.getDAO();
    }

    public void ejbActivate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub

    }

    public void ejbPassivate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub

    }

    public void ejbRemove() throws EJBException, RemoteException {
        // TODO Auto-generated method stub

    }

    public void setSessionContext(SessionContext arg0) throws EJBException,
            RemoteException {
        // TODO Auto-generated method stub

    }

    public List<StudentInfo> findStudents() {
        return sd.findStudents();

    }

    public StudentInfo findStudent(int id) {
        return sd.findStudent(id);

    }

    public int saveStudent(StudentInfo si) {
        return sd.saveStudent(si);

    }

    public int updateStudent(StudentInfo si) {
        return sd.updateStudent(si);

    }

    public int deleteStudent(int id) {
        return sd.deleteStudent(id);

    }
}

 

 

package com.ww.test.manager;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface StudentSLHome extends EJBHome {
    public StudentSL create() throws CreateException, RemoteException;
}

 

 

package com.ww.test.manager;

import java.util.List;

import com.ww.test.ejb.StudentInfo;

public interface StudentSLLocal extends javax.ejb.EJBLocalObject {

    public List<StudentInfo> findStudents();

    public StudentInfo findStudent(int id);

    public int saveStudent(StudentInfo si);

    public int updateStudent(StudentInfo si);

    public int deleteStudent(int id);
}

 

 

package com.ww.test.manager;

import javax.ejb.CreateException;


public interface StudentSLLocalHome extends javax.ejb.EJBLocalHome {
    public StudentSLLocal create() throws CreateException;
}

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
    <enterprise-beans>
        <session>
            <display-name>my ejb test</display-name>
            <ejb-name>MyFirstEJB</ejb-name>

            <home>com.ww.test.manager.StudentSLHome</home>
            <remote>com.ww.test.manager.StudentSL</remote>
           
            <local-home>com.ww.test.manager.StudentSLLocalHome</local-home>
            <local>com.ww.test.manager.StudentSLLocal</local>
           
            <ejb-class>com.ww.test.manager.StudentSLBean</ejb-class>
            <session-type>stateless</session-type>
            <transaction-type>Container</transaction-type>    
        </session>
    </enterprise-beans>
    <assembly-descriptor>
        <container-transaction>
            <method>
                <ejb-name>MyFirstEJB</ejb-name>
                <method-name>*</method-name>
            </method>
            <trans-attribute>Required</trans-attribute>
        </container-transaction>
       
    </assembly-descriptor>
</ejb-jar>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN' 'http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd'>
<weblogic-ejb-jar>
    <weblogic-enterprise-bean>
        <ejb-name>MyFirstEJB</ejb-name>
        <jndi-name>remote/studentjndi</jndi-name>
        <local-jndi-name>local/studentjndi</local-jndi-name>
    </weblogic-enterprise-bean>
</weblogic-ejb-jar>



WEB项目中的文件:

 

package com.ww.test.ejb;

import java.rmi.RemoteException;

import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;

public class StudentInfo implements EntityBean{

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public void ejbActivate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
       
    }

    public void ejbLoad() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
       
    }

    public void ejbPassivate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
       
    }

    public void ejbRemove() throws RemoveException, EJBException,
            RemoteException {
        // TODO Auto-generated method stub
       
    }

    public void ejbStore() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
       
    }

    public void setEntityContext(EntityContext arg0) throws EJBException,
            RemoteException {
        // TODO Auto-generated method stub
       
    }

    public void unsetEntityContext() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
       
    }

}

 

package com.ww.test.manager;

import java.rmi.RemoteException;
import java.util.List;

import javax.ejb.EJBObject;

import com.ww.test.ejb.StudentInfo;

public interface StudentSL extends EJBObject {
//必须抛出远程异常
    public List<StudentInfo> findStudents()throws RemoteException;

    public StudentInfo findStudent(int id)throws RemoteException;

    public int saveStudent(StudentInfo si)throws RemoteException;

    public int updateStudent(StudentInfo si)throws RemoteException;

    public int deleteStudent(int id)throws RemoteException;

}

 

package com.ww.test.manager;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface StudentSLHome extends EJBHome {
    public StudentSL create() throws CreateException, RemoteException;
}

 

 

package com.ww.test.servlet;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.List;
import java.util.Properties;

import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ww.test.ejb.StudentInfo;
import com.ww.test.manager.StudentSL;
import com.ww.test.manager.StudentSLHome;

public class StudentServlet extends HttpServlet {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private StudentSL studentSL = null;
    private StudentSLHome studentSLHome = null;
    private List<StudentInfo> list = null;
    InitialContext ctx = null;
   
    @Override
    public void init() throws ServletException {
        Properties prop = new Properties();
        prop.put(Context.INITIAL_CONTEXT_FACTORY,
                "weblogic.jndi.WLInitialContextFactory");
        prop.put(Context.PROVIDER_URL, "t3://localhost:7001");
        try {
            ctx = new InitialContext(prop);
            //远程调用
            Object obj = ctx.lookup("remote/studentjndi");
            studentSLHome = (StudentSLHome)PortableRemoteObject.narrow(obj,StudentSLHome.class);
            studentSL = studentSLHome.create();
       
            //本地调用
            obj = ctx.lookup("local/studentjndi");
            studentSLLocalHome = (StudentSLLocalHome)obj;
            studentSLLocal = studentSLLocalHome.create();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (CreateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        super.init();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");

        list = studentSL.findStudents();
        System.out.println(null==list?"没有获取到list值":"list长度为:"+list.size());
        request.setAttribute("list", list);
        request.getRequestDispatcher("result.jsp").forward(request, response);

    }
   
   
    public static void main(String[] args) {
        StudentSL studentSL = null;
        List<StudentInfo> list = null;
        InitialContext ctx = null;
        Properties prop = new Properties();
        prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
        prop.put(Context.PROVIDER_URL, "t3://localhost:7001");
        try {
            ctx = new InitialContext(prop);
            Object obj = ctx.lookup("studentjndi");
            StudentSLHome home = (StudentSLHome)PortableRemoteObject.narrow(obj,StudentSLHome.class);
            studentSL = home.create();
            list = studentSL.findStudents();
            System.out.println(null==list?"没有获取到list值":"list长度为:"+list.size());
            if(null!=list&&list.size()>0){
                for(StudentInfo st:list){
                    System.out.println("-----"+st.getId()+"---"+st.getName()+"-----");
                }
            }
        } catch (NamingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (CreateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>StudentServlet</servlet-name>
    <servlet-class>com.ww.test.servlet.StudentServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>StudentServlet</servlet-name>
    <url-pattern>/StudentServlet.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
 
  <body>
    <table width="100%" border="1">
        <tr>
        <td width="15%">
            <a href="StudentServlet.do" target="myframe">查询</a>
        </td>
        <td>
        <iframe name="myframe" width="800px" height="500px"></iframe>
        </td>
        </tr>
    </table>
  </body>
</html>

 

 

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="com.ww.test.ejb.StudentInfo" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'result.jsp' starting page</title>
   
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
 
  <body>
    <table width="500px" border="1">
    <tr><td>id</td><td>name</td></tr>
    <%
    List<StudentInfo>  list =(List<StudentInfo>)request.getAttribute("list");
    if(null!=list&&list.size()>0){
    for(int i=0;i<list.size();i++){
        %>
        <tr><td><%=list.get(i).getId() %></td><td><%=list.get(i).getName() %></td></tr>
        <%
    }
    }
     %>
    
    </table>
  </body>
</html>

注意:

1.本地调用jndi前提是客户端和服务器必须在同一个java虚拟机中,测试类和main方法不能进行本地调用的测试,必须发布到虚拟机中测试例如servlet

2.EJB中的po类必须进行序列化

 

3.给客户端添加wlclient.jar、wlfullclient.jar(将weblogic中的jar打包成wlfullclient.jar)

打包脚本为:java -jar wljarbuilder.jar 脚本文件名为:xxx.cmd

原创粉丝点击