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

📄 loginservlet.java

📁 运用JSP/servlet/JavaBean 技术
💻 JAVA
字号:
package com.lovo.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lovo.po.User;
import com.lovo.service.IUserBo;
import com.lovo.service.IUserBoImpl;

/**
 * <p>管理注册的控制器</p>
 * @author fenglu
 *
 */
public class LoginServlet extends HttpServlet
{
	/** userBo接口,用于业务方法调用 */
	private IUserBo userBo = new IUserBoImpl();

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/** 设置响应文件类型及字符集编码 */
		response.setContentType("text/html; charset=utf-8");
		/** 设置响应头,让浏览器不缓存信息 */
		response.setHeader("Cache-control", "no-cache");
		/** 检测用户是否被占用 */
		String checkEmailName = request.getParameter("checkEmailName");
		if(checkEmailName != null && checkEmailName.equals("true"))
		{
			PrintWriter writer = response.getWriter();
			String emailName = request.getParameter("newEmailName").toLowerCase();
			if(userBo.isEmailNameExist(emailName) == -1)
			{
				writer.println("可以使用");
			}
			else
			{
				writer.println("帐号被占用");
			}
			return;
		}
		/** 获得注册帐号及密码 */
		String emailName = request.getParameter("newEmailName").toLowerCase();
		String password  = request.getParameter("password1");
		/** 检验待注册帐号是否已经存在 */
		boolean exist = userBo.isEmailNameExist(emailName) != -1 ? true : false;
		String message = "";//存反馈信息
		/**
		 * 如果帐号已经存在,那么设置反馈信为帐号已经存在。
		 * 如果帐号不存在,那么进行注册,并且设置反馈信息为注册成功
		 */
		if(exist)
		{
			/** 帐号已经存在,设置反馈信息 */
			message = "用户已经存在,请重新输入帐号!";
		}
		else
		{
			/** 帐号还没有人用,注册该帐号 */
			/** 将待注册信息封装到User对象中 */
			User user = new User();
			user.setEmailName(emailName);
			user.setPassword(password);
			/** 添加新用户,并返回boolean值以表示是否添加成功 */
			boolean success = userBo.addUser(user);
			/**
			 * 如果添加成功,设置添加成功信息
			 * 如果失败,设置返回信息为出错
			 */
			if(success)
			{
				/** 添加成功,返回成功信息 */
				message = "注册成功!";
			}
			else
			{
				/** 添加失败,返回失败信息 */
				message = "系统出错,注册失败!";
			}
		}
		/** 设置注册反馈信息到session */
		request.getSession().setAttribute("loginMsg", message);
		/** 跳转到Login.html显示操作结果 */
		response.sendRedirect("Login.html");
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		doGet(request, response);
	}

}

⌨️ 快捷键说明

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