⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 registrationservlet.java

📁 在线足球社区源代码
💻 JAVA
字号:
package web;

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.util.*;
import java.sql.*;

import web.domain.*;
import web.util.*;

/*
 * 
 * @author sun
 *
 * TODO 要更改此生成的类型注释的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class RegistrationServlet extends HttpServlet{
	private RegisterService regSvc;
	/*
	 *  (非 Javadoc)
	 * @see javax.servlet.GenericServlet#init()
	 */
	public void init(){
		regSvc = new RegisterService();
	}
	/*
	 *  (非 Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
		System.out.println("In the method of doPost");
		
		String action = request.getParameter("action");
		System.out.println("Action:"+action);
		
		if(action.equals("SelectLeague")){
			processSelectLeagueRequest(request,response);
		}else if(action.equals("EnterPlayer")){
			processEnterPlayerRequest(request,response);
		}else if(action.equals("SelectDivision")){
			processSelectDivisionRequest(request,response);
		}
	}
	/*
	 * 处理select_league表单的请求
	 */
	public void processSelectLeagueRequest(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
		System.out.println("In the method of processSelectLeagueRequest");
		
		//step1
		String season = request.getParameter("season");
		String year = request.getParameter("year");
		System.out.println("Season&Year: "+season+" "+year);
		
		//step2
		Status status = new Status();
		if(season==null||season.equals("UNKNOWN")){
			String msg = "Please select a league season.";
			status.addException(msg);
		}
		if(year==null||year.equals("UNKNOWN")){
			String msg = "Please select a league year.";
			status.addException(msg);
		}
		
		//step3
		if(status.isSuccessful()){
			try{
				League league = regSvc.getLeague(season,year);
				if(league==null){
					String msg = "The league your selected is not exist yet,please select another.";
					status.addException(msg);
				}
				if(status.isSuccessful()){
					HttpSession session = request.getSession();//New
					session.setAttribute("league",league);//
					
					RequestDispatcher view = request.getRequestDispatcher("enter_player.jsp");
					view.forward(request,response);
				}else{
					request.setAttribute("status",status);
					RequestDispatcher view = request.getRequestDispatcher("select_league.jsp");
					view.forward(request,response);					
				}			
			}catch(SQLException ex){
				throw new ServletException(ex);
			}
			
		}else{
			request.setAttribute("status",status);
			RequestDispatcher view = request.getRequestDispatcher("select_league.jsp");
			view.forward(request,response);
		}
	}

	/*
	 * 处理enter_player表单的请求
	 */
	public void processEnterPlayerRequest(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
		System.out.println("In the method of processEnterPlayerRequest");
		
		String name=request.getParameter("name");
		String address=request.getParameter("address");
		String city=request.getParameter("city");
		String province=request.getParameter("province");
		String postalCode=request.getParameter("postalCode");

		System.out.println("Name:"+name);
		System.out.println("Address:"+address);
		System.out.println("City:"+city);
		System.out.println("Province:"+province);
		System.out.println("Postal Code:"+postalCode);	
		
		Status status = new Status();
		if(name==null||name.length()==0){
			String msg = "Please enter your name.";
			status.addException(msg);
		}
		if(address==null||address.length()==0||city==null||city.length()==0||
				province==null||province.length()==0||postalCode==null||postalCode.length()==0){
			String msg = "Please enter full address.";
			status.addException(msg);
		}
		
		if(status.isSuccessful()){
			try{
				Player player = regSvc.getPlayer(name);
				if(player!=null){
					String msg ="The player your entered is already exist,please enter another.";
					status.addException(msg);
				}
				
				if(status.isSuccessful()){
					player = new Player(name,address,city,province,postalCode);
					HttpSession session = request.getSession();//Get
					session.setAttribute("player",player);//
					
					RequestDispatcher view = request.getRequestDispatcher("select_division.jsp");
					view.forward(request,response);
				}else{
					request.setAttribute("status",status);
					RequestDispatcher view = request.getRequestDispatcher("enter_player.jsp");
					view.forward(request,response);					
				}
			}catch(SQLException ex){
				throw new ServletException(ex);
			}
		}else{
			request.setAttribute("status",status);
			RequestDispatcher view = request.getRequestDispatcher("enter_player.jsp");
			view.forward(request,response);
		}
	}	

	/*
	 * 处理select_division表单的请求
	 */
	public void processSelectDivisionRequest(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
		System.out.println("In the method of processSelectDivisionRequest");
		
		String division=request.getParameter("division");
		System.out.println("Division:"+division);
		
		Status status = new Status();
		if(division==null||division.equals("UNKNOWN")){
			String msg = "Please select a division.";
			status.addException(msg);
		}
		
		if(status.isSuccessful()){
			try{
				HttpSession session = request.getSession();//Get
				League league = (League)session.getAttribute("league");
				Player player = (Player)session.getAttribute("player");
				System.out.println("League: "+league);
				System.out.println("Player: "+player);
				regSvc.register(league,player,division);//?
				
				RequestDispatcher view = request.getRequestDispatcher("thank_you.jsp");
				view.forward(request,response);
			}catch(SQLException ex){
				throw new ServletException(ex);
			}
		}else{
			request.setAttribute("status",status);
			RequestDispatcher view = request.getRequestDispatcher("select_division.jsp");
			view.forward(request,response);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -