使用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>