JavaEE实验3报告_昆明理工大学

昆明理工大学信息工程与自动化学院学生实验报告

20## 2013   学年学期

课程名称:JAVA EE技术    开课实验室:信自楼444       2012 年  12 月 19  日

一、实验目的

通过上机,熟练掌握JSP编程、JavaBean的应用以及EL和JSTL的应用。

二、内容及要求

1、上机内容:

l  编写两个JavaBean,实现值类和业务类。

l  编写servlet实现控制的功能;

l  编写top.jsp、left.jsp、 bottom.jsp、 employeemain.jsp,实现页面的组装。

l  在left.jsp增加登录部分,若登录成功后在同样的位置显示:欢迎您和用户号。

l  在top.jsp中增加两个链接,实现投票和管理投票中心,其中管理投票中心只有在登录成功后方显示。。

三、步骤及具体实施

1、简要描述程序的开发流程、文件部署截图、及各种配置;

2、程序代码

package chapter10.dao;

import java.util.*;

import java.sql.*;

import chapter10.value.EmployeeValue;

public class EmployeeBusiness {

    

     public List getList() throws Exception

     {

         List empList=new ArrayList();

         String sql="select * from userinfo";

         Connection cn=null;

         try

         {

              Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

              cn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=javaee", "root", "123");

              Statement stmt = cn.createStatement();

              //PreparedStatement ps=cn.prepareStatement(sql);

              ResultSet rs=stmt.executeQuery(sql);

              while(rs.next())

              {

                   EmployeeValue ev=new EmployeeValue();

                   ev.setUserId(rs.getString("userid"));

                   ev.setPassWord(rs.getString("password"));

                   ev.setName(rs.getString("name"));

                   ev.setAge(rs.getInt("age"));

                   empList.add(ev);  

              }

              rs.close();

              stmt.close();

         }

         catch(Exception e)

         {

              throw new Exception("取得员工列表错误:"+e.getMessage());

         }

         finally

         {

              cn.close();

         }

         return empList;

     }

}

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

importjavax.servlet.http.HttpServletRequestWrapper;

import javax.servlet.http.*;

//请求数据字符编码处理过滤器

publicclass CharEncodingFilter implements Filter

{

     private FilterConfig config=null;

     private String contentType=null;

     private String code=null;

     //初始化方法,在过滤器对象创建后被调用

     publicvoid init(FilterConfig config) throws ServletException

     {

         this.config=config;

         //取得Filter初始化参数

         //contentType=config.getInitParameter("contentType");

         code=config.getInitParameter("encoding");

         System.out.println("init...");

     }

     //过滤方法

     publicvoid doFilter(ServletRequest req, ServletResponse res,

              FilterChain chain) throws IOException, ServletException

     {

         //转换为HTTP请求对象

         HttpServletRequest request=(HttpServletRequest)req;

         //request.setAttribute("infoType", "image/jpeg");

        

         request.setCharacterEncoding(code);//设置字符编码集

         res.setCharacterEncoding(code);//设置字符编码集

         System.out .println("filting...");

        

        

         //继续下个过滤器

         chain.doFilter(req, res);

     }

     //销毁方法,在过滤器销毁前被调用

     publicvoid destroy()

     {

         System.out.println("destroy...");

     }

}

<%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/fmt"prefix="fmt"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/sql"prefix="sql"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/xml"prefix="x"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/functions"prefix="fn"%>

<%

  String d_id =(String)(request.getParameter("id"));

System.out.println("d_id ="+d_id);

%>

<sql:setDataSourcedriver="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost/cityoa"user="root"password="abc123"

var="infoData"scope="request"/>

<sql:updatesql="update userinfo1 set TYPE=? Where USERID=?"dataSource="${infoData}"

var="result"scope="request">

<sql:paramvalue="black"/>

<sql:paramvalue="<%=d_id%>"/>

</sql:update>

<jsp:forwardpage="admin.jsp"/>

String path = request.getContextPath();

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

%>

<jsp:useBeanid="now"class="java.util.Date"></jsp:useBean>

<%

         request.setCharacterEncoding("gb2312");

         HttpSession session1=request.getSession();

         //取得会话对象中保存的验证码,由验证码生成Servlet存入

         String userid=(String)session1.getAttribute("userid");

         String note1=request.getParameter("note1");

         %>

<sql:setDataSourcedriver="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost/cityoa"user="root"password="abc123"

var="infoData"scope="request"/>

<sql:updatesql="insert into notes(id,note,dates) values(?,?,?)"dataSource="${infoData}"

var="result"scope="request">

    

<sql:paramvalue="${userid}"/>

<sql:paramvalue="<%=note1%>"/>

<sql:paramvalue="${now}"/>

</sql:update>

<jsp:forwardpage="luntan.jsp"/>

<%@pagecontentType="text/html;charset=GBK"%>

<%@pagelanguage="java"import="java.sql.* "%>

<jsp:useBeanid="connDbBean"scope="page"class="javaee.ch04.VoteConn"/>

<%request.setCharacterEncoding("gb2312");

String userid = request.getParameter("userid");

  String name = request.getParameter("name");

//String password=request.getParameter("password");

         //String repassword=request.getParameter("repassword");

         //String username=request.getParameter("name");

        

         //File pathname=new File("F:/apache-tomcat-7.0.23/webapps/04/20.jpg");

         //FileInputStream filename=new FileInputStream(pathname);

        

         //String photo=BinaryStream(//filename,(int)name.length());

    String sql = "insert into userinfo1(USERID,PASSWORD,NAME,SEX,ADDRESS,TEXT,PATH) ";

    sql=sql+"values ('"+userid+"','"+userid+"','"+name+"',"+"'man','云南','my nameis ben','')";

System.out.println("sql:"+sql);

     connDbBean.executeUpdate(sql);

%>

<jsp:forwardpage="admin.jsp"/>

<%@pagecontentType="text/html;charset=GBK"%>

<%@pagelanguage="java"import="java.sql.* "%>

<jsp:useBeanid="connDbBean"scope="page"class="javaee.ch04.VoteConn"/>

<%request.setCharacterEncoding("gb2312");

String userid = request.getParameter("userid");

  String name = request.getParameter("name");

//String password=request.getParameter("password");

         //String repassword=request.getParameter("repassword");

         //String username=request.getParameter("name");

        

         //File pathname=new File("F:/apache-tomcat-7.0.23/webapps/04/20.jpg");

         //FileInputStream filename=new FileInputStream(pathname);

        

         //String photo=BinaryStream(//filename,(int)name.length());

    String sql = "insert into userinfo1(USERID,PASSWORD,NAME,SEX,ADDRESS,TEXT,PATH) ";

    sql=sql+"values ('"+userid+"','"+userid+"','"+name+"',"+"'man','云南','my nameis ben','')";

System.out.println("sql:"+sql);

     connDbBean.executeUpdate(sql);

%>

<jsp:forwardpage="admin.jsp"/>

<%@pagecontentType="text/html;charset=GBK"%>

<%@pagelanguage="java"import="java.sql.*"%>

<jsp:useBeanid="connDbBean"scope="page"class="javaee.ch04.VoteConn"/>

<%request.setCharacterEncoding("gb2312");

  String nn = request.getParameter("note");

if(nn != null&& nn.length() >0) {

     ResultSet tmpRs = connDbBean.executeQuery("select max(id) As maxid from vote");

tmpRs.next();

int max_id = tmpRs.getInt("maxid") + 1;

tmpRs.close();

    String sql = "insert into vote values (" + max_id + ",'";

      sql += nn + "',0);";

     connDbBean.executeUpdate(sql);

  }

x="c"%>

<jsp:useBeanid="connDbBean"scope="page"class="javaee.ch04.VoteConn"/>

<html>

<bodybgcolor="#FFFFFF">

<%@includefile="/gl_top.jsp"%>

<p><fontcolor="#990000"size="+2"><u>同学信息维护中心</u></font></p>

<%

     ResultSet tempRs = connDbBean.executeQuery("SELECT USERID,NAME,TYPE FROM userinfo1 ");

%>

<tablewidth="80%"border="0"cellspacing="1"cellpadding="2">

<tr>

<tdwidth="10%"class="text1"bgcolor="#99CCFF">

<divalign="center">用户ID</div>

</td>

<tdwidth="40%"class="text1"bgcolor="#99CCFF">

<divalign="center">姓名</div>

</td>

<tdwidth="20%"class="text1"bgcolor="#99CCFF">

<divalign="center">类型</div>

</td>

<tdwidth="10%"class="text1"bgcolor="#99CCFF">

<divalign="center">删除</div>

</td>

<tdwidth="20%"class="text1"bgcolor="#99CCFF">

<divalign="center">操作</div>

</td>

</tr>

<%

int i = 1 ;

     i=i+1;

}

s="text1"colspan="2">&nbsp;</td>

</tr>

<tr>

<tdclass="text1"colspan="3"bgcolor="#99CCFF">增加用户</td>

</tr>

<tr>

<tdclass="text1"colspan="3">

<formmethod="post"action="adduser_gl.jsp">

<p>账号:

<inputtype="text"name="userid"size="50"maxlength="50">

</p>

<p>   用户名:

<inputtype="text"name="name"size="50"maxlength="50">

</p>

<p>   

<inputtype="submit"name="Submit"value="提交">

<inputtype="reset"name="Submit2"value="重置">

String path = request.getContextPath();

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

%>

<jsp:useBeanid="connDbBean"scope="page"class="javaee.ch04.VoteConn"/>

<%

  String d_id = request.getParameter("id");

if(d_id!=null&& d_id.length()>0) {

connDbBean.executeUpdate("update userinfo1 set TYPE=null Where USERID=" + d_id);

  }   

ng d_id =(String)(request.getParameter("id"));

System.out.println("d_id ="+d_id);

%>

<sql:setDataSourcedriver="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost/cityoa"user="root"password="abc123"

var="infoData"scope="request"/>

<sql:updatesql="DELETE FROM notes Where id=?"dataSource="${infoData}"

ng d_id =(String)(request.getParameter("id"));

System.out.println("d_id ="+d_id);

%>

<sql:setDataSourcedriver="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost/cityoa"user="root"password="abc123"

ring d_id = request.getParameter("id");

//if(d_id!=null && d_id.length()>0) {

  System.out.println("要删除的id为:"+request.getParameter("id"));

connDbBean.executeUpdate("DELETE FROM userinfo1 Where USERID="+d_id );

//}   

%>

<jsp:forwardpage="admin.jsp"/>

<%@pagelanguage="java"import="java.sql.*"%>

<jsp:useBeanid="connDbBean"scope="page"class="javaee.ch04.VoteConn"/>

<%

  String d_id = request.getParameter("id");

if(d_id!=null&& d_id.length()>0) {

connDbBean.executeUpdate("DELETE FROM VOTE Where id=" + d_id);

  }   

tot_num=0;

int tmp_num=0;

// 获取总票数

   ResultSet tmpRs = connDbBean.executeQuery("select sum(c_num) As totalnum from vote");

tmpRs.next();

   tot_num = tmpRs.getInt("totalnum");

tmpRs.close();

   ResultSet rs = connDbBean.executeQuery("select * from vote");

int i=1;

while(rs.next()) { %>

              <%

                   tmp_num = rs.getInt("c_num");

              %>

              <tdvalign="bottom"align="center">投票率:<%=Math.floor(tmp_num*100/tot_num)%></br>

              <imgsrc="back.gif"width="10"height="<%=Math.floor(tmp_num*200/tot_num)%>"></br>

              <hr>

              选项<%=i%>:<%=rs.getString("note")%>

              </td>

<%

i++;

    }

%>

</tr>

</table>

<ahref="main.jsp">返回</a>

<%

rs.close();

%>

</BODY>

</HTML>

<%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%>

<divstyle="margin:10px 10px 20px 30px;width:1024px; height:222px;background:url(head.jpg)">

<divstyle="margin:192px 0px 0px ;width:1024px; height:30px;">

<tablewidth="100%"height="100%"border="0"cellspacing="0"cellpadding="0">

<trvalign="bottom"align="center">

<tdwidth="300"valign="middle"align="center"><fontcolor="#0000FF"face="微软雅黑"size=5px>

<ahref="guanlimain.jsp">首页</a></font></td>

<tdwidth="300"valign="middle"align="center"><fontcolor="#00FF00"face="微软雅黑"size=5px>

<ahref="admin.jsp">学生信息管理</a></font></td>

<tdwidth="300"valign="middle"align="center"><fontcolor="#CC0000"face="微软雅黑"size=5px>

<ahref="glnote.jsp">班级论坛管理</a></font></td>

<tdwidth="300"valign="middle"align="center"><fontcolor=#174cacface="微软雅黑"size=5px><ahref="vote_gl.jsp">班委选举管理</a></font></td>

<tdwidth="300"valign="middle"align="center"></td>

<tdwidth="200"valign="middle"align="center"><ahref="Logout"target=_top>注销</a></td>

</tr>

</table>

</div>

</div>

<title>班级论坛</title>

     <metahttp-equiv="pragma"content="no-cache">

     <metahttp-equiv="cache-control"content="no-cache">

     <metahttp-equiv="expires"content="0">

     <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

     <metahttp-equiv="description"content="This is my page">

</head>

<body>

<%@includefile="/gl_top.jsp"%>

<%

         HttpSession session1=request.getSession();

         //取得会话对象中保存的验证码,由验证码生成Servlet存入

         String userid=(String)session1.getAttribute("userid");

         System.out.println("userid:"+userid);

         %>

<%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>

<p>欢迎来到班级论坛</p>

<sql:setDataSourcedriver="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost/cityoa"user="root"password="abc123"

var="noteData"scope="request"/>

<sql:querysql="select * from notes"dataSource="${noteData}"

var="notelist"scope="request">

<%System.out.println("userid1:"+userid);%>

</sql:query>

<hr/>

     <c:forEachvar="row"items="${notelist.rows}">

     <table>

     <tr>

     ${row.id }:<br>

     <td>

<divstyle="border:solid 1px black;">

<p>${row.note }</p>

     </div>

     发表时间:<fmt:formatDatevalue="${row.dates}" type="both"

     pattern="yy年MM月dd日 "/>

    

     <br>

     </td>

     <td><ahref="delnote_gl.jsp?id=${row.id}">删除</a></td>

     </tr>

     </table>

     </c:forEach>

</body>

</html>

<%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>

<%

String path = request.getContextPath();

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

%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">

<title>My JSP 'grxx.jsp' starting page</title>

<divstyle="margin:10px 0 0 8px;width:100%;">

<h1>修改个人信息</h1>

<hr/>

<p></p>

<ahref="mylist.jsp">查询个人信息</a>

<br/>

<ahref="xuigaimm.jsp">修改密码</a>

<br/>

<ahref="xuigaixx.jsp">修改信息</a>

</div>

</td>

<td>

<divstyle="margin:10px 0 0 8px;width:100%;">

</div>

</td>

</tr>

</table>

</body>

</html>

<%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%>

<%

String path = request.getContextPath();

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

%>

<%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>班级网站</title>

     <metahttp-equiv="pragma"content="no-cache">

     <metahttp-equiv="cache-control"content="no-cache">

     <metahttp-equiv="expires"content="0">

     <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

     <metahttp-equiv="description"content="This is my page">

    

    

</head>

<body>

<%@includefile="/gl_top.jsp"%>

<tablewidth="100%"border="0">

<tr>

<td>

<divstyle="margin:10px 0 0 8px;width:100%;">

<%@pagecontentType="text/html;charset=GBK"%>

<%@pagelanguage="java"import="java.sql.*"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>

<jsp:useBeanid="connDbBean"scope="application"class="javaee.ch04.VoteConn"/>

<head>

     <title>班委候选名单</title>

</head>

<%

ResultSet rs;

rs= connDbBean.executeQuery("select * from vote ");

%>

<bodybgcolor="#FFFFFF">

<%@includefile="/top.jsp"%>

<center>

<h2><fontcolor="#0000ff">请您投票:</font></h2>

<formaction="vote.jsp"method="post">

<Tableborder="1"bgcolor="#00FFFF">

<%while(rs.next())

 { %>

<TR>

<TD>

<inputtype="radio"name="type"value=<%=rs.getString("id")%>>

<ahref="mylist_vote.jsp?username=<%=rs.getString("note")%>">

<%=rs.getString("note")%></a></TD></TR>

     <%}

     rs.close();

     %>

<TRalign="center"><TD><Inputtype="submit"value="投票"></TD>

</TR>

<TR><TD><Ahref="details.jsp">查看投票</A></TD></TR>

</Table>

</form>

</center>

</body>

</html>

<%@pagelanguage="java"import="java.util.*"pageEncoding="gb2312"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/fmt"prefix="fmt"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/sql"prefix="sql"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/xml"prefix="x"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/functions"prefix="fn"%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>班级论坛</title>

     <metahttp-equiv="pragma"content="no-cache">

     <metahttp-equiv="cache-control"content="no-cache">

     <metahttp-equiv="expires"content="0">

     <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

     <metahttp-equiv="description"content="This is my page">

</head>

<body>

<%

         HttpSession session1=request.getSession();

         //取得会话对象中保存的验证码,由验证码生成Servlet存入

         String userid=(String)session1.getAttribute("userid");

         System.out.println("userid:"+userid);

         %>

<%@includefile="/top.jsp"%>

<p>欢迎来到班级论坛</p>

<sql:setDataSourcedriver="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost/cityoa"user="root"password="abc123"

var="noteData"scope="request"/>

<sql:querysql="select note,dates,NAME from notes,userinfo1 where id like USERID"dataSource="${noteData}"

var="notelist"scope="request">

<%System.out.println("userid1:"+userid);%>

</sql:query>

<hr/>

     <c:forEachvar="row"items="${notelist.rows}">

     ${row.NAME }:<br>

<divstyle="border:solid 1px black;">

<p>${row.note }</p>

     </div>

     发表时间:<fmt:formatDatevalue="${row.dates}" type="both"

     pattern="yy年MM月dd日 "/>

     <br>

     </c:forEach>

<hr/>

<p>我要发表:</p>

         <formmethod="post"action="addnote.jsp">

rs= connDbBean.executeQuery("select * from vote ");

%>

<h2><fontcolor="#0000ff">请您投票:</font></h2>

<formaction="vote.jsp"method="post">

<Tableborder="1"bgcolor="#00FFFF">

<%while(rs.next())

 { %>

<TR>

<TD>

<inputtype="radio"name="type"value=<%=rs.getString("id")%>>

<ahref="mylist_vote.jsp?username=<%=rs.getString("note")%>">

<%=rs.getString("note")%></a></TD></TR>

     <%}

     rs.close();

     %>

<TRalign="center"><TD><Inputtype="submit"value="投票"></TD>

</TR>

<TR><TD><Ahref="details.jsp">查看投票</A></TD></TR>

</Table>

</form>

</center>

</div>

</td>

<td>

<divstyle="margin:50px 10px 20px 30px;">

<h1>个人信息</h1>

     <hr/>

     <c:iftest="${empty infoDat}">

     <%System.out.println("infoData:null"); %>

     </c:if>

     <c:forEachvar="row"items="${result.rows}">

     <tableborder="1"align="center">

     <tr>

     <tdwidth="300"height="40"colspan="2"align="center">账号:${row.USERID}</td>

     <tdwidth='150'height='120'rowspan='3'>

     <imgsrc="${row.PATH}"width="150"height="120"/>

     </td>

     </tr>

     <tdwidth='150'height='40'align='center'>用户名:${row.NAME}</td>

     <tdwidth='150'height='40'align='center'>性别:${row.SEX}</td>

     </tr>

     <tdwidth='300'height='40'colspan='2'align='center'>家庭住址:${row.ADDRESS}</td>

     </tr>

     <tdwidth='450'height='200'colspan='3'valign='top'>个人简介:${row.TEXT}</td>

     </tr>

     </table>

</div>

</td>

</tr>

</table>

    

     </c:forEach>

                   <ahref='main.jsp'>返回首页</a>

                   </c:catch>

                   <c:outvalue="${error.message}"/>

     </body>

                   </html>

<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/fmt"prefix="fmt"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/sql"prefix="sql"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/xml"prefix="x"%>

<%@tagliburi="http://java.sun.com/jsp/jstl/functions"prefix="fn"%>

<%

         HttpSession session1=request.getSession();

         //取得会话对象中保存的验证码,由验证码生成Servlet存入

         //String userid=(String)session1.getAttribute("userid");

         %>

四、调试及运行

运行(程序截图)

五、实验结果、分析和结论

通过此次实验,深刻了解了JavaBeen和JSP编程技术的结合,掌握了JavaBeen如何负责取得数据库列表。在线投票系统中结合JavaBeen和JSP,由JavaBeen负责取得数据库vote列表。在JSP页面中使用useBeen动作定义JavaBeen对象,调用Been对象的方法,取得vote列表,通过后台管理页面,插入和删除被投票人,使用JSP代码脚本和表达式脚本进行列表显示。

也进一步熟悉了Java EE开发的环境的各种语法的运用,理解了jsp的运行原理和JavaBean技术,基本掌握了它们的运用,为以后的学习打下了良好的基础,不断提高自己Java EE的编程能力

相关推荐