この例では、参照用のJava関数を実装するための特定のコードを共有しています。特定のコンテンツは次のとおりです
レンダリングは次のとおりです。
メインページindex.html:
<!doctype html public " - // w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <title> digg </titpe> <script = "text/javascript" "" src = "jquery-1.3.2.min.js"> </script> <script> <text/javascript "> $(function(){getdigshtml();})function isdigs(digtype)//操作{$ .ajax({type: 'post'、url: '' '、' 'post'、 'post'、データ: 'Action = Digtype ='+Digtype、 /* beforesend:function( "#vote")。 $ show();評価!」 $ .ajax({type: 'post'、url: 'digg'、data: 'action = getdigshtml'、success:function(msg){$( "#digg")。html(msg);}})} </script> <style type = "text/css"> * {pading:0;マージン:0; } .digg {height:auto;幅:190px;フォントサイズ:12px; font-weight:正常; } .digg a {display:block;高さ:48px;幅:189px;背景イメージ:url(images/mark.gif);バックグラウンドリピート:ノーリピート。位置:相対;色:#000;テキスト装置:なし; } .digg .good {margin-bottom:10px;マージントップ:5px; } .digg .good a {background -position:-189px 0px; } .digg .good a:hover {background-position:0px 0px; } .digg .bad a {background -position:-378px 0px; } .digg .bad a:hover {background -position:-567px 0px; } .digg a:hover {background -position:-567px 0px; } .digg ap {padding-left:30px;ラインハイト:25px; } .digg .bar {background-color:white;高さ:5px;左:20px;オーバーフロー:隠し;位置:絶対;テキストアライグ:左;トップ:30px;幅:55px; } .bar #g_img {background-image:url(images/sprites.gif);バックグラウンドリピート:Repeat-X;高さ:5px;幅:auto; } .bar #b_img {background-image:url(images/sprites.gif);バックグラウンドリピート:Repeat-X;高さ:5px;幅:auto;バックグラウンドポジション:0px -5px; } .num {color:#333;フォント:通常の通常100 10px/12pxタホマ。左:80px;位置:絶対;トップ:26px; } .digg .good .bar {border:1px solid#40a300; } .digg .bad .bar {border:1px solid#555; } </style> <スクリプトタイプ= "text/javascript"> </script> </head> <body> <div id = "digg" style = "margin-left:auto; margin-right:auto;"> </div> </body> </html> </html>バックエンドサーブレット:
パッケージcom.test; java.io.ioexceptionをインポートします。 java.io.printwriterをインポートします。 Java.sql.Connectionをインポートします。 Java.sql.drivermanagerをインポートします。 java.sql.resultsetをインポートします。 java.sql.sqlexceptionをインポートします。 java.sql.Statementをインポートします。 java.text.numberformatをインポートします。 javax.servlet.servletexceptionをインポートします。 javax.servlet.http.httpservletをインポートします。 javax.servlet.http.httpservletrequestをインポートします。 javax.servlet.http.httpservletResponseをインポートします。パブリッククラスdiggはhttpservletを拡張します{private static connection con = null; private staticステートメントstmt = null; /***オブジェクトのコンストラクター。 */ public digg(){super(); } /***サーブレットの破壊。 <br> */ public void Destroy(){super.destroy(); //ログに「Destroy」文字列を置くだけで//ここにコードを置く} public void doget(httpservletrequest request、httpservletResponse応答)Servletexception、ioexception {this.dopost(request、response); } public void dopost(httpservletrequest request、httpservletresponse応答)servletexception、ioexception {request.setcharacterencoding( "utf8"); Response.setcharacterencoding( "utf8");文字列Action = request.getParameter( "action"); string digtype = request.getParameter( "digtype"); if(action.equals( "digs")){try {response.getWriter()。write(dig(digtype)); } catch(Exception e){e.printstacktrace(); }} else if(action.equals( "getdigshtml")){try {ressons.getWriter()。write(getDightml()); } catch(Exception e){e.printstacktrace(); }}} private string dig(string digtype)スロー例外{string sql = ""; if(digtype.equals( "digs")){sql = "dig set digs = digs+1 where id = 1"; } else {sql = "更新dig set undgs = undgs+1ここでid = 1"; } int num = stmt.executeupdate(sql); if(num> 0){return "3"; } "1"を返します。 } public static void main(string [] args){numberformat nf = numberformat.getPercentInstance(); nf.setmaximumintegerdigits(4); nf.setmaximumfractiondigits(6); double d =(double)1/(double)7; system.out.println(nf.format(d)); } private string getDightml()スロー例外{numberformat nf = numberformat.getPercentInstance(); nf.setmaximumintegerdigits(3); nf.setmaximumfractiondigits(2);文字列sql = "select * from dig where id = 1";結果res = stmt.executequery(sql); double digsum = 0; double undgsum = 0; double digsumall = 0;文字列digper = "0%";文字列undigper = "0%"; while(res.next()){digsum = res.getint( "digs"); undgsum = res.getint( "undgss"); } digsumall = digsum + undigsum; if(digsumall!= 0){digper = nf.format(digsum/digsumall); undigper = nf.format(undgsum/digsumall); } string str = "<div class = 'good'>"; str+= "<a href = javascript:isdigs( 'digs')>"; str+= "<p> good </p> <div class = 'bar'> <div id = 'g_img' style = 'width:"+digper+"'> </div> </div>"; str+= "<span class = 'num'>"+digper+"("+digsum+")</span>"; str+= "</a> </div> <div class = 'bad'>"; str+= "<a href = javascript:isdigs( 'undigs')>"; str+= "<p>非常に悪い</p> <div class = 'bar'> <div id = 'b_img' style = 'width:"+undigper+"'> </div> </div>"; str+= "<span class = 'num'>"+undigper+"("+undigsum+")</span>"; str+= "</a> </div>"; strを返します。 } /***サーブレットの初期化。 <br> * * @throws servletexception *エラーが発生した場合 */ public void init()throws servletexception {try {class.forname( "com.mysql.jdbc.driver"); con = drivermanager.getConnection( "jdbc:mysql://172.16.42.39:3306/dig"、 "root"、 "12345678"); stmt = con.createstatement(); } catch(例外e){// todo auto-enerated catch block e.printstacktrace(); }} public void closecon(){try {stmt.close(); con.close(); } catch(sqlexception e){// todo auto-enerated catch block e.printstacktrace(); }}} SQLステートメント:
Table Dig(ID intプライマリキー、digs int、undgs int)を作成します。 DIG値に挿入(1,0,0);
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。