《Web程序设计(二)课程设计》实验报告院系名称:管理学院专业班级:电子商务级班学生姓名:学号:注册功能登录功能发布留言查看留言留言修改留言删除合计20分15分15分20分15分15分100分2014年07 月一、实验目的本实验属于设计性实验,目的是使学生在学完《Web程序设计(二)》课程的基本知识之后,通过使用Struts2和Hibernate等框架设计并开发一个简单的在线留言系统,进一步熟悉和领悟JSP、Struts2、Hibernate和Java语言的语法结构和使用方法,通过实验学习Java Web系统的MVC开发步骤与方法,任务是设计一个简单的在线留言系统。
二、实验步骤1.下载并安装配置JDK、eclipse、tomcat软件,使计算机有一个程序开发设计的软件支持。
2.用access建立一个数据库,在数据库中按要求建立表,包括用户注册基本信息表、年龄信息表、收入信息表、文化程度表、娱乐方式信息表、体育运动信息表、电影类型表。
3.在JDK中新建一个工程,建立一个包,名为beans,其下建立一个java文件,名为conndb,其中写入数据库调用信息。
4.用代码编写出html页面和相应的jsp文件,包括导航页面、注册页面、register.jsp、登录页面、login.jsp、问卷调查页面、diaochasend.jsp、问卷结果显示页面。
5.在控制面板中用sql.server接数据库。
6.配置并打开tomcat服务器,在服务器上运行项目,正确就完成,错误则进行修正。
7.在其他pc端进行检验,确保程序能够正常运行三、系统设计1、界面设计2、功能设计本系统具有注册功能、登录功能、发布留言功能、查看留言功能、留言修改功能、留言删除功能。
3、数据库设计本留言板程序采用的是sql server 2005进行数据库的设计与实现,数据库命名为dbs。
建表如下:① User表:② Tip表:四、系统的核心代码1、Index.jsp<body><div id="box"><div id="bottom1"><a href="regist.jsp">注册</a><a href="index.jsp">登录</a><a href="message_list.jsp">留言管理</a></div><div id="login"><div>用户登陆</div></div><div id="bottom"><s:form method="post" name="login" action="Login" theme="simple"validate="true"><p>用户名:<s:textfield name="erName" maxlength="20"cssClass="input-border"></s:textfield></p><p> 密码:<s:password name="user.passWord" maxlength="20"cssClass="input-border"></s:password> <label for="password"></label></p><p> <s:submit value="登陆" onmouseover="this.className='login-b2'" onmousedown="this.className='login-b3'" onmouseout="this.className='login-b'" cssClass="login-b" ></s:submit> <s:reset value="重置" ></s:reset></p></s:form>没有帐号? <a href="regist.jsp">点击注册</a></div><s:property value="#er"/></div>2、Regist.jsp<body><div id="box"><div id="bottom1"><a href="regist.jsp">注册</a><a href="index.jsp">登录</a><a href="message_list.jsp">留言管理</a></div><div id="login"><div>用户注册</div></div><div id="bottom"><s:form action="UserRegist" name="regist" method="post" validate="true" theme="simple"><p>用户姓名:<s:textfield label="用户名 " name="ername" maxlength="20" cssClass="input-border"></s:textfield></p><p>用户密码:<s:password label="密码 " name="regist.password" maxlength="20" cssClass="input-border"></s:password></p><p>密码确认:<s:password label="确认密码 " name="regist.repassword" maxlength="20" cssClass="input-border"></s:password></p><p>真实姓名:<s:textfield label="真实姓名 " name="" maxlength="20" cssClass="input-border"></s:textfield></p><p>性 别:<s:radio label="性别" list="#{'男':'男','女':'女'}" name="ex" value="'男'"></s:radio></p><p> <s:submit value="注册" ></s:submit> <s:reset value="重置" ></s:reset></p></s:form>已有帐号?<a href="index.jsp">点击登陆</a></div></div></body>3、AddMes.jsp<body><div id="box"><div id="bottom1"><a href="regist.jsp">注册</a><a href="index.jsp">登录</a><a href="addmes.jsp">留言</a><a href="message_list.jsp">留言管理</a></div><div id="login"><div>请您留言</div></div><div id="bottom"><form action="MessageServlet" method="post" name="form"><p>留言标题:<input name="title" type="text" size="50" /></p><p> 留言内容:<textarea name="content" cols="38" rows="5"></textarea> <label for="password"></label></p><p> <input type="submit" name="button" id="button" value="提交留言" /> <input type="reset" name="button2" id="button2" value="重新填写" /> </p></form></div></div></body>4、MessageList.jsp<body><div id="box"><div id="bottom1"><a href="regist.jsp">注册</a><a href="index.jsp">登录</a><a href="addmes.jsp">留言</a><a href="message_list.jsp">留言管理</a></div><div id="login"><div>留言内容及管理</div></div><div id="bottom"><table width="90%" border="1" id="table"><c:choose><c:when test="${empty pageModel.list}"><tr><td height="68" colspan="3">没有留言内容!</td></tr></c:when><c:otherwise><!-- 循环输出留言信息 --><c:forEach items="${pageModel.list}" var="m"><tr><td rowspan="2">用户名:${ername}<br />用户ID:${erId}</td><td height="43">留言标题:${m.title}</td><td rowspan="2" align="center"><c:if test="${!empty user}"><a href="ManagerServlet?method=delete&id=${m.id}">删除</a><a href="ManagerServlet?method=update&id=${m.id}">更改</a></c:if></td></tr><tr><td height="76">留言内容:${m.content}</td></tr></c:forEach></c:otherwise></c:choose></table></div></div></body>5、DBConnect.javapackage com.kuan.dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet; //导入数据库操作的类import java.sql.SQLException;import java.sql.Statement;public class DBConnect {private static Connection conn = null; //连接对象private static Statement stmt = null;private static String user="sa";//数据库的用户名private static String password=""; //数据库的密码//语句对象private static ResultSet rs = null; //结果集对象private static StringAccessDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//保存Mssql驱动程序private static StringAccessURL="jdbc:sqlserver://localhost:1433;integratedSecurity=true;DatabaseName =dbs";//保存Mssql连接字符串public DBConnect() {conn = null;}public static Connection getConnection() {try{Class.forName(AccessDriver).newInstance();conn=DriverManager.getConnection(AccessURL,user,password);}catch(Exception e){e.printStackTrace();}return conn;}//加载驱动,连接数据库public static Statement getStmt() {try{Class.forName(AccessDriver).newInstance();conn=DriverManager.getConnection(AccessURL,user,password);stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_ UPDATABLE);}catch(Exception e){e.printStackTrace();}return stmt;}public static ResultSet getRS(String sql) throws SQLException {//查询ResultSet rs ;Statement stmt = DBConnect.getStmt();rs = stmt.executeQuery(sql);return rs;}public static int Update(String sql) throws SQLException {//插入int ret = 0 ;Statement stmt = DBConnect.getStmt();ret = stmt.executeUpdate(sql);return ret;}public static void close(){if(rs!=null)try {rs.close();} catch (SQLException e) { } if(stmt!=null)try {stmt.close();} catch (SQLException e) { } if(conn!=null)try {conn.close();} catch (SQLException e) { } }}6、User.javapackage com.kuan.model;public class User {private Integer id; //ID编号private String username; //用户名private String password; //密码private String name; //姓名private String sex; //性别public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) { ername = username;}public String getPassword() {return password;}public void setPassword(String password) { this.password = password;}public String getName() {return name;}public void setName(String name) { = name;}}7、Message.javapackage com.kuan.model;public class Message {private Integer id; //ID编号private String title; //标题private String content; //内容private Integer userId; //用户IDpublic Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getContent() {return content;}public void setContent(String content) { this.content = content;}public Integer getUserId() {return userId;}public void setUserId(Integer userId) { erId = userId;}}UserDao.javapackage com.kuan.dao;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import er;public class UserDao {public void saveUser(User user){Session session = null; //Session对象try {//获取Sessionsession.beginTransaction(); //开启事物session.save(user); //持久化usersession.getTransaction().commit(); //提交事物} catch (Exception e) {e.printStackTrace(); //打印异常信息}finally{}}/*** 查询所有用户信息* @return List集合*/public List<User> findAllUser(){Session session = null; //Session对象List<User> list = null; //List集合try {//获取Sessionsession.beginTransaction(); //开启事物String hql = "from user1";list = session.createQuery(hql) //创建Query对象 .list(); //获取结果集session.getTransaction().commit(); //提交事物} catch (Exception e) {e.printStackTrace(); //打印异常信息session.getTransaction().rollback();//回滚事物}finally{//关闭Session}return list;}/*** 通过用户名和密码查询用户* 用于登录* @param username 用户名* @param password 密码* @return User对象*/public User findUser(String username, String password){Session session = null; //Session对象User user = null; //用户try {//获取Sessionsession.beginTransaction(); //开启事物//HQL查询语句String hql = "from user1 u where ername=? and u.password=?";Query query = session.createQuery(hql) //创建Query对象.setParameter(0, username)//动态赋值.setParameter(1, password);//动态赋值user = (User)query.uniqueResult(); //返回User对象session.getTransaction().commit(); //提交事物} catch (Exception e) {e.printStackTrace(); //打印异常信息}finally{}return user;}/*** 判断指定用户名的用户是否存在* @param username 用户名* @return*/public boolean findUserByName(String username){Session session = null; //Session对象boolean exist = false;try {//获取Sessionsession.beginTransaction(); //开启事物//HQL查询语句String hql = "from user1 u where ername=?";Query query = session.createQuery(hql) //创建Query对象.setParameter(0, username);//动态赋值Object user = query.uniqueResult(); //返回User对象//如果用户存在exist为trueif(user != null){exist = true;}session.getTransaction().commit(); //提交事物} catch (Exception e) {e.printStackTrace(); //打印异常信息}finally{}return exist;}}8、UserLogin.javapackage com.kuan.action;import java.sql.ResultSet;import java.util.Map;import com._52run.struts_task.dao.DBConnect;import er;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class UserLogin extends ActionSupport {private static final long serialVersionUID = 1L;private User user ;private String msg ;public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public User getUser() {return user;}public void setUser(User user) {er = user;}public String execute() throws Exception {String sql = "select * from user1 where userName = '"+user.getUsername()+"' and uPwd = '"+user.getPassword()+"'";ResultSet rs = DBConnect.getRS(sql);if(!rs.next()) {msg = "用户名或密码错误,请重新输入!";DBConnect.close();return INPUT;} else {ActionContext actionContext = ActionContext.getContext();Map<String, String> session = actionContext.getSession();session.put("user", user.getUsername());//ActionContext.getContext().getSession().put("user",user.getuserName());DBConnect.close();return SUCCESS;}}/* public void validate() {String sql = "select * from regist where userName = '"+user.getuserName()+"' and uPwd = '"+user.getpassWord()+"'";try {ResultSet rs = DBConnect.getRS(sql);if(!rs.next()) {this.addFieldError("msg", "用户名或密码错误,请重新输入");}} catch (SQLException e) {// TODO Auto-generated catch blockSystem.out.print("出现异常");}}*/}9、UserRegist.javapackage com.kuan.action;import java.sql.ResultSet;import java.util.Map;import com._52run.struts_task.dao.DBConnect;import er;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class UserLogin extends ActionSupport {private static final long serialVersionUID = 1L;private User user ;private String msg ;public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public User getUser() {return user;}public void setUser(User user) {er = user;}public String execute() throws Exception {String sql = "select * from user1 where userName = '"+user.getUsername()+"' and uPwd = '"+user.getPassword()+"'";ResultSet rs = DBConnect.getRS(sql);if(!rs.next()) {msg = "用户名或密码错误,请重新输入!";DBConnect.close();return INPUT;} else {ActionContext actionContext = ActionContext.getContext();Map<String, String> session = actionContext.getSession();session.put("user", user.getUsername());//ActionContext.getContext().getSession().put("user",user.getuserName());DBConnect.close();return SUCCESS;}}/* public void validate() {String sql = "select * from regist where userName = '"+user.getuserName()+"' and uPwd = '"+user.getpassWord()+"'";try {ResultSet rs = DBConnect.getRS(sql);if(!rs.next()) {this.addFieldError("msg", "用户名或密码错误,请重新输入");}} catch (SQLException e) {// TODO Auto-generated catch blockSystem.out.print("出现异常");}}*/}五、实验总结通过这次Web程序设计实验,我把Web程序设计课程的知识框架重新梳理清楚,对Struts2和Hibernate等框架更加熟悉,对JSP、Struts2、Hibernate和Java语言的语法结构和使用方法更加灵活和得心应手。