中南民族大学计算机科学学院
JavaEE实验报告
2012—2013第一学期
课程名称: J2EE及其应用 院 系: 年 级: 2010 专 业: 计算机科学与技术(2)大班 小组成员(学号姓名):
10061103 张名扬
10061104 卢童
指导教师: 蓝雯飞 郭 峰 孔德志
20xx年12月31日
源代码如下:
1、 登录页面代码:login.html
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
<head>
<title>登陆界面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head>
<body bgcolor="#FFFFFF">
<center><h1>欢迎登陆系统</h1> </center>
<form name="login" method="post" action="Main">
<div align="center">
<label>
账号:
<input type=text name="userID" value="">
</label>
</div></label>
<label>
<div align="center">密码:
<input type=password name="password" value="" />
</div>
</label>
<div align="center">
</input>
<input type="submit" name="tj" value="提交" />
</input>
<input type="reset" name="reset" value="重置" />
</div>
</form>
</body>
</body>
</html>
2、 判断登录界面 main.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author lenovo
*/
public class Main extends HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here. You may use following sample code. */ response.setContentType("text/html;charset=GB2312");
request.setCharacterEncoding("gb2312");
String userID=request.getParameter("userID");
if(userID==null)
{
userID=" ";
}
String password=request.getParameter("password");
if(password==null)
{
password=" ";
}
if(userID.equals("张名扬")&&password.equals("10061103"))
{
request.getSession().setAttribute("userID",userID);
RequestDispatcher dispatcher=request.getRequestDispatcher("webchat"); dispatcher.forward(request, response);
}
else if(userID.equals("卢童")&&password.equals("10061104"))
{
request.getSession().setAttribute("userID",userID);
RequestDispatcher dispatcher=request.getRequestDispatcher("webchat"); dispatcher.forward(request, response);
}
else if(userID.equals("库承鹏")&&password.equals("10061106"))
{
request.getSession().setAttribute("userID",userID);
RequestDispatcher dispatcher=request.getRequestDispatcher("webchat"); dispatcher.forward(request, response);
}
else
{
RequestDispatcher dispatcher=request.getRequestDispatcher("Loginfail"); dispatcher.forward(request, response);
}
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
3、聊天主界面:webchat.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
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;
/**
*
* @author lenovo
*/
public class webchat extends HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
String name=request.getParameter("userID");
String temp=(String)getServletContext().getAttribute("online");
if(temp==null) {
temp="0";
}
// String talkwords=(String)getServletContext().getAttribute("talking");
out.println("<html>");
out.println("<head>");
out.println("<title>聊天窗口</title>");
// out.println("<script language="JavaScript"></sctipt>");
//out.println("<meta http-equiv="refresh" content="20">" );
//out.println("</script> ")
out.println("</head>");
out.println("<body><table border=\"0\"width=\"100%\"length=\"100%\"><tr>"); out.println("<td align=\"left\" valign=\"bottom\">");
out.println("<h5>欢迎!" + request.getSession().getAttribute("userID") + "同学,您已经成功登陆该系统</h5>");
out.println("<h5><a href=\"Abort\">退出</a></h5>");
out.println("<center><h1>欢迎登陆聊天系统</h1></center></td></tr></table><hr>");
out.println ("<center><h3>当前用户:"+temp+"<h3></center>");
out.println("<table width=\"50%\" border=\"1\" align=\"center\">");
out.println("<tr><td>");
out.println("<h4>聊天信息输出</4>");
out.println("<textarea name=\"talkingwindow\" cols=\"100\" rows=\"10\"
style=\"overflow:auto\">"+Content.content+ "</textarea>");
out.println("</tr></td> </table>");
out.println("<br>");
out.println("<form action=\"Content\" method=\"post\">");
out.println("<table width=\"50%\" border=\"1\" align=\"center\">"); out.println("<tr><td>");
out.println("<h4>聊天信息输入</h4>");
out.println("<textarea name=\"talkingtext\" cols=\"100\" rows=\"10\"> </textarea>");
out.println("</tr></td> </table>");
out.println("<br>");
out.println("<table width=\"10%\" border=\"0\" align=\"center\">"); out.println("<tr><td>");
out.println("<input type=\"submit\" align=\"right\" name=\"btn_submit\" value=\"发送\">");
out.println("<input type=\"reset\" name=\"btn_reset\" value=\"清除\">"); out.println("</tr></td> </table></form>");
out.println("</body>");
out.println("</html>");
out.close();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
4、登录失败:Loginfail
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author lenovo
*/
public class Loginfail extends HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here. You may use following sample code. */ out.println("<html>");
out.println("<head>");
out.println("<title>登录失败</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>登录失败,请重新登陆</h1>");
RequestDispatcher dispatcher=request.getRequestDispatcher("login.html"); dispatcher.include(request, response);
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
5、聊天内容的实现:Content.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author lenovo
*/
;
public class Content extends HttpServlet {
static String content;
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("gb2312");
PrintWriter out = response.getWriter();
try {
String talkingtime =time.getTimeShort();
content += (request.getSession().getAttribute("userID") +" "+talkingtime + "\r\n");
content+=(request.getParameter("talkingtext")+ "\r\n");
RequestDispatcher dispatcher=request.getRequestDispatcher("webchat"); dispatcher.forward(request,response);
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
6、时间返回功能:time.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.servlet;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author lenovo
*/
public class time {
public static String getTimeShort(){
SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date contentTime=new Date();
String dateString =timeformat.format(contentTime);
return dateString;
}
}
7、会话监听:SessionListener.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.servlet;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* Web application lifecycle listener.
*
* @author lenovo
*/
@WebListener()
public class SessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
String current=(String)se.getSession().getServletContext().getAttribute("online"); if(current==null) {
current="0";
}
int c=Integer.parseInt(current);
c++;
current=String.valueOf(c);
se.getSession().getServletContext().setAttribute("online", current);
String his=(String)se.getSession().getServletContext().getAttribute("Counter");
if(his==null) {
his="0";
}
int total=Integer.parseInt(his)+1;
his=String.valueOf(total);
se.getSession().getServletContext().setAttribute("Counter", his);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
String current=(String)se.getSession().getServletContext().getAttribute("online"); if(current==null) {
current="0";
}
int c=Integer.parseInt(current);
c--;
current=String.valueOf(c);
se.getSession().getServletContext().setAttribute("online", current);
}
}
8、 退出登入:Abort.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
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;
/**
*
* @author lenovo
*/
public class Abort extends HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.getSession().invalidate();
PrintWriter out = response.getWriter();
try {
/* TODO output your page here. You may use following sample code. */ out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Abort</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1><center>退出成功...</center></h1>");
out.println("<a href=\"login.html\"><center>重新登陆</center></a>"); out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
源代码如下:
1、 登录界面 login.jsp
<%--
Document : index
Created on : 2012-12-21, 10:15:38
Author : lenovo
--%>
<%@page contentType="text/html" pageEncoding="gbk"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk"> <title>登陆</title>
</head>
<body bgcolor="#FFFFFF">
<center><h1>欢迎登陆系统</h1> </center>
<form name="login" method="post" action="logcheck.jsp">
<div align="center">
<label>
账号:
<input type=text name="userID" value=""/>
</label>
</div>
<div align="center">
<label>
密码:
<input type=password name="password" value="" />
</label>
</div>
<div align="center">
<input type="submit" name="login" value="提交" />
<input type="reset" name="reset" value="重置" />
</div>
</form>
</body>
</html>
2、 连接数据库,判断登录 logcheck.jsp
<%--
Document : Logcheck
Created on : 2012-12-23, 13:18:58
Author : lenovo
--%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="gbk"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk"> <title>JSP Page</title>
</head>
<body>
<% String promt=new String();
String Name=request.getParameter("userID");
String Password=request.getParameter("password");
java.sql.Connection conn=null;
java.sql.ResultSet sqlRst;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=
java.sql.DriverManager.getConnection("jdbc:mysql://localhost/user","root","root"); java.sql.Statement stmt=conn.createStatement();
if((Name!=null&&Name.length()!=0)&&(Password!=null&&Password.length()!=0)) {
String sqlstr="select* from username where username='"+Name+"'and password='"+Password+"'";
sqlRst=stmt.executeQuery(sqlstr);
if(sqlRst.next())
{
response.sendRedirect("loginsuccess.jsp");
}
else{
out.println("密码错误");
response.sendRedirect("login.jsp");
}
}
else
{
System.out.println("用户密码不能为空");
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
3、 购物界面 loginsuccess.jsp
<%--
Document : LoginSuccess
Created on : 2012-12-23, 15:12:30
Author : lenovo
--%>
<%@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>
<table border="0" width="100%">
<tr>
<td align="center" valign="bottom">
<h1>欢迎来到水果店购物</h1></td></tr>
</table><hr>
<form action="output.jsp" method="post">
<table align="center" CELLSPACING="5" CELLPADDING="5"> <tr>
<td ><B>种类</B></td>
<td ><B>单价</B></td>
<td ><B>数量</B></td>
</tr>
<tr>
<td >苹果</td>
<td >4.0</td>
<td >
<input name="apple_amount"/></td>
</tr>
<tr>
<TD ALIGN="center">香蕉</td>
<td >2.5</td>
<td >
<input name="banana_amount"/></td>
</tr>
<tr>
<td >葡萄</td>
<td >3.6</td>
<td >
<input name="grape_amount"/></td>
</tr>
</table><hr>
<p align="center">
<input type="submit" name="submit" value="放入购物车">
</p>
</form>
</body>
</html>
4、 结账输出 output.jsp
<%--
Document : output
Created on : 2012-12-23, 15:25:02
Author : lenovo
--%>
<%@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>
<%request.setCharacterEncoding("gb2312");%>
<center>
<jsp:useBean id="add1" scope="page" class="com.jsp.add"/>
<jsp:setProperty name="add1" property="appleSum" param="apple_amount"/> <jsp:setProperty name="add1" property="bananaSum" param="banana_amount"/>
<jsp:setProperty name="add1" property="grapeSum" param="grape_amount"/> <h2>您的购物信息:</h2>
<table>
<tr>
<td>名称<td>
<td>单价<td>
<td>数量<td>
</tr>
<tr>
<br>
<td>苹果<td>
<td><jsp:getProperty name="add1" property="applePrice"/><td> <td><jsp:getProperty name="add1" property="appleSum"/><td> </tr>
<tr>
<br>
<td>香蕉<td>
<td><jsp:getProperty name="add1" property="bananaPrice"/><td> <td><jsp:getProperty name="add1" property="bananaSum"/><td> </tr>
<tr>
<br>
<td>葡萄<td>
<td><jsp:getProperty name="add1" property="grapePrice"/><td> <td><jsp:getProperty name="add1" property="grapeSum"/><td> </tr>
<tr>
<br>
<td>总额<td>
<td><jsp:getProperty name="add1" property="total" property="total"/><td>
<td></td>
</tr>
<tr> <td></td>
<td>
<form name="shop" method="post" action="loginsuccess.jsp"> <input type="submit" value="继续购物"/>
</form>
</td>
</tr>
</table>
</center>
</body>
</html>
5、javaBean辅助计算总价 add.java
/*
* To change this template, choose Tools | Templates * and open the template in the editor.
*/
package com.jsp;
/**
*
* @author lenovo
*/
public class add {
private float applePrice, appleSum;
private float bananaPrice, bananaSum; private float grapePrice, grapeSum;
private float total;
public add() {
applePrice = (float) 4.0;
appleSum = 0;
bananaPrice = (float) 2.5;
bananaSum = 0;
grapePrice = (float) 3.6;
grapeSum = 0;
total = 0;
}
/**
* @return the applePrice
*/
public float getApplePrice() {
return applePrice;
}
/**
* @param applePrice the applePrice to set */
public void setApplePrice(float applePrice) { this.applePrice = applePrice;
}
/**
* @return the appleSum
*/
public float getAppleSum() {
return appleSum;
}
/**
* @param appleSum the appleSum to set */
public void setAppleSum(float appleSum) { this.appleSum = appleSum;
}
/**
* @return the bananaPrice
*/
public float getBananaPrice() {
return bananaPrice;
}
/**
* @param bananaPrice the bananaPrice to set */
public void setBananaPrice(float bananaPrice) { this.bananaPrice = bananaPrice;
}
/**
* @return the bananaSum
*/
public float getBananaSum() {
return bananaSum;
}
/**
* @param bananaSum the bananaSum to set */
public void setBananaSum(float bananaSum) { this.bananaSum = bananaSum;
}
/**
* @return the grapePrice
*/
public float getGrapePrice() {
return grapePrice;
}
/**
* @param grapePrice the grapePrice to set
*/
public void setGrapePrice(float grapePrice) {
this.grapePrice = grapePrice;
}
/**
* @return the melonSum
*/
public float getGrapeSum() {
return grapeSum;
}
/**
* @param melonSum the melonSum to set
*/
public void setgrapeSum(float grapeSum) {
this.grapeSum = grapeSum;
}
/**
* @return the total
*/
public float getTotal() {
setTotal(applePrice * appleSum + bananaPrice * bananaSum + grapePrice * grapeSum);
return total;
}
/**
* @param total the total to set
*/
public void setTotal(float total) {
this.total = total;
}
}