桂林电子科技大学
一、实验目的
掌握JSP+DAO的网站开发步骤;
理解JSP+DAO网站模式的优缺点。
二、实验环境
PC系列微机,CPU2G以上,内存1G以上,Windows XP,MyEclipse6.5
三、实验报告
程序源代码如下:
<%--
Document : login
Created on : 2002-1-1, 0:41:42
Author : Administrator
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
java.sql.Connection conn=null;
java.lang.String strConn;
java.sql.PreparedStatement pStmt=null;
java.sql.ResultSet sqlRst=null;
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=use";
String user = "sa";
String pwd = "123";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}
try{
conn=java.sql.DriverManager.getConnection(url,user,pwd);
pStmt=conn.prepareStatement("select password from member where name=?");
String username=request.getParameter("username");
pStmt.setString(1, username);
sqlRst=pStmt.executeQuery();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="textml; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<p><h2>欢迎进入系统</h2></p>
<form>
用户:<input type="text"name="username"/><br/>
密码:<input type="password"name="password"/><br/>
</p>
<input type="reset" name="reset" value="重置"/>
<input type="submit" name="submit" value="提交"/>
<%
if(sqlRst.next()){
%>
<h2>恭喜你,登录成功!</h2>
<%
}
%>
</form>
</body>
<ml>
<%
}catch(java.sql.SQLException e){
out.println(e.toString());
}finally{
if(conn!=null) conn.close();
}
%>
运行结果:
四、实验小结
通过第二次实验,对于Servlet的工作原理和Servlet编程基本技能,我已经有了更深刻的认识,不再局限于书本上的理论知识,通过上网查找资料,我也扩充了自己的视野范围。同时我也掌握了基于数据源访问数据库等技术
桂林电子科技大学
一、实验目的
掌握JAVA EE JSP+servlet+web service开发模式开发JAVA EE的web服务;
掌握该开发模式实现用户访问web服务及web服务的业务逻辑功能。
二、实验环境
PC系列微机,CPU2G以上,内存1G以上,Windows XP,MyEclipse6.5以上。
三、实验报告
源代码如下:
Weightcheck.java
package com.hyl;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
/**
* @author Administrator
@WebService(serviceName = "WeightCheck")
public class WeightCheck {
@WebMethod(operationName = "check")
public String check(@WebParam(name = "sex") boolean sex, @WebParam(name = "weight") char weight, @WebParam(name = "height") int height) {
int r=height-weight;
if(sex)r=105-r;
else r=115-r;
if(r<-10)return "太瘦";
if(r<0&&r>-10) return "偏瘦";
if(r<10&&r>0) return "超重";
if(r<20&&r>10) return "肥胖";
else return "严重肥胖";
}
}
Invoke.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
boolean sex = false;
int weight = 48;
int height = 165;
String xb = request.getParameter("sex");
if(xb.equals("true"))sex=true;
weight = Integer.parseInt(request.getParameter("weight"));
height = Integer.parseInt(request.getParameter("height"));
%>
<%--star web service invocation--%><hr/>
<%
try {
com.hyl.wsclient.WeightCheck_Service service = new com.hyl.wsclient.WeightCheck_Service();
com.hyl.wsclient.WeightCheck port = service.getWeightCheckPort();
java.lang.String result = port.check(sex, weight, height);
out.println("你的体重为"+weight+",你的身高为"+height+"<br>");
out.println("测试结果为:"+result);
} catch (Exception ex) {
}
%>
<%-- end web service invocation --%><hr/>
</body>
</html>
Test.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>体重超标检测</h1>
<form name="info"action="invoke.jsp"method="POST">
性别: <select name="sex">
<option value="true">男</option>
<option value="false">女</option>
<lect>
体重: <input type="text"name="weight"value="50"/>
身高: <input type="text"name="height"value="160"/>
<input type="submit"value="测试"name="up"/>
</form>
</body>
</html>
运行结果:
四、实验小结
通过第三次实验,我了解了WEB服务的基本概念,对WEB服务技术的优缺点有了客观上的理解。通过实验课,我了解了WEB服务的基本步骤。
桂林电子科技大学
一、实验目的
掌握J2EE JSP+servlet+EJB开发模式开发J2EE的EJB组件;
掌握该开发模式实现用户访问EJB及EJB组件功能。
二、实验环境
PC系列微机,CPU2G以上,内存1G以上,Windows XP,MyEclipse6.5以上。
三、实验步骤
1、创建ejbsession.java实现会话 EJB的计算逻辑功能。
编写该ejbsession.java实现EJB业务逻辑功能。
2、创建ejbentity.java实现实体EJB的数据模型。
编写该ejbentity.java实现用户账户的数据模型。
3、创建ejbmessage.java实现用户提交信息触发事件,完成逻辑计算和实体EJB对象的修改。
四、实验报告
程序源代码:
Ejbentity.java:
public class Ejbentity implements StatefulCalculatorRemote {
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() {
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 String USERNAME = "username";
public static final String PASSWORD = "password";
public static final String AGE = "age";
@PersistenceContext
private EntityManager entityManager;
public void save(Student entity) {
LogUtil.log("saving Student instance", Level.INFO, null);
try {
entityManager.persist(entity);
LogUtil.log("save 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);
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 successful", 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 instance;
} 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, Level.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 = Math.max(0, rowStartIdxAndCount[0]);
if (rowStartIdx > 0) {
query.setFirstResult(rowStartIdx);
}
if (rowStartIdxAndCount.length > 1) {
int rowCount = Math.max(0, rowStartIdxAndCount[1]);
if (rowCount > 0) {
query.setMaxResults(rowCount);
}
}
}
return query.getResultList();
} catch (RuntimeException re) {
LogUtil.log("find by property 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, rowStartIdxAndCount);
}
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 model from Student model";
Query query = entityManager.createQuery(queryString);
if (rowStartIdxAndCount != null && rowStartIdxAndCount.length > 0) {
int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
if (rowStartIdx > 0) {
query.setFirstResult(rowStartIdx);
}
if (rowStartIdxAndCount.length > 1) {
int rowCount = Math.max(0, rowStartIdxAndCount[1]);
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 = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination" , propertyValue="queue/testQueue"),
@ActivationConfigProperty(propertyName = "messageSelector" , propertyValue="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。