展开全部
package cn.sdeit.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.sdeit.beans.Team;
import cn.sdeit.beans.User;
import cn.sdeit.mysql.ConnectMySql;
/**
* Servlet implementation class JoinTeam
*/
@WebServlet(name = "JoinTeam",urlPatterns = {"/JoinTeam"})
public class JoinTeam extends HttpServlet
{
private static final long serialVersionUID = 1L;
private String captainName = null;
private String teamName = null;
private PrintWriter writer = null;
/**
* @see HttpServlet#HttpServlet()
*/
public JoinTeam()
{
62616964757a686964616fe58685e5aeb931333361323565super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
writer = response.getWriter();
captainName = request.getParameter("name");
teamName = request.getParameter("team");
if(teamName.equals((new Team()).getTeamNameByCaptainName(captainName)))
{
int result = joinTeam(((User)request.getSession().getAttribute("oldUser")).getEmail());
if(result == 0)
{
showErrorMessage("抱歉!服务器内部错误,加入队伍失败!请及时反馈管理员!");
}
else
{
response.sendRedirect("mine.jsp");
}
}
else
{
showErrorMessage("抱歉!没有找到符合条件的团队!请联系您的队长解决,若信息无误,且问题一直出现,请联系管理员!");
writer.flush();
writer.close();
}
}
private void showErrorMessage(String error)
{
writer.write(error);
writer.flush();
writer.close();
}
private int joinTeam(String email)
{
int position = (new Team()).getMinEmptyMemberPosition(teamName);
int result = 0;
if(position == -1)
{
showErrorMessage("抱歉!此小组人数已达到上限,请更换队伍或者协商队长调整组员结构!");
}
else
{
Connection connection = ConnectMySql.createConnection();
String sql = "update team set member"+position+" "+"= ? where name = ?";
PreparedStatement prepared = ConnectMySql.createPreparedStatement(connection, sql);
try
{
prepared.setString(1, email);
prepared.setString(2, teamName);
result = prepared.executeUpdate();
connection.close();
prepared.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
return result;
}
}