华北电力大学
实 验 报 告
|
|
实验名称 基于J2EE架构的小型应用系统的设计与实现
课程名称 J2EE开发平台及程序设计
|
|
专业班级: 学生姓名:
学 号: 成 绩:
指导教师:王蓝婧 实验日期: 20##年11月
一、 实验目的及要求
学生在了解Java语言的基础上了解并掌握J2EE的构架及相关技术,重点掌握核心技术中最基本的Servlet、JSP、Javabean、JDBC,初步具有基于J2EE平台的软件开发能力。
1.设计并实现简单的聊天室:1)要求使用JSP或者servlet,使用session控制2)至少3个页面3)使用application对象或者servletcontext保存聊天信息4)能进行用户登录验证5)能显示在线用户名及数量6)供多个用户同时聊天
2.学有余力同学,可设计并实现小型应用系统,如网上书店等
1)设计数据库表: 1)user 类 2)图书信息表book
2)要求使用JSP、Javabean和JDBC实现所设计系统
a)数据库表的设计并录入数据
b)提供用户登录及验证
c)图书目录的列出、查询
d)将所选中图书加入购物车、并显示购物车
e)要求用户信息及图书信息必须保存在数据库中
f)提供用户注册、图书管理(选作)
二、 所用仪器、设备
PC机,基于window xp 或者windows server 平台,使用JDK+Netbeans+MySQL为开发工具。
三、 实验原理
PC机,基于window xp 或者windows server 平台,使用JDK+Netbeans+MySQL为开发工具
1、(1)实验原理图
↓
↓ ↓
↓
↓
(2)表示层有:login.jsp显示登录页面;fail.jsp显示登录失败页面;chatsuccess.jsp登录成功页面
及聊天室页面 说话框
聊天显示框
控制逻辑层:login.jsp进行登录,judge.jsp进行判断,成功进入chatsuccess.jsp点击按钮,跳转到chatroom.jsp,说话时跳到say.jsp,如果登录失败,跳到Fail.jsp
数据层:程序中用到了cookie、session以及application,cookie用来保存用户的会话信息,显示登录次数,session用来防止匿名用户登录,application用来记录聊天信息。
(3)用到了Servlet的:全都是由JSP换成Servlet之后再执行的。
(4)数据设计:使用数据库。
(5)跳转关系:login.jsp进行登录,judge.jsp进行判断,成功进入chatsuccess.jsp点击按钮,跳转到chatroom.jsp,说话时跳到say.jsp,如果登录失败,跳到Fail.jsp
(6)会话管理技术及内置对象在程序中的应用:程序中用到了cookie,session以及application。
四、实验代码
登录代码,以及判断代码已经有前面的实验完成。下面只扩关于登录到聊天室之后的部分。
通过judge.jsp判断后跳转到chatsuccess.jsp页面,显示登录成功,并cookie跟踪用户显示用户第几次登录。
代码为
<% String name1= session.getAttribute("Uname").toString();
String password1= session.getAttribute("Password").toString();
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/chat", "root", "javaee");
String sql="select * from user where username='"+name1+"'";
Statement s=conn.createStatement();
ResultSet rs=s.executeQuery(sql);
rs.next();
String name=rs.getString("username");
String psw=rs.getString("password");
if(name1.equals(name)&&password1.equals(psw)){ %>
<h1>欢迎!<%=name1%>您已成功登录</h1>
<%
Cookie cookie=null;
Cookie[] cookies=request.getCookies();
if(cookies!=null){
boolean flag=false;
for(int i=0;(i<cookies.length)&&(!flag);i++){
if(cookies[i].getName().equals("VisitTime")){
String v=cookies[i].getValue();
int value=Integer.parseInt(v)+1;
cookies[i].setValue(Integer.toString(value));
response.addCookie(cookies[i]);
flag=true;
cookie=cookies[i];
}
}
}
if(cookie==null){
int maxAge=10000;
cookie=new Cookie("VisitTime","1");
cookie.setPath(request.getContextPath());
cookie.setMaxAge(maxAge);
response.addCookie(cookie);
}
%>
<h3>欢迎您第<%=cookie.getValue()%>次访问本页面</h3>
<h3></h3>
登录成功后转到chatroom界面,在说话时跳转到say.jsp处理聊天信息
<% java.util.Date now = new java.util.Date();
DateFormat d3 = DateFormat.getTimeInstance();
String str3 = d3.format(now);
out.println(str3+" ");
out.println(vn+":");
ArrayList al_say=new ArrayList();
al_say=(ArrayList)application.getAttribute("say");
for (int says=0;says<al_say.size();says++)
{ %>
<%=al_say.get(says)%>
<%
ArrayList al_say = new ArrayList();
al_say = (ArrayList)application.getAttribute("say");
for(int says = 0; says < al_say.size(); says++){
%>
<div class="head"><%=al_say.get(says)%></div>
<div class="msg"><%=al_say.get(++says)%></div>
<%
}
// session.invalidate(); // 删除session
%>
五、实验结果截图
聊天室界面:
两人对话:
第二篇:J2EE实验1实验报告
基于JavaEE的项目开发实验报告
实验题目 Java 的集合框架
姓 名 宋书龙 同组同学 无
学 号 091142121
实验日期 2010.9.12 成 绩_______________________
一、 实验目的
1、熟练掌握Java中Arrays类的使用
2、理解并掌握List、Set、Map接口特性和常用实现类的使用
二、 实验步骤及执行分析结果
1、分析并上机验证所给代码Matrix.java,实现矩阵的加、减、乘、除,给出程序的运行结果和分析说明,要求不列举出程序中所有用到的Arrays类的相关方法.
2、分析并上机验证所给代码UnrepeatRandomNumber.java,给出程序的运行结果和分析说明,要求列举出程序中所有用到相关框架类及相关方法和实现的功能。
3、分析并上机验证所给代码 TestMap.java,给出程序的运行结果和分析说明,要求列举出程序中所有用到相关框架类及相关方法和实现的功能。
4、使用所学的框架类编写一个程序,要求能够实现员工基本信息(包含员工编号、姓名、职务和薪金等信息)的处理,包括添加一个新员工、员工信息的修改、查找、遍历和删除)。
建表:
create table employee
(
id int primary key auto_increment,
emp_name varchar(20) not null,
emp_no varchar(20) not null,
emp_post varchar(20) ,
emp_saraly int
)
实体类:
package com.rh.entity;
public class Employee implements java.io.Serializable{
private Integer id;
private String name;
private String no;
private String post;
private int saraly;
public Employee(Integer id, String name, String no, String post, int saraly) {
super();
this.id = id;
this.name = name;
this.no = no;
this.post = post;
this.saraly = saraly;
}
public Employee(String name, String no, String post, int saraly) {
super();
this.name = name;
this.no = no;
this.post = post;
this.saraly = saraly;
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
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 String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public int getSaraly() {
return saraly;
}
public void setSaraly(int saraly) {
this.saraly = saraly;
}
}
工具类:
package com.rh.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcUtil {
private static String driver = "com.mysql.jdbc.Driver";
private static String username = "root";
private static String password = "hejie";
private static String url = "jdbc:mysql://localhost:3306/hejietest";
public static Connection getConnection() throws ClassNotFoundException, SQLException{
Class.forName(driver);
Connection con = DriverManager.getConnection(url,username,password);
return con;
}
public static void closeRel(Connection con,Statement sta,ResultSet rs){
try {
if(con!=null)con.close();
if(sta!=null)sta.close();
if(rs!=null)rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
dao:访问数据库
package com.rh.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 com.rh.entity.Employee;
public class EmployeeDao {
public Employee save(Employee emp) throws ClassNotFoundException, SQLException{
Connection con = JdbcUtil.getConnection();
String sql = "insert into employee(emp_name,emp_no,emp_post,emp_saraly) values("+emp.getName()+","+emp.getNo()+","+emp.getPost()+","+emp.getSaraly()+")";
Statement sta = con.createStatement();
sta.executeUpdate(sql);
JdbcUtil.closeRel(null, sta, null);
return emp;
}
public Employee update(Employee emp) throws ClassNotFoundException, SQLException{
Connection con = JdbcUtil.getConnection();
String sql = "update employee set emp_name="+emp.getName()+",emp_no="+emp.getNo()+",emp_post="+emp.getPost()+",emp_saraly="+emp.getSaraly()+" where id="+emp.getId();
Statement sta = con.createStatement();
sta.executeUpdate(sql);
JdbcUtil.closeRel(null, sta, null);
return emp;
}
public Employee remove(Employee emp) throws ClassNotFoundException, SQLException{
Connection con = JdbcUtil.getConnection();
String sql = "delete from employee where id="+emp.getId();
Statement sta = con.createStatement();
sta.executeUpdate(sql);
JdbcUtil.closeRel(null, sta, null);
return emp;
}
public List<Employee> findAll() throws ClassNotFoundException, SQLException{
Connection con = JdbcUtil.getConnection();
String sql = "select * from employee";
Statement sta = con.createStatement();
ResultSet rs = sta.executeQuery(sql);
List<Employee> lists = new ArrayList<Employee>();
while(rs.next()){
Employee e = new Employee(
rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getInt(5)
);
lists.add(e);
}
JdbcUtil.closeRel(null, sta, rs);
return lists;
}
public Employee findByName(String name) throws ClassNotFoundException, SQLException{
Connection con = JdbcUtil.getConnection();
String sql = "select * from employee where name="+name;
Statement sta = con.createStatement();
ResultSet rs = sta.executeQuery(sql);
while(rs.next()){
Employee e = new Employee(
rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getInt(5)
);
JdbcUtil.closeRel(null, sta, rs);
return e;
}
JdbcUtil.closeRel(null, sta, rs);
return null;
}
public Employee findById(Integer id) throws ClassNotFoundException, SQLException{
Connection con = JdbcUtil.getConnection();
String sql = "select * from employee where id="+id;
Statement sta = con.createStatement();
ResultSet rs = sta.executeQuery(sql);
while(rs.next()){
Employee e = new Employee(
rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getInt(5)
);
JdbcUtil.closeRel(null, sta, rs);
return e;
}
JdbcUtil.closeRel(null, sta, rs);
return null;
}
}
servlet:
package com.rh.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rh.dao.EmployeeDao;
import com.rh.entity.Employee;
public class SelectServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public SelectServlet() {
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String idd = request.getParameter("id");
Integer id = 0;
try {
id = Integer.parseInt(idd);
} catch (NumberFormatException e1) {
}
EmployeeDao ed = new EmployeeDao();
if (name != null) {
try {
Employee e = ed.findByName(name);
request.setAttribute("result", e);
request.getRequestDispatcher("/index.jsp").forward(request,
response);
return;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
} else if (id != 0) {
try {
Employee e = ed.findById(id);
request.setAttribute("result", e);
request.getRequestDispatcher("/modify.jsp").forward(request,
response);
return;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
} else {
try {
List list = ed.findAll();
request.setAttribute("result", list);
request.getRequestDispatcher("/index.jsp").forward(request,
response);
return;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
=====================
package com.rh.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rh.dao.EmployeeDao;
import com.rh.entity.Employee;
public class RemoverServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public RemoverServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String idd = request.getParameter("id");
int id = 0;
try {
id = Integer.parseInt(idd);
EmployeeDao ed = new EmployeeDao();
ed.remove(new Employee(id,null,null,null,0));
response.sendRedirect("SelectServlet");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
request.getRequestDispatcher("/error.jsp").forward(request, response);
}
}
}
=======================
package com.rh.servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rh.dao.EmployeeDao;
import com.rh.entity.Employee;
public class ModifyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public ModifyServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String no = request.getParameter("no");
String post = request.getParameter("post");
String saraly = request.getParameter("saraly");
String idd = request.getParameter("id");
try {
Integer id = Integer.parseInt(idd);
int saraly1 = Integer.parseInt(saraly);
EmployeeDao ed = new EmployeeDao();
ed.update(new Employee(id, name, no, post, saraly1));
response.sendRedirect("SelectServlet");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
request.getRequestDispatcher("/SelectServlet").forward(request, response);
return;
}
}
}
==========================
package com.rh.servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rh.dao.EmployeeDao;
import com.rh.entity.Employee;
public class AddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public AddServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name = request.getParameter("name");
String no = request.getParameter("no");
String post = request.getParameter("post");
String saraly = request.getParameter("saraly");
int sar = 0;
try {
sar = Integer.parseInt(saraly);
} catch (NumberFormatException e) {
}
EmployeeDao ed = new EmployeeDao();
try {
ed.save(new Employee(name,no,post,sar));
response.sendRedirect("SelectServlet");
// request.getRequestDispatcher("/SelectServlet").forward(request, response);
return ;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
jsp:
temp.jsp:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page="SelectServlet"></jsp:forward>
</body>
</html>
========================
modify.jsp
<%@ page language="java" import="com.rh.entity.*" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<%
Employee e = (Employee)request.getAttribute("result");
%>
<form action="ModifyServlet">
<input type="hidden" name="id" value="<%=e.getId()%>"/>
<input type="hidden" name="no" value="<%=e.getNo()%> "/>
<input type="hidden" name="name" value="<%=e.getName()%> "/>
<table border=1>
<tr>
<td>员工号:<%=e.getNo() %> </td>
</tr>
<tr>
<td>姓名:<%=e.getName() %> </td>
</tr>
<tr>
<td>职位:<input type="text" name="post" value="<%=e.getPost()%>"/></td>
</tr>
<tr>
<td>薪资:<input type="text" name="saraly" value="<%=e.getSaraly()%>"/></td>
</tr>
<tr>
<input type="submit" value="提交修改"></input>
</tr>
</table>
</form>
</body>
</html>
=========================
index.jsp
<%@ page language="java" import="java.util.*,com.rh.entity.*" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<div>
查询结果 <a href="add.jsp">添加</a>
<table>
<tr>
<td>员工号</td>
<td>姓名</td>
<td>职位</td>
<td>薪资</td>
<td>操作</td>
</tr>
<%
List<Employee> list = (List<Employee>)request.getAttribute("result");
int count = list.size();
for(int i=0;i<count;i++){
Employee e = list.get(i);
%>
<tr>
<td><%=e.getNo() %></td>
<td><%=e.getName() %></td>
<td><%=e.getPost() %></td>
<td><%=e.getSaraly() %></td>
<td>
<a href="SelectServlet?id=<%=e.getId() %>">修改</a>
<a href="RemoverServlet?id=<%=e.getId() %>">删除</a>
</td>
</tr>
<%
}
%>
</table>
</div>
</body>
</html>
==========================
error.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
error
</body>
</html>
===========================
add.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="AddServlet">
员工号:<input type="text" name="no"/><br></br>
姓名:<input type="text" name="name"/><br></br>
职位:<input type="text" name="post"><br></br>
薪资:<input type="text" name="saraly"><br></br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
==============
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>employee</display-name>
<welcome-file-list>
<welcome-file>temp.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>SelectServlet</display-name>
<servlet-name>SelectServlet</servlet-name>
<servlet-class>com.rh.servlet.SelectServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SelectServlet</servlet-name>
<url-pattern>/SelectServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>ModifyServlet</display-name>
<servlet-name>ModifyServlet</servlet-name>
<servlet-class>com.rh.servlet.ModifyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ModifyServlet</servlet-name>
<url-pattern>/ModifyServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>AddServlet</display-name>
<servlet-name>AddServlet</servlet-name>
<servlet-class>com.rh.servlet.AddServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddServlet</servlet-name>
<url-pattern>/AddServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>RemoverServlet</display-name>
<servlet-name>RemoverServlet</servlet-name>
<servlet-class>com.rh.servlet.RemoverServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RemoverServlet</servlet-name>
<url-pattern>/RemoverServlet</url-pattern>
</servlet-mapping>
</web-app>
三、 思考题
1、 实例Queue.java采用LinkList实现了队列操作,是否可用其他的集合类来实现队列功能??为什么?
答:ArrayLis能实现队列功能,在ArrayList的前面或中间插入数据时,你必须将其后的所有数据相应的后移,这样必然要花费较多时间,所以,当你的操作是在一列数据的后面添加数据而不是在前面或中间,并且需要随机地访问其中的元素时,使用ArrayList会提供比较好的性能
而访问链表中的某个元素时,就必须从链表的一端开始沿着连接方向一个一个元素地去查找,直到找到所需的元素为止,所以,当你的操作是在一列数据的前面或中间添加或删除数据,并且按照顺序访问其中的元素时,就应该使用LinkedList了。
2、 集合框架类使用时有哪些选取原则?
答:集合框架,接口Collection 用来管理多个对象,集合中的每个元素都是对象。Map,Map中没有对象,而是键值对,由Key,value组成的键值对,Key是不可重复的。value是可以相同的,一个Key和一个value一一对应。集合中用到的类,接口在java.util包中,在使用时注意将其引入import。Collection 接口。· Collection 接口是一组允许重复的对象。
Set 接口继承 Collection,但不允许重复,使用自己内部的一个排列机制。List 接口继承 Collection,允许重复,以元素安插的次序来放置元素,不会重新排列。Map接口是一组成对的键-值对象,即所持有的是key-value pairs。Map中不能有重复的key。拥有自己的内部排列机制。
四、 实验总结及体会
通过实验我体会到了Java语言处理有关员工学生等团体信息的方便性.