国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

川石用JAVA寫的留言板原代碼

2019-11-18 15:04:55
字體:
來源:轉載
供稿:網友

  /*
* guestbookServlet.java
*
* Created on
*/

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.Date;
/**
*
* @author 川石
* 請尊重作者的勞動
* @version 1.0
*/
public class guestbookServlet extends HttpServlet {

boolean debug=false;
String sDBDriver;
Connection conn=null;
ResultSet rs=null;
/** Initializes the servlet.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
if(debug)
sDBDriver=new String("sun.jdbc.odbc.JdbcOdbcDriver");
else
sDBDriver=new String("org.gjt.mm.MySQL.Driver");
try{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e){

System.err.}

}

/** Destroys the servlet.
*/
public void destroy() {

}

/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setContentType("text/Html;charset=gb2312");
java.io.PrintWriter out = response.getWriter();
boolean empty=true;
boolean noResult=true;
String sqlStr;
int currentPage=0;
int totalPage=0;
int reccount=0;
ResultSet myrs=null;
String username=request.getParameter("username");
String email=request.getParameter("email");
String UContent=request.getParameter("ucontent");
String
if(ipage==nullipage.length()==0)
currentPage=1;
else
currentPage=Integer.parseInt(ipage);
if((username==nullusername.length()==0)(ucontent==nullucontent.length()==0))empty=true;
else empty=false;

Date myDate=new Date();
//String intime=new String(String.valueOf(myDate.getYear()+1990)+String.valueOf(myDate.getMonth()));
String year=String.valueOf(myDate.getYear()+1900);
String month=String.valueOf(myDate.getMonth()+1);
if(month.length()==1)
month=new String("0"+month);
String days=String.valueOf(myDate.getDate());
if(days.length()==1)
days=new String("0"+days);
String hours=String.valueOf(myDate.getHours());
if(hours.length()==1)
hours=new String("0"+hours);
String minutes=String.valueOf(myDate.getMinutes());
if(minutes.length()==1)
minutes=new String("0"+minutes);

String intime=year+"-"+month+"-"+days+" "+hours+":"+minutes;
if(email==nullemail.length()==0)
email=new String("");
if(!empty){
//username=convert(username);
//email=convert(email);
//ucontent=convert(ucontent);
sqlStr="insert into Mintegbook(Mname,Memail,Mcontent,Mtime,Mid) values("+username+","+email+","+ucontent+","+intime+",1)";
getDsnConn();
executeInsert(sqlStr);
}
sqlStr=new String("select Mname,Memail,Mcontent,Mtime from Mintegbook order by Mtime DESC");

getDsnConn();
reccount=getRecordCount("Mintegbook");
if(reccount==0)
noResult=true;
else
noResult=false;
if(!noResult){
int ipageSize=10;
totalPage=getTotalPage("Mintegbook",ipageSize);
if(currentPage>totalPage)
currentPage=totalPage;
int cursor=(currentPage-1)*ipageSize+1;
try{
myrs=executeScrollableQuery(sqlStr);
myrs.absolute(cursor);
}
catch(SQLException e){
noResult=true;
}
}
out.println("<HTML><HEAD><TITLE>我的Servlet留言板</TITLE>");
out.println("<META http-equiv="Content-Type" content="text/html; charset=gb2312">");
out.println("<STYLE type="text/
CSS">");
out.println("<!--");
out.println(".mytext { font-family: "宋體"; font-size: 12px}");
out.println(" -->");
out.println("</STYLE>");
out.println("</HEAD>");
out.println("<BODY bgcolor="#FFFFFF" text="#000000">");
out.println("<TABLE width="600" border="0" cellspacing="0" cellpadding="0" align="CENTER" class="mytext">");
out.println("<TR><TD height="22"> <a >我的主頁</a> 我的Servlet留言板(<A href="mailto:yf188@21cn.com">川石</A>制作)</TD></TR>");
out.println("<TR><TD height="1" bgcolor="#999933"></TD></TR>");
out.println("</TABLE>");
out.println("<BR>");
out.println("<TABLE width="600" border="0" cellspacing="0" cellpadding="0" align="CENTER" class="mytext">");
out.println("<TR><TD height="8"></TD></TR>");
out.println("<TR><TD height="18" bgcolor="#f7f7f7"><DIV align="right">");
out.println("共有 "+reccount + " 條留言 ");
out.println(" 當前第<font color=#ff0000>"+currentPage+"</font>/共 "+totalPage+" 頁 ");
if(currentPage>1)
out.println(" <a href=guestbookServlet?ipage=1>首頁</a> <a href=guestbookServlet?ipage="+(currentPage-1)+">上一頁</a> ");
else
out.println(" 首頁 上一頁 ");
if(currentPage<totalPage)
out.println(" <a href=guestbookServlet?ipage="+(currentPage+1)+">下一頁</a> <a href=guestbookServlet?ipage="+totalPage+">末頁</a> ");
else
out.println(" 下一頁 末頁 ");

out.println("</DIV></TD></TR>");

out.println("</TABLE>");
out.println("<BR>");

//這里是顯示留言內容
if(!noResult){
String dname;
String demail;
String dcontent;
String dtime;
Date temptime;
try{
do{

dname=new String(myrs.getString("Mname"));
demail=new String(myrs.getString("Memail"));
dcontent=new String(myrs.getString("Mcontent"));
try{
dtime=new String(myrs.getObject("Mtime").toString());
}
catch(java.lang.NullPointerException e){
dtime=new String("2001-04-06 12:30");
}
if(dname==null)
dname=new String("川石");
if(demail==null)
demail=new String("yf188@21cn.com");
if(dcontent==null)
dcontent=new String("test");
if(dtime==null)
dtime=new String("2001-04-06 12:30");
dname=convert(dname);
dcontent=convert(dcontent);
//temptime=myrs.getDate("Mtime");

/*
String tempyear=String.valueOf(temptime.getYear()+1900);
String tempmonth=String.valueOf(temptime.getMonth()+1);
if(tempmonth.length()==1)
tempmonth=new String("0"+tempmonth);
String tempdays=String.valueOf(temptime.getDate());
if(tempdays.length()==1)
tempdays=new String("0"+tempdays);
String tempminute=String.valueOf(temptime.getMinutes());
if(tempminute.length()==1)
tempminute=new String("0"+tempminute);
String temphours=String.valueOf(temptime.getHours());
if(temphours.length()==1)
temphours=new String("0"+temphours);
String dtime=tempyear+" 年 "+ tempmonth +" 月 "+ tempdays +" 日 " + temphours+ " 時 "+ tempminute + " 分 ";
*/
out.println("<TABLE width="600" border="0" cellspacing="0" cellpadding="4" align="CENTER" class="mytext">");
out.println("<TR><TD>姓名 <A href="mailto:"+demail+"">"+dname+"</A> 留言時間:"+dtime+"</TD></TR>");
out.println("<TR><TD height="10"></TD></TR>");
out.println("<TR><TD height="10">"+dcontent+"</TD></TR>");
out.println("</TABLE>");
out.println("<HR width="600" size="1">");
}while(myrs.next());}
catch(SQLException e){
out.println("error found");
}
}
else{
out.println("還沒有留言!");
}
//結束

out.println("<FORM name="form1" method="post" action="guestbookServlet">");
out.println("<TABLE width="600" border="0" cellspacing="0" cellpadding="4" align="CENTER" class="mytext">");
out.println("<TR><TD width="80">姓名:</TD><TD><INPUT type="text" name="username">*</TD></TR>");
out.println("<TR><TD>Email:</TD><TD><INPUT type="text" name="email">*</TD></TR>");
out.println("<TR><TD>留言:</TD><TD><TEXTAREA name="ucontent" cols="65" rows="4"></TEXTAREA></TD></TR>");
out.println("</TABLE>");
out.println("<TABLE width="400" border="0" cellspacing="0" cellpadding="6" align="CENTER" class="mytext">");
out.println("<TR><TD height="15" width="200"> </TD><TD> </TD></TR>");
out.println("<TR><TD><DIV align="RIGHT"><INPUT type="button" name="Button" value=" 提 交 " style="cursor:hand" onclick="Javascript:check()"></DIV></TD>");
out.println("<TD><INPUT type="reset" name="Submit2" value=" 重 置 "></TD></TR>");
out.println("</TABLE>");
out.println("</FORM>");

out.println("<TABLE width="400" border="0" cellspacing="0" cellpadding="4" align="CENTER" class="mytext">");
out.println("<TR><TD width="15"> </TD></TR>");
out.println("<TR><TD><DIV align="LEFT">聲明:這是川石利用國外免費jsp服務器開發的Servlet留言程序。應用JDBC連接mySQL數據庫。</DIV></TD></TR>");
out.println("<TR><TD> </TD></TR>");
out.println("</TABLE>");
out.println("</BODY>");
out.println("</HTML>");

out.println("<SCRIPT language="javascript">");
out.println("function check(){");
out.println("if(form1.username.value.length<1form1.ucontent.value.length<1)");
out.println("{alert(姓名和留言是必須有的!);}else{form1.submit();}}");
out.println("</SCRIPT>");

out.close();
}

/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}

/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
protected void getDsnConn(){
String sqlUrl="jdbc:mysql://10.0.0.1/你申請用戶名?user=你的名字&passWord=你的帳號";
try{
if(debug)
conn=DriverManager.getConnection("jdbc:odbc:ODBC源","用戶名","密碼口令");
else
conn=DriverManager.getConnection(sqlUrl);
}
catch(SQLException es){
System.err.println("和庫連接時出錯:"+es.getMessage());
}
}
protected void executeInsert(String sqlStr){
try{
Statement stmt=conn.createStatement();
stmt.executeUpdate(sqlStr);
}
catch(SQLException es){
System.err.println("執行插入時:"+es.getMessage());
}
}
protected void executeUpdate(String sqlStr){
try{
Statement stmt=conn.createStatement();
stmt.executeUpdate(sqlStr);
}
catch(SQLException e){
System.err.println("error in query record");
}
}

//查尋

protected ResultSet executeQuery(String sqlStr){
rs=null;
try{
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sqlStr);
}
catch(SQLException ex){
System.err.println("執行查尋出錯:"+ex.getMessage());
}
return rs;
}
protected ResultSet executeScrollableQuery(String sqlStr){
rs=null;
try{
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sqlStr);
}
catch(SQLException e){
System.err.println("執行動態查尋出錯");
}
return rs;
}
//得到表記錄總數
protected int getRecordCount(String sTableName){
rs=null;
int CountResult=0;
String sqlStr="select count(*) from "+sTableName;
try{
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sqlStr);
if(rs.next())
CountResult=rs.getInt(1);
rs=null;
stmt.close();
}
catch(SQLException ex){
System.err.println(ex.getMessage());

}

return CountResult;
}
//得到記錄總頁數
protected int getTotalPage(String sTableName,int iPageSize){
int totalPage;
int totalRecNum=getRecordCount(sTableName);
if(totalRecNum%iPageSize==0)
totalPage=totalRecNum/iPageSize;
else
totalPage=totalRecNum/iPageSize+1;
return totalPage;
}
protected String convert(String InputStr){
String converted=new String();
byte[] bytes;
try{
bytes=InputStr.getBytes("ISO8859-1");
converted=new String(bytes,"GB2312");
}
catch(java.io.UnsupportedEncodingException e){
System.out.print("error");
}
return converted;
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}

}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大同市| 南通市| 赞皇县| 金阳县| 五华县| 大余县| 达孜县| 寻乌县| 全州县| 宽甸| 扎鲁特旗| 临沭县| 贡嘎县| 达孜县| 肥城市| 墨竹工卡县| 汤阴县| 焉耆| 望都县| 哈巴河县| 措勤县| 合水县| 江阴市| 连州市| 古浪县| 滁州市| 洪雅县| 石楼县| 司法| 长岭县| 兴安盟| 元江| 曲水县| 朝阳县| 蒙自县| 库车县| 濉溪县| 宁德市| 桃源县| 祥云县| 枝江市|