| jsp留言本示例 |
|
来源:互联网 |
时间:2005-09-01 |
浏览:
相关评论 |
报告错误 |
发布文章
|
【字号:大 | 中 | 小】
【背景色
】
|
|
<form name="form1" method="post" action="servlet_reg"> <table width="352" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="129">姓名:</td> <td width="223"><input name="tname" type="text" id="tname" maxlength="10"></td> </tr> <tr> <td>密码:</td> <td><input name="tpass1" type="password" id="tpass1" maxlength="10"></td> </tr> <tr> <td>确认密码:</td> <td><input name="tpass2" type="password" id="tpass2" maxlength="10"></td> </tr> <tr> <td height="14">性别:</td> <td>男 <input name="请不要发表含有不雅言语的内容" type="radio" value="男" checked> 女 <input type="radio" name="请不要发表含有不雅言语的内容" value="女"> <a href="userhead.html">查看头像</a></td> </tr> <tr> <td>头像选择:</td> <td><select name="ttouxiang" id="ttouxiang"> <option>1.gif</option> <option>2.gif</option> <option>3.gif</option> <option>4.gif</option> <option>5.gif</option> <option>5.gif</option> <option>6.gif</option> <option>7.gif</option> <option>8.gif</option> <option>9.gif</option> <option>10.gif</option> <option>11.gif</option> <option>12.gif</option> <option>13.gif</option> <option>14.gif</option> <option>15.gif</option> <option>16.gif</option> <option>17.gif</option> <option>18.gif</option> <option>19.gif</option> <option>20.gif</option> </select></td> </tr> <tr> <td>Email:</td> <td><input name="tmail" type="text" id="tmail" maxlength="50"></td> </tr> <tr> <td>Oicq:</td> <td><input name="toicq" type="text" id="toicq" maxlength="9"></td> </tr> <tr> <td><div align="center"> <input type="button" name="Submit" value="提交" onClick="chick();"> </div></td> <td><input type="reset" name="Submit" value="重置"></td> </tr> </table>
</form>
</td> </tr> <tr> <td> </td> </tr> </table> </form> <jsp:include page="foot.jsp"/> </body>
<script language="javascript"> function chick() { if(document.form1.tname.value=="") { alert("姓名不可为空!!"); document.form1.tname.focus(); } else if(document.form1.tpass1.value=="") { alert("密码不可为空!!"); document.form1.tpass1.focus(); } else if(document.form1.tpass2.value=="") { alert("密码确认不可为空!!"); document.form1.tpass2.focus(); } else if(document.form1.tmail.value=="") { alert("Email不可为空!!"); document.form1.tmail.focus(); } else if(document.form1.toicq.value=="") { alert("Oicq不可为空!!"); focument.form1.toicq.focus(); } else if(!document.form1.tpass1.value.equals(document.form1.tpass2.value)) { alert("请确认两次密码必须输入一致!!"); document.form1.tpass1.focus(); } else { document.form1.submit(); } }
</script> </html>
show.jsp>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <%@ page contentType="text/html; charset=GBK" %> <%@ page import ="java.sql.* "%>
<% //定义一个一页显示多少条数据 int intPageSize; //定义一个总记录数 int intRowCount; //定义一个总页数 int intPageCount; //准备显示的页 int intPage; //取得页书号的变量 String strPage;
//Data连接数据库类 connection.Data data=new connection.Data();
//connection连接数据哭类 Connection conn; //Statement操作数据库类 Statement stme; Statement stme2; //ResultSet操作记录机类 ResultSet rslyxx; ResultSet rsuserinfo; //每页显示的记录数 intPageSize=8;
//取得页面号 if(request.getParameter("page")==null) { intPage=1; } else { strPage=(String)request.getParameter("page"); intPage=Integer.parseInt(strPage); if(intPage<1) { intPage=1; } } intPageCount=0;
conn=data.getConn(); stme=conn.createStatement(); rslyxx=stme.executeQuery("select * from lyxx"); rslyxx.last(); //获得总记录数 intRowCount=rslyxx.getRow(); //获得总页数 intPageCount=(intRowCount+intPageSize-1)/intPageSize; //如果每页显示的记录数大约总记录数 if(intPageSize>intRowCount) { intPageSize=intRowCount; } if(intPage>intPageCount) { intPage=intPageCount; }
%> <html> <head> <title> show </title> </head> <body> <p> <jsp:include page="head.jsp"/> </p> <table width="600" align="center" cellspacing="0"> <tr> <th scope="col"> <%--显示表格或者是汉字提示有无记录--%>
<% if(intRowCount<1) {
%> <table width="600" cellspacing="0"> <tr> <th scope="col">暂时还没有人在此留言</th> </tr> </table>
</th> </tr> <tr> <td> <% } else { //将指针定位到每页最后一位上 rslyxx.absolute(intRowCount-(intPage-1)*intPageSize); //将指针定位到每页第一位上 // rslyxx.absolute((intPage-1) * intPageSize + 1); //显示数据
int i = 0;
while(i<intPageSize && !rslyxx.isBeforeFirst()) { stme2=conn.createStatement(); rsuserinfo=stme2.executeQuery("select * from userinfo where username='"+rslyxx.getString("lyuser")+"'"); rsuserinfo.next(); %>
<table width="713" border="0" cellspacing="0"> <tr> <th width="146" scope="col"><%=rslyxx.getString("lyuser")%></th> <th colspan="4" scope="col"> <table width="600"> <tr> <th width="513" scope="col"><%=rslyxx.getString("lybt")%></th> <th width="75" scope="col"> <% if(session.getAttribute("loginname")!=null) { String tempname=(String)session.getAttribute("loginname"); tempname.trim(); String dataname=(String)rsuserinfo.getString("username"); if(tempname.equals(dataname.trim())) { %> <a href="editnr.jsp?id=<%=rsuserinfo.getString("id")%>">编辑</a> <% } else { if(session.getAttribute("adminname")!=null) { %> <a href="editnr.jsp?id=<%=rsuserinfo.getString("id")%>">编辑</a> <% } } } %> </th> </tr> </table></th> </tr> <tr> <td align="center" valign="top"><img src="pic/<%=rsuserinfo.getString("userhead").trim()%>" width="100" height="100"></td> <td colspan="4"><%=rslyxx.getString("lynr")%></td> </tr> <tr> <td>注册时间:<%=rsuserinfo.getString("userdate")%></td> <td width="111">性别:</td> <td width="111">Oicq:<%=rsuserinfo.getString("useroicq")%></td> <td width="150">Email:<%=rsuserinfo.getString("usermail")%></td> <td width="185">发帖时间:<%=rslyxx.getString("lydate")%></td> </tr> </table> <% rsuserinfo.close(); stme2.close(); rslyxx.previous(); i++; } } stme.close(); conn.close();
%>
</td> </tr> <tr> <td><table width="250" border="0" align="right" cellspacing="0"> <tr> <th width="44" scope="col">第<%=intPage%>页</th> <th width="44" scope="col">共<%=intPageCount%>页</th> <th width="55" scope="col"><a href="show.jsp?page=<%=intPage-1%>">上一页</a></th> <th width="49" scope="col"><a href="show.jsp?page=<%=intPage+1%>">下一页</a></th> </tr> </table></td> </tr> </table>
<p> <jsp:include page="foot.jsp"/> </p> </body> </html>
userhead.html>>>>>>>>>>>>>>>>> <!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=gb2312"> <title>无标题文档</title> </head>
<body> <table width="600" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td><div align="center"><img src="pic/1.gif" width="100" height="100"></div></td> <td><div align="center"><img src="pic/2.gif" width="100" height="100"></div></td> <td><div align="center"><img src="pic/3.gif" width="100" height="100"></div></td> <td><div align="center"><img src="pic/4.gif" width="100" height="100"></div></td> <td><div align="center"><img src="pic/5.gif" width="100" height="100"></div></td> </tr> <tr> <td><div align="center">1</div></td> <td><div align="center">2</div></td> <td><div align="center">3</div></td> <td><div align="center">4</div></td> <td><div align="center">5</div></td> </tr> <tr> <td><div align="center"><img src="pic/6.gif" width="100" height="100"></div></td> <td><div align="center"><img src="pic/7.gif" width="100" height="100"></div></td> <td><div align="center"><img src="pic/8.gif" width="100" height="100"></div></td> <td><div align="center"><img src="pic/9.gif" width="100" height="100"></div></td> <td><div align="center"><img src="pic/10.gif" width="100" height="100"></div></td> </tr> <tr> <td><div align="center">6</div></td> <td><div align="center">7</div></td> <td><div align="center">8</div></td> <td><div align="center">9</div></td> <td><div align="center">10</div></td> </tr> <tr> <td><div align="center"><img src="pic/11.gif" width="100" height="100"></div></td> <td><div align="center"><img src="pic/12.gif" width="100" height="100"></div></td> <td><div align="center"><img src="pic/13.gif" width="100" height="100"></div></td> <td><div align="center"><img src="pic/14.gif" width="100" height="100"></div></td> <td><div align="center"><img src="pic/15.gif" width="100" height="100"></div></td> </tr> <tr> <td><div align="center">11</div></td> <td><div align="center">12</div></td> <td><div align="center">13</div></td> <td><div align="center">14</div></td> <td><div align="center">15</div></td> </tr> <tr> <td><div align="center"><img src="pic/16.gif" width="100" height="100"></div></td> <td><div align="center"><img src="pic/17.gif" width="100" height="100"></div></td> <td><div align="center"><img src="pic/18.gif" width="100" height="100"></div></td> <td><div align="center"><img src="pic/19.gif" width="100" height="100"></div></td> <td><div align="center"><img src="pic/20.gif" width="100" height="100"></div></td> </tr> <tr> <td><div align="center">16</div></td> <td><div align="center">17</div></td> <td><div align="center">18</div></td> <td><div align="center">19</div></td> <td><div align="center">20</div></td> </tr> <tr> <td colspan="5"><div align="center"></div> <div align="right"><a href="javascript:history.back();">返回</a></div> <div align="center"></div> <div align="center"></div> <div align="center"></div> <div align="center"></div> <div align="center"></div> <div align="center"></div> <div align="center"></div> <div align="right"></div></td> </tr> </table> </body> </html>
Data.java>>>>>>>>>>>>>>>>>> package connection; import java.sql.*;
public class Data { Connection conn; public Connection getConn() { try { Class.forName("org.gjt.mm.mysql.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/txl?useUnicode=true&characterEncoding=gb2312","root","qlshzbk"); return conn; } catch(Exception ex) { ex.printStackTrace(); return null; } } }
master.java>>>>>>>>>>>>>>>>>>>>> package tools;
public class master { //网站首页 String wangzhanshouye=""; //网站简介 String wangzhanjianjie=""; //联系我们 String lianxiwomen=""; //关于我们 String guanyuwomen=""; //友情连接 String youqinglianjie="";
public String getWangzhanshouye() { return wangzhanshouye; } public String getWangzhanjianjie() { return wangzhanjianjie; } public String getLianxiwomen() { return lianxiwomen; } public String getGuanyuwomen() { return guanyuwomen; } public String getYouqinglianjie() { return youqinglianjie; }
}
Servlet_edit.java
package tools;
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.sql.*; import connection.Data; import java.text.*; import java.util.*;
public class Servlet_edit extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=GBK"; Data data=new Data(); Connection conn; Statement stme; ResultSet rs; String sname="",spass="",请不要发表含有不雅言语的内容="",stouxiang="",smail="",soicq=""; //Initialize global variables public void init() throws ServletException { } //Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("gb2312"); response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); spass=request.getParameter("tpass1"); 请不要发表含有不雅言语的内容=request.getParameter("请不要发表含有不雅言语的内容"); stouxiang=request.getParameter("ttouxiang"); smail=request.getParameter("tmail"); soicq=request.getParameter("toicq"); ServletContext sc=this.getServletContext(); HttpSession session=request.getSession(); try { conn=data.getConn(); stme=conn.createStatement(); stme.executeUpdate("update userinfo set userpass='"+spass+"',请不要发表含有不雅言语的内容='"+请不要发表含有不雅言语的内容+"',usermail='"+smail+"',useroicq='"+soicq+"',userhead='"+stouxiang+"' where username='"+session.getAttribute("loginname")+"'" ); closeConn(); session.setAttribute("chenggong","2"); sc.getRequestDispatcher("/chenggong.jsp").forward(request,response); } catch(Exception ex) { ex.printStackTrace(); closeConn(); sc.getRequestDispatcher("/edit.jsp").forward(request,response); } } public void closeConn() { try { conn.close(); stme.close(); } catch (SQLException e) { e.printStackTrace(); } } //Clean up resources public void destroy() { } } Servlet_login.java>>>>>>>>>>>>>>>>>>>>>> package tools;
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.sql.*; import connection.Data; import java.sql.SQLException;
public class Servlet_login extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=gb2312"; Data data=new Data(); Connection conn; Statement stme; Statement stme2; ResultSet rs; ResultSet rs2; String sname; String spass;
//Initialize global variables public void init() throws ServletException { } //Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ request.setCharacterEncoding("gb2312"); response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); ServletContext sc=this.getServletContext(); HttpSession session=request.getSession(); sname=request.getParameter("tname"); spass=request.getParameter("tpass"); try { conn=data.getConn(); stme=conn.createStatement(); rs=stme.executeQuery("select * from userinfo where username='"+sname.trim()+"' and userpass= '"+spass.trim()+"' and userflag=1"); if(rs.next()) { session.setAttribute("loginname",sname); closeConn(); sc.getRequestDispatcher("/index.jsp").forward(request,response); } else { stme2=conn.createStatement(); rs2=stme2.executeQuery("select * from userinfo where username='"+sname.trim()+"' and userpass= '"+spass.trim()+"' and userflag=3"); if(rs2.next()) { session.setAttribute("loginname",sname); session.setAttribute("adminname","3"); stme2.close(); sc.getRequestDispatcher("/index.jsp").forward(request,response); } else { session.setAttribute("error" ,"2"); closeConn(); sc.getRequestDispatcher("/error.jsp").forward(request , response); } } } catch(Exception ex) { ex.printStackTrace(); closeConn(); sc.getRequestDispatcher("/index.jsp").forward(request,response); } } public void closeConn() { try { conn.close(); stme.close(); } catch(SQLException e) { e.printStackTrace(); }
} //Clean up resources public void destroy() { } }
Servlet_post.java
package tools;
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.util.*; import java.text.*; import java.sql.*; import connection.*;
public class Servlet_post extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=gb2312"; Connection conn; Statement stme; ResultSet rs; Data data=new Data(); String snr=""; String sbt=""; //Initialize global variables public void init() throws ServletException { } //Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); request.setCharacterEncoding("gb2312"); PrintWriter out = response.getWriter(); java.util.Date newDate=new java.util.Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss"); String date=sdf.format(newDate); sbt=request.getParameter("tbt"); snr=request.getParameter("tnr");
//取得session对象 HttpSession session=request.getSession(); //取得servletContext对象 ServletContext sc=this.getServletContext(); if(session.getAttribute("loginname")!=null) { //没有输入用户名,提前登陆了 try { String tempname = (String)session.getAttribute("loginname"); conn=data.getConn(); stme = conn.createStatement(); stme.executeUpdate( "insert into lyxx(lyuser,lybt,lynr,lydate) values ('" + tempname + "','" + sbt + "','" + snr + "','" + date + "') "); closeConn(); sc.getRequestDispatcher("/show.jsp").forward(request,response); } catch(Exception ex) { ex.printStackTrace(); closeConn(); } } else { //以前没有登陆,输入用户名登陆的 String sname=request.getParameter("tname"); String spass=request.getParameter("tpass"); try { conn=data.getConn(); stme = conn.createStatement(); rs = stme.executeQuery( "select * from userinfo where username='" + sname + "'and userpass='" + spass + "'"); if(rs.next()) { //可以找到用户,添加留言信息 session.setAttribute("loginname",sname); stme.executeUpdate("insert into lyxx(lyuser,lybt,lynr,lydate) values ('" + sname + "','" + sbt + "','" + snr + "','" + date + "') "); closeConn(); sc.getRequestDispatcher("/show.jsp").forward(request,response); } else { session.setAttribute("error","3"); closeConn(); sc.getRequestDispatcher("/error.jsp").forward(request,response); } } catch(Exception ex) { closeConn(); ex.printStackTrace(); }
} } public void closeConn() { try { conn.close(); stme.close(); } catch (SQLException e) { e.printStackTrace(); } } //Clean up resources public void destroy() { } }
Servlet_reg.java>>>>>>>>>>>>>> package tools;
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.sql.*; import connection.Data; import java.text.*; import java.util.*;
public class Servlet_reg extends HttpServlet { Data data=new Data(); Connection conn; Statement stme; ResultSet rs; String sname="",spass="",请不要发表含有不雅言语的内容="",stouxiang="",smail="",soicq=""; private static final String CONTENT_TYPE = "text/html; charset=GBK"; //Initialize global variables public void init() throws ServletException { } //Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("gb2312"); response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); sname=request.getParameter("tname"); spass=request.getParameter("tpass1"); 请不要发表含有不雅言语的内容=request.getParameter("请不要发表含有不雅言语的内容"); stouxiang=request.getParameter("ttouxiang"); smail=request.getParameter("tmail"); soicq=request.getParameter("toicq"); ServletContext sc=this.getServletContext(); HttpSession session=request.getSession(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); java.util.Date newDate=new java.util.Date(); String date_s=sdf.format(newDate); try { conn=data.getConn(); stme=conn.createStatement(); rs=stme.executeQuery("select * from userinfo where username='"+sname+"'"); if(rs.next()) { session.setAttribute("error","1"); closeConn(); sc.getRequestDispatcher("/error.jsp").forward(request,response); } else { stme.executeUpdate("insert into userinfo(username,userpass,请不要发表含有不雅言语的内容,usermail,useroicq,userdate,userhead) values ('"+sname+"','"+spass+"','"+请不要发表含有不雅言语的内容+"','"+smail+"','"+soicq+"','"+date_s+"','"+stouxiang+"')"); session.setAttribute("loginname",sname); closeConn(); session.setAttribute("chenggong","1"); sc.getRequestDispatcher("/chenggong.jsp").forward(request,response); } } catch(Exception ex) { ex.printStackTrace(); closeConn(); sc.getRequestDispatcher("/reg.jsp").forward(request,response); } } public void closeConn() { try { conn.close(); stme.close(); } catch (SQLException e) { e.printStackTrace(); } } //Clean up resources public void destroy() { } }
Data.java是在包connection里建的,其余的java文件是在tools包里的 大家用的时候别弄错了
|
| 分页 [1] [2] [3] [4] [5] |
|
 |
| |
|
|
|
|