《JAVA EE编程技术实验报告.doc》由会员分享,可在线阅读,更多相关《JAVA EE编程技术实验报告.doc(16页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、J2EE编程技术实验报告书专业: 指导老师: 学号:姓名: 实验一 JSP网页设计一、实验目的掌握J2EE JSP+Servlet+JavaBean开发模式开发JSP网页步骤;掌握该开发模式实现用户登录页面功能。二、实验环境PC系列微机,CPU2G以上,内存1G以上,Windows XP,MyEclipse6.5以上。三、实验步骤1、创建Servlet LoginServlet实现登录验证编写该Servlet实现登录验证并进行配置。2、创建login.jsp、admin.jsp、user.jsp这3个jsp的页面采用的JSP+JavaBean开发模式中相同。3、调试程序是否正常运行四、实验报告
2、LoginServlet.java:package com.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.dao.UserDao;import com.dao.Use
3、rDaoImpl;import com.model.User;public class LonginServlet extends HttpServlet public LonginServlet() super();public void destroy() super.destroy(); public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException public void doPost(HttpServletRequest re
4、quest, HttpServletResponse response)throws ServletException, IOException String a=123;User user=new User();user.setUsername(request.getParameter(username);user.setUserpassword(request.getParameter(password);user.setRole(request.getParameter(role);UserDao userdao=new UserDaoImpl();if(userdao.login(us
5、er)if(user.getRole().equals(admin)request.getRequestDispatcher(Admin.jsp).forward(request,response);elserequest.getRequestDispatcher(User.jsp).forward(request, response);elserequest.setAttribute(errormessage, error);request.getRequestDispatcher(login.jsp).forward(request, response);public void init(
6、) throws ServletException Login.jsp: base href= My JSP login.jsp starting page 登录 账号: 密码: Admin.jsp: base href= My JSP Admin.jsp starting page 登录成功 用户信息: 用户类型:ADMIN用户账号: 用户密码: 五、实验小结通过这一次实验,我基本掌握了J2EE JSP+Servlet+JavaBean开发模式开发JSP网页步骤;掌握该开发模式实现用户登录页面功能。做完实验,感觉对于书本上的一些知识有了客观,清晰的认识,真正做到了把理论与实践相结合。实验二
7、JSP网站设计一、实验目的掌握JSP+DAO的网站开发步骤;理解JSP+DAO网站模式的优缺点。二、实验环境PC系列微机,CPU2G以上,内存1G以上,Windows XP,MyEclipse6.5三、实验步骤1、编写POJO User类 2、编写或使用原来的DBConnection类获得数据库连接3、创建数据访问抽象层接口UserDAO 4、创建数据访问实现类UserDAOImpl 5、创建登录需要的JSP页面页面有:login.jsp、login_do.jsp、admin.jsp、user.jsp。流程见作业JSP-DAO,修改有关的程序、数据库及名称。四、 实验报告程序源代码如下:Use
8、r.java:package com.model;public class User private String username;private String userpassword;private String role;public String getRole() return role;public void setRole(String role) this.role = role;public String getUsername() return username;public void setUsername(String username) this.username
9、= username;public String getUserpassword() return userpassword;public void setUserpassword(String userpassword) this.userpassword = userpassword;UserDAO.java:package com.dao;import com.model.User;public interface UserDao boolean login(User user);UserDaoImpl.java:package com.dao;import java.sql.Conne
10、ction;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import com.conn.DBConnection;import com.model.User;public class UserDaoImpl implements UserDao public boolean login(User user) Connection conn=DBConnection.getConnection();ResultSet rs=null;PreparedStateme
11、nt st=null;try st=conn.prepareStatement(select * from +user.getRole()+ where username=? and userpassword=?);st.setString(1, user.getUsername();st.setString(2, user.getUserpassword();rs=st.executeQuery();if(rs.next()return true; catch (SQLException e) e.printStackTrace();return false;DBConnection.jav
12、a:package com.conn;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class DBConnection public static Connection getConnection()Connection conn=null;try Class.forName(org.gjt.mm.mysql.Driver);try conn=DriverManager.getConnection(jdbc:mysql:/localhost:3306/t
13、est,root,sa); catch (SQLException e) e.printStackTrace(); catch (ClassNotFoundException e) e.printStackTrace();return conn;五、 实验小结 通过第二次实验,对于Servlet的工作原理和Servlet编程基本技能,我已经有了更深刻的认识,不再局限于书本上的理论知识,通过上网查找资料,我也扩充了自己的视野范围。而对于JSP工作原理JSP脚本元素、指令等,JDBC工作原理,我也初步掌握了,同时我也掌握了基于数据源访问数据库等技术,JSP+DAO的网站开发步骤;理解了JSP+DA
14、O网站模式的优缺点。实验三 Web Service编程实验一、实验目的掌握J2EE JSP+servlet+web service开发模式开发J2EE的web服务;掌握该开发模式实现用户访问web服务及web服务的业务逻辑功能。二、实验环境PC系列微机,CPU2G以上,内存1G以上,Windows XP,MyEclipse6.5以上。三、实验步骤1、创建Iwebservice.java实现J2EE的web 业务逻辑的接口。编写该Iwebservice.java实现web 业务逻辑的接口及参数描述。2、创建webserviceImpl.java实现J2EE的web 业务逻辑编写该webservi
15、ceImpl.java实现计算逻辑并将结果写入数据库,返回计算结果和数据库写入成功标记。3、调试程序是否正常运行四、实验报告网站程序源代码:Iwebservice.java:package com.testservice;public interface ITestService public String example(String message);public String Sort(String str);webserviceImpl.java:public class TestServiceImpl implements ITestService public String exa
16、mple(String message) return message;public String Sort(String str) String splits=str.split( );Integer Insplits=new Integersplits.length;for(int i=0;iInsplits.length;i+)Insplitsi=Integer.valueOf(splitsi);Integer temp=0;temp=Insplits0;for(int i=0;iInsplits.length;i+)for(int j=0;jInsplitsj+1)temp=Inspl
17、itsj;Insplitsj=Insplitsj+1;Insplitsj+1=temp;String result=new String();for(int i=0;iInsplits.length;i+)result+=Insplitsi.toString()+ ;Connection conn=DBConnection.getConnection();try PreparedStatement st=conn.prepareStatement(insert into webservice(sortedstr) values(?);st.setString(1, result);st.exe
18、cuteUpdate(); catch (SQLException e) e.printStackTrace();return fail;return success;Sort.java:public class Sort protected String in0;public String getIn0() return in0;public void setIn0(String value) this.in0 = value;SortSPonse.Java:public class SortResponse protected String out;public String getOut
19、() return out;public void setOut(String value) this.out = value;五、 实验小结 通过第三次实验,我了解了WEB服务的基本概念,对WEB服务技术的优缺点有了客观上的理解。通过实验课,我了解了WEB服务的基本步骤。同时也比较J2EE与.net的web服务开发的相同点与不同点,做到横向与纵向的比较。实验4.J2EE与EJB数据库编程一、实验目的掌握J2EE JSP+servlet+EJB开发模式开发J2EE的EJB组件;掌握该开发模式实现用户访问EJB及EJB组件功能。二、实验环境PC系列微机,CPU2G以上,内存1G以上,Window
20、s XP,MyEclipse6.5以上。三、实验步骤1、创建ejbsession.java实现会话 EJB的计算逻辑功能。编写该ejbsession.java实现EJB业务逻辑功能。2、创建ejbentity.java实现实体EJB的数据模型。编写该ejbentity.java实现用户账户的数据模型。3、创建ejbmessage.java实现用户提交信息触发事件,完成逻辑计算和实体EJB对象的修改。4、调试程序是否正常运行四、实验报告网页程序源代码:Ejbentity.java:public class Ejbentity implements StatefulCalculatorRemote
21、 private int a = 0;private int b = 0;public int add() return a + b;public void setA(int a) this.a = a;public void setB(int b) this.b = b;public void destory() System.out.println(EJB StatefullCalculator 即将销毁);public void created() System.out.println(EJB StatefullCalculator 已经创建完毕);public void passive
22、() System.out.println(EJB StatefullCalculator 准备钝化);public void active() System.out.println(EJB StatefullCalculator 已经激活);public void remove() System.out.println(EJB StatefullCalculator 请求容器销毁当前 Bean 实例);Ejbentity.java:public class StudentFacade implements StudentFacadeLocal public static final Stri
23、ng USERNAME = username;public static final String PASSWORD = password;public static final String AGE = age;PersistenceContextprivate EntityManager entityManager;public void save(Student entity) LogUtil.log(saving Student instance, Level.INFO, null);try entityManager.persist(entity);LogUtil.log(save
24、successful, Level.INFO, null); catch (RuntimeException re) LogUtil.log(save failed, Level.SEVERE, re);throw re;public void delete(Student entity) LogUtil.log(deleting Student instance, Level.INFO, null);try entity = entityManager.getReference(Student.class, entity.getId();entityManager.remove(entity
25、);LogUtil.log(delete successful, Level.INFO, null); catch (RuntimeException re) LogUtil.log(delete failed, Level.SEVERE, re);throw re;public Student update(Student entity) LogUtil.log(updating Student instance, Level.INFO, null);try Student result = entityManager.merge(entity);LogUtil.log(update suc
26、cessful, Level.INFO, null);return result; catch (RuntimeException re) LogUtil.log(update failed, Level.SEVERE, re);throw re;public Student findById(Integer id) LogUtil.log(finding Student instance with id: + id, Level.INFO,null);try Student instance = entityManager.find(Student.class, id);return ins
27、tance; catch (RuntimeException re) LogUtil.log(find failed, Level.SEVERE, re);throw re;SuppressWarnings(unchecked)public List findByProperty(String propertyName,final Object value, final int. rowStartIdxAndCount) LogUtil.log(finding Student instance with property: + propertyName+ , value: + value, L
28、evel.INFO, null);try final String queryString = select model from Student model where model.+ propertyName + = :propertyValue;Query query = entityManager.createQuery(queryString);query.setParameter(propertyValue, value);if (rowStartIdxAndCount != null & rowStartIdxAndCount.length 0) int rowStartIdx
29、= Math.max(0, rowStartIdxAndCount0);if (rowStartIdx 0) query.setFirstResult(rowStartIdx);if (rowStartIdxAndCount.length 1) int rowCount = Math.max(0, rowStartIdxAndCount1);if (rowCount 0) query.setMaxResults(rowCount);return query.getResultList(); catch (RuntimeException re) LogUtil.log(find by prop
30、erty name failed, Level.SEVERE, re);throw re;public List findByUsername(Object username,int. rowStartIdxAndCount) return findByProperty(USERNAME, username, rowStartIdxAndCount);public List findByPassword(Object password,int. rowStartIdxAndCount) return findByProperty(PASSWORD, password, rowStartIdxA
31、ndCount);public List findByAge(Object age, int. rowStartIdxAndCount) return findByProperty(AGE, age, rowStartIdxAndCount);SuppressWarnings(unchecked)public List findAll(final int. rowStartIdxAndCount) LogUtil.log(finding all Student instances, Level.INFO, null);try final String queryString = select
32、model from Student model;Query query = entityManager.createQuery(queryString);if (rowStartIdxAndCount != null & rowStartIdxAndCount.length 0) int rowStartIdx = Math.max(0, rowStartIdxAndCount0);if (rowStartIdx 0) query.setFirstResult(rowStartIdx);if (rowStartIdxAndCount.length 1) int rowCount = Math
33、.max(0, rowStartIdxAndCount1);if (rowCount 0) query.setMaxResults(rowCount);return query.getResultList(); catch (RuntimeException re) LogUtil.log(find all failed, Level.SEVERE, re);throw re;Ejbmessage.java:MessageDriven(mappedName = jms/MyMDB, activationConfig = ActivationConfigProperty(propertyName
34、 = acknowledgeMode, propertyValue = Auto-acknowledge),ActivationConfigProperty(propertyName = destinationType, propertyValue = javax.jms.Queue),ActivationConfigProperty(propertyName = destination , propertyValue=queue/testQueue),ActivationConfigProperty(propertyName = messageSelector , propertyValue
35、=BookName LIKE %MyEclipse6%)public class MyMDB implements MessageListener public void onMessage(Message message) if(message instanceof TextMessage)TextMessage msg =(TextMessage) message;String text;try text = msg.getText();System.out.println(收到文本消息: + text); catch (JMSException e) e.printStackTrace(); else System.out.println(收到其它消息: + message);五、 实验小结 通过第四次实验,我知道了EJB有消息EJB,实体EJB,回话EJB,对三种EJB的工作原理有了清晰的认识,同时也对无状态会话EJB、有状态实体EJB、实体EJB以及消息驱动EJB进行了比较,知道他们之间有什么不同于区别,也初步掌握了如何运用三种EJB。
限制150内