教大家使用java实现顶一下踩一下功能
作者:上天入地 发布时间:2021-08-08 21:31:15
标签:java,顶一下,踩一下
本文实例为大家分享了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</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(function(){ getdigshtml();})
function isdigs(digtype)//顶一下,踩一下操作
{
$.ajax({
type:'POST',
url:'Digg',
data:'action=digs&digtype='+digtype,
/* beforeSend:function(){
$("#vote").hide();
$("#loadings").show();
}, ajax请求显示loading效果*/
success:function(msg){
switch (msg)
{
/* 后台用来判断
case '1':
$("#loadings").hide();
$("#vote").show();
alert("请先登录!");
break;
case '2':
$("#loadings").hide();
$("#vote").show();
alert("请先下载,再操作!");
break;
case '4':
$("#loadings").hide();
$("#vote").show();
alert("您已经参与过评价!");
break;*/
case '3':
getdigshtml();//重新绑定html
//$("#loadings").hide();
//$("#vote").show();
alert("谢谢你的参与!");
break;
default:
}
}
})
}
function getdigshtml()//获取顶一下,踩一下html
{
$.ajax({
type:'POST',
url:'Digg',
data:'action=getdigshtml',
success:function(msg){
$("#digg").html(msg);
}
})
}
</script>
<style type="text/css">
* {
padding:0;
margin:0;
}
.digg {
height: auto;
width: 190px;
font-size:12px;
font-weight:normal;
}
.digg a {
display: block;
height: 48px;
width: 189px;
background-image: url(images/mark.gif);
background-repeat: no-repeat;
position: relative;
color: #000;
text-decoration: none;
}
.digg .good {
margin-bottom:10px;
margin-top: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 p {
padding-left:30px;
line-height:25px;
}
.digg .bar {
background-color: white;
height: 5px;
left: 20px;
overflow: hidden;
position: absolute;
text-align: left;
top: 30px;
width: 55px;
}
.bar #g_img {
background-image: url(images/sprites.gif);
background-repeat: repeat-x;
height: 5px;
width: auto;
}
.bar #b_img {
background-image: url(images/sprites.gif);
background-repeat: repeat-x;
height: 5px;
width: auto;
background-position: 0px -5px;
}
.num {
color: #333;
font: normal normal 100 10px/12px Tahoma;
left: 80px;
position: absolute;
top: 26px;
}
.digg .good .bar {
border: 1px solid #40A300;
}
.digg .bad .bar {
border: 1px solid #555;
}
</style>
<script type="text/javascript">
</script>
</head>
<body>
<div class="digg" id="digg" style="margin-left: auto;margin-right: auto;">
</div>
</body>
</html>
后台servlet:
package com.test;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.NumberFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Digg extends HttpServlet {
private static Connection con = null;
private static Statement stmt = null;
/**
* Constructor of the object.
*/
public Digg() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf8");
response.setCharacterEncoding("utf8");
String 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 {
response.getWriter().write(getDigHtml());
} catch (Exception e) {
e.printStackTrace();
}
}
}
private String dig(String digtype)throws Exception{
String sql ="";
if(digtype.equals("digs")){
sql ="update dig set digs=digs+1 where id =1";
}else{
sql ="update dig set undigs=undigs+1 where id =1";
}
int num =stmt.executeUpdate(sql);
if(num>0){
return "3";
}
return "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()throws Exception{
NumberFormat nf = NumberFormat.getPercentInstance();
nf.setMaximumIntegerDigits(3);
nf.setMaximumFractionDigits(2);
String sql ="select * from dig where id=1";
ResultSet res = stmt.executeQuery(sql);
double digSum = 0 ;
double unDigSum =0 ;
double digSumAll = 0;
String digPer = "0%";
String unDigPer = "0%";
while(res.next()){
digSum = res.getInt("digs");
unDigSum = res.getInt("undigs");
}
digSumAll = digSum + unDigSum;
if(digSumAll !=0 ){
digPer = nf.format(digSum/digSumAll);
unDigPer = nf.format(unDigSum/digSumAll);
}
String str="<div class='good'>";
str+="<a href=JavaScript:isdigs('digs') >";
str+="<p>很好</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>";
return str;
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
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 (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void closeCon() {
try {
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
sql语句:
CREATE TABLE dig(
id INT PRIMARY KEY,
digs INT,
undigs INT
);
INSERT INTO dig VALUES(1,0,0);
来源:http://blog.csdn.net/xuweilinjijis/article/details/8880968


猜你喜欢
- 一、概述使用Java技术构建Web应用时, 我们通常离不开tomcat和jetty之类的servlet容器,这些Web服务器功能强大,性能强
- yield()介绍yield()的作用是让步。它能让当前线程由“运行状态”进入到“就绪状态”,从而让其它具有相同优先级的等待线程获取执行权;
- 本文实例讲述了Android获取设备CPU核数、时钟频率以及内存大小的方法。分享给大家供大家参考,具体如下:因项目需要,分析了一下 Face
- 引言在实际应用场景中,我们封装一个学生的类,这个类用于封装学生的日常行为,如:上学、吃饭、上课等。然而,在疫情期间,学生上学时入校、吃饭时进
- Servlet 3.0之前的版本中,文件上传是个挺让人头疼的问题,虽然有第三方框架来实现,但使用也还是比较麻烦,在Servlet 3.0中,
- 本文实例为大家分享了C++实现连连看游戏的具体代码,供大家参考,具体内容如下这个项目还是挺不错的,运行后也比较有意思,可以看看。#inclu
- 人人客户端有一个特效还是挺吸引人的,在主界面手指向右滑动,就可以将菜单展示出来,而主界面会被隐藏大部分,但是仍有左侧的一小部分同菜单一起展示
- 冒泡排序原理:从头(左边)开始比较每一对相邻的元素,如果第1个比第2个大,就交换它们的位置,执行完一轮后,最末尾(最右边)就是最大的元素。举
- 延迟加载(lazy loading) 设计模式是为了避免一些无谓的性能开销而提出来的,所谓延迟加载就是当在真正需要数据(读取属性值)的时候,
- 前言最近我在项目写了几万行代码,小伙伴担心会让程序启动速度变慢,所以本渣就来做测试。 本渣使用了代码创建器,创建了 1000 个垃圾文件,这
- 近期用到了一位师兄写的C++程序,总体功能良好。使用不同的数据测试,发现了一个明显的缺点:大数据量下,预处理过程耗时很长。中科院的某计算集群
- 介绍在上一篇“SimpleAdapter“章节中,我们看到了把:ListView和Listview内
- 一、前言Android 实现卫星式菜单也叫弧形菜单,主要要做的工作如下:1.动画的处理2.自定义ViewGroup来实现卫星式菜单View(
- ViewStub可以在运行时动态的添加布局。帮助文档给定的定义是:"A ViewStub is an invisible, zer
- 背景:听说ClassLoader类加载机制是进入BAT的必经之路。ClassLoader总述:普通的Java开发其实用到ClassLoade
- 项目里头需要做一个判断用户输入的号码是否是正确的手机号码,正确的手机号码应该是11位的,这里我们需要用一个正则表达式来进行判断,
- 系统有很多光标类型 :Cursors 类 (System.Windows.Input) | Microsoft Docs本章介绍如何自定义光
- 本文是作者结合网上的一些资料封装的java图片处理类,支持图片的缩放,旋转,马赛克化。不多说,上代码:package deal;import
- 前段时间写了一篇C#解析Lrc歌词文件,对lrc文件进行解析,支持多个时间段合并。本文借下载歌词文件来探讨一下同步和异步方法。 L
- Docker是干什么的Docker 是一个基于Linux容器(LXC-linux container)的高级容器引擎,基于go语言开发,源代