GuestBook Test

类别:Java 点击:0 评论:0 推荐:

这是我的第一个留言板测试例子,包括登陆界面,留言板主页,添加留言等页面:

1.页面
F:\Tomcat 5.0\webapps\ROOT\login
log.html
log_cm.jsp
success.jsp
relog.jsp
index.jsp
guestbook.jsp

2.Bean
F:\Tomcat 5.0\webapps\ROOT\WEB-INF\classes\login
LogBean.java

3.数据库
F:\Tomcat 5.0\webapps\ROOT\BUSINESS(BUSINESS 是可以自定义的)
firm.mdb——两个表:
user(name,password,email);
guest(未完成,用作保存留言.....)

4.表情
F:\Tomcat 5.0\webapps\ROOT\images
hello.gif
...


1。log.html

<html>
<head>
<title>我的第一个HTML登陆界面</title>

<meta http-equiv = "Content-Type" content = "text/html; charset = gb2312" >
<style type = "text/css">

body { font-family:宋体; font-size:9pt}
   th { font-size: 9pt }

</style>
</head>

<body>

<center>
<table border = "0" cellspacing = "0" width = "332" bgcolor = "#F0F8FF"
  bordercolorlight = "#4DA6FF" bordercolordark = "#ECF5FF">

<tr><td colspan=2 align='center'><h4>请你登陆:</h4></td></tr>

<form method = post action = "log_cm.jsp">
<tr>
<td align='left'>用户名:</td>
<td><input type="text" name="username"></td>
</tr>

<tr>
<td align='left'>密&nbsp;&nbsp;码:</td>
<td><input type="password" name="password"></td>
</tr>

<tr>
<td align='left'>邮&nbsp;&nbsp;箱:</td>
<td><input type="text" name="email"></td>
</tr>

<tr>
<td colspan=2 align='center'>
<input type="submit" size="4" value="确定">
<input type="reset" size="4" value="重置">
</td>
</tr>

</form>
</table>

</center>
</body>
</html>


2。log_cm.jsp

<%--  class="login.LogBean" 指代\ROOT\WEB-INF\classes\login 目录下的LogBean 类 --%>
<jsp:useBean id="handle" class="login.LogBean" scope="request">
   <jsp:setProperty name="handle" property="*"/>
</jsp:useBean>

<html>
<head><title>验证界面</title></head>

<body>
<%-- session.putValue("loginsign", "ok"); 用于检验用户是否已经登陆 --%>
<%
    session.putValue("loginsign", "ok");
    if(handle.validate()){
%>
<jsp:forward page="success.jsp"/>
<% 
 }
 if(!handle.validate()){
%>
<jsp:forward page="relog.jsp"/>
<%
 session.putValue("loginsign", "false");
 }
%>
</body>
</html>


3。success.jsp

<%--  class="login.LogBean" 指代\ROOT\WEB-INF\classes\login 目录下的LogBean 类 --%>
<jsp:useBean id="handle" class="login.LogBean" scope="request"/>
<jsp:useBean id="addBean"  class="dbtest.Accessdb" scope="page"/>

<html>
<head>
<title>注册成功</title>

<meta http-equiv = "Content-Type" content = "text/html; charset = gb2312" >
<style type = "text/css">

body { font-family:宋体; font-size:9pt}
   th { font-size: 9pt }
   td { font-size: 9pt }

</style>
</head>

<body>

<center>
<table border = "0" cellspacing = "0" width = "332" bgcolor = "#F0F8FF"
  bordercolorlight = "#4DA6FF" bordercolordark = "#ECF5FF">

<tr><td colspan=2 align='center'><h4>祝贺你注册成功!</h4></td></tr>

<tr>
<td align='left'>用户名:</td>
<td><jsp:getProperty name="handle" property="username"/></td>
</tr>

<tr>
<td align='left'>密&nbsp;&nbsp;码:</td>
<td><jsp:getProperty name="handle" property="password"/></td>
</tr>

<tr>
<td align='left'>邮&nbsp;&nbsp;箱:</td>
<td><jsp:getProperty name="handle" property="email"/></td>
</tr>

</table>


<%
//把"8859_1"码转换为"gb2312"码
String name = new String(handle.getUsername().getBytes("8859_1"));
String password = new String(handle.getPassword().getBytes("8859_1"));
String email = new String(handle.getEmail().getBytes("8859_1"));
String strSQL = " insert into user(name, password, email) values(' "
                      + name + " ',' " + password + " ',' " + email + " ') "; //把记录写入数据库
addBean.executeUpdate(strSQL); 
out.print("你的留言已经保存到数据库中,谢谢!");
%>

</center>
</body>
</html>


4。relog.jsp

<jsp:useBean id="handle" class="login.LogBean" scope="request"/>

<html>
<head>
<title>重新登陆</title>

<meta http-equiv = "Content-Type" content = "text/html; charset = gb2312" >
<style type = "text/css">

body { font-family:宋体; font-size:9pt}
   th { font-size: 9pt }

</style>
</head>

<body>
<form method = post action = "log_cm.jsp">
<table border = "0" cellspacing = "0" width = "332" bgcolor = "#F0F8FF"
  bordercolorlight = "#4DA6FF" bordercolordark = "#ECF5FF">

<tr><td colspan=2 align='center'><h4>请重新登陆:</h4></td></tr>

<tr>
<td align='left'>用户名:</td>
<td><input type="text" name="username"
value="<jsp:getProperty name="handle" property="username"/>">
</td>
</tr>

<tr>
<td align='left'>密&nbsp;&nbsp;码:</td>
<td><input type="password" name="password"
value="<jsp:getProperty name="handle" property="password"/>">
<%=handle.getErrorMsg("password")%>
</td>
</tr>

<tr>
<td align='left'>邮&nbsp;&nbsp;箱:</td>
<td><input type="text" name="email"
value="<jsp:getProperty name="handle" property="email"/>">
<span class="error"><%=handle.getErrorMsg("email")%></span>
</td>
</tr>

<tr>
<td colspan=2 align='center'>
<input type="submit" size="4" value="确定">
<input type="reset" size="4" value="重置">
</td>
</tr>

</table>
</form>

</body>
</html>


5。index.jsp

<%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="java.sql.*" %>
<jsp:useBean id="showBean" scope="page" class="dbtest.Accessdb" />

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>我的第一个留言板主页</title>

<link rel=stylesheet href="../style.css" type="text/css">
</head>

<body>
<script language="javascript">
function submit(){
 self.location.replace("index.jsp")
}
</script>

<center><p>留言板</p>
<table>
<tr>
<td>
<p align='left'>&gt;&gt;分页&nbsp;
<a href='guestbook.jsp'>我要留言</a>&nbsp;
</td>
</tr>
</table>


<%
//指定行数
int pageLine = 10;
int totalRec = 0;
int totalSub = 0;
int intPage = 1;
int i;

try{
 ResultSet rs = null;
 //取得总记录数
 rs = showBean.executeQuery("select count(*) as cnt from guestbook");
 if( rs.next() )
  totalRec = rs.getInt("cnt");
 rs.close();
 showBean.closeStat();
}catch(Exception e){ e.printStackTrace(); }

//取得总页数
int intPageCount = 0;
intPageCount = (totalRec + pageLine - 1) / pageLine ;
 
ResultSet RS = showBean.executeQuery("select * from guestbook order by serial_no desc");
String emote1;
String name1;
Date gtime1;
String content1;
%>

<table border='1' cellspacing='0' width="80%" bgcolor='#d7e3b9' bordercolorlight='#green' bordercolordark='#ecf5ff'>

<tr align='center'>
<td>表情</td>
<td>留言人</td>
<td>留言时间</td>
<td>留言内容</td>
</tr>

<%
if(intPageCount>0){
 for(i=1; i<(intPage-1)*pageLine; i++)
  RS.next();
 for(i=1; i<=pageLine; i++) {
  if(RS.next()){
      emote1 = RS.getString("emote");
   name1 = RS.getString("name");
   gtime1 = RS.getDate("guest_time");
   content1 = RS.getString("content");
%>

<tr>
<td  align='center'><img src="../images/<%=emote1%>.gif" ></td>
<td  align='center'><%=name1%></td>
<td  align='center'><%=gtime1%></td>
<td  align='center'><%=content1%></td>
</tr>

<%
  }//if(RS.next())
 }//for(i=1; i<=pageLine; i++)
RS.close();
}//if(intPageCount>0)
%>
<!--以下用于留言分页显示-->

</table>
</center>

</body>
</html>


6。guestbook.jsp

<html>
<head>
<title>我的第一个GUESTBOOK</title>
<link rel = stylesheet href="../style.css" type="text/css">
<script language="JavaScript">
function valiform() {
 if(document.form1.content.value=""){
  alert("留言内容不能为空!");
  documnet.form1.content.focus();
  return false;
 }
}
</script>
</head>
<body>
<form method=post action="ok_guestbook.jsp" name=form1 onSubmit="return valiform()">

<center><h4></font color="blue">留言</font></h4></p>
<table width='60%' height='120'>

<%-- 取得用户登陆时的session 变量值,并存放在隐藏域中 --%>
<%String name1 = (String)session.getValue("username");%>
<input type="hidden" name="name" value=<%=name1%>>
<tr>
<td>留言人:<%=name1%></td>
</tr>

<tr>
<td>表&nbsp;&nbsp;情:
<select name="emote" size="1">
<option style="color: #000000" value="p1" selected>大家好</option>
<option style="color: #000000" value="p2" >赞成</option>
<option style="color: #000000" value="p3" >反对</option>
<option style="color: #000000" value="p4" >不太明白</option>
</select>
</td>
</tr>

<tr>
<td>留&nbsp;&nbsp;言:<br>
<textarea name="content" cols="50" rows="10" maxlength="80"></textarea>
</td>
</tr>

<tr>
<td align="center">
<input type="submit" size="4" value="提交" class="buttonface">&nbsp;&nbsp;
<input type="reset" size="4" value="重写" class="buttonface">
</td>
</tr>

</table>
</center>
</form>
</body>
</html>


7。LogBean.java

package login;

import java.util.*;

public class LogBean
{
  private String username;
  private String password;
  private String email;
  private Hashtable errors;
 
  public LogBean(){
    username = "";
    password = "";
    email = "";
    errors = new Hashtable();
  }
 
  public String getUsername(){
    return username;
  }
 
  public String getPassword(){
    return password;
  }
 
  public String getEmail(){
    return email;
  }
 
  public void setUsername(String usernameStr){
    username = usernameStr;
  }
 
  public void setPassword(String passwordStr){
    password = passwordStr;
  }

  public void setEmail(String emailStr){
    email = emailStr;
  }
 
  public void setErrors(String Key,String msg){
    errors.put(Key,msg);
  }
 
  public boolean validate(){
    boolean allOk = true;
    if(username.equals("")){
      errors.put("username","Plz enter your name");
      username = "";
      allOk = false;
    }
    if( (password.equals(""))||(password.length()>10)||(password.length()<6) ){
      errors.put("password","Plz enter a password of 6-10 charactors.");
      allOk = false;
    }
    if( (email.equals(""))||(email.indexOf('@')==-1)||(email.indexOf('.')==-1) ){
      errors.put("email","Plz enter a valid Email");
      allOk = false;
    }
   
    return allOk;
   
  }
 
  public String getErrorMsg(String s){
    String errorMsg = (String) errors.get(s.trim());
    return (errorMsg==null)?"":errorMsg;
  }
 
}

本文地址:http://com.8s8s.com/it/it11444.htm