2021-11-08

这篇博客展示了如何利用JSP和Servlet来创建一个简单的明星投票系统。通过Servlet接收并处理投票请求,利用ServletContext存储投票计数,jQuery的AJAX实现页面无刷新更新。用户点击按钮即可为喜欢的明星投票,投票结果实时显示在页面上。
摘要由CSDN通过智能技术生成

使用jsp+servlet完成对喜欢的明星进行投票

首先创建servlet接收请求,处理请求


package com.m.controller;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/vote.do")
public class VoteController implements Servlet {
	int lin=0;
	int wang=0;
	int hu=0;
	int jin=0;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public ServletConfig getServletConfig() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getServletInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		HttpServletRequest request =(HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		String a= request.getParameter("a");
		 ServletContext application = request.getServletContext();
		 if (application.getAttribute(a)==null&&a.equals("lin")) {
			 lin=0;
			 application.setAttribute(a, ++lin);
			 response.getWriter().append("success");
			 return;
		 }
		 if (application.getAttribute(a)==null&&a.equals("wang")) {
			 wang=0;
			 application.setAttribute(a, ++wang);
			 response.getWriter().append("success");
			 return;
		 }
		 if (application.getAttribute(a)==null&&a.equals("hu")) {
			 hu=0;
			 application.setAttribute(a, ++hu);
			 response.getWriter().append("success");
			 return;
		 }
		 if (application.getAttribute(a)==null&&a.equals("jin")) {
			 jin=0;
			 application.setAttribute(a, ++jin);
			 response.getWriter().append("success");
			 return;
		 }
		 if (application.getAttribute(a)!=null&&a.equals("lin")) {
			 application.setAttribute(a, Integer.valueOf(application.getAttribute(a).toString())+1);
			 response.getWriter().append("success");
		 }
		 if (application.getAttribute(a)!=null&&a.equals("wang")) {
			 application.setAttribute(a,  Integer.valueOf(application.getAttribute(a).toString())+1);
			 response.getWriter().append("success");
		 }
		 if (application.getAttribute(a)!=null&&a.equals("hu")) {
			 application.setAttribute(a,  Integer.valueOf(application.getAttribute(a).toString())+1);
			 response.getWriter().append("success");
		 }
		 if (application.getAttribute(a)!=null&&a.equals("jin")) {
			 application.setAttribute(a,  (Integer.parseInt(application.getAttribute(a).toString()))+1);
			 response.getWriter().append("success");
		 }
		 
	}

}



页面的编写使用jquery的ajax请求进行数据的传输

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-3.4.1.min.js" ></script>
<script type="text/javascript">
	function ticket(a) {
	 	$.ajax({
			url:"vote.do",
			data:"a="+a,
			success:function(result){
				if (result=="success") {
					location.href="vote.jsp";
				}
			}
		}); 
	}
</script>
</head>
<body>
	<table border="1px"  >
		<tr>
			<td>林志颖</td>
			<td>${lin }张票</td>
			<td><input type="button" value="投票" onclick="ticket('lin')" ></td>
		</tr>
		<tr>
			<td>胡歌</td>
			<td>${hu }张票</td>
			<td><input type="button" value="投票" onclick="ticket('hu')" ></td>
		</tr>
		<tr>
			<td>靳东</td>
			<td>${jin }张票</td>
			<td><input type="button" value="投票" onclick="ticket('jin')" ></td>
		</tr>
		<tr>
			<td>王凯</td>
			<td>${wang }张票</td>
			<td><input type="button" value="投票" onclick="ticket('wang')" ></td>
		</tr>
	</table>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值