用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

邮箱登录

2014-04-17 作者: 云代码会员举报

[java]代码库

package chapter03.servlet;

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

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import chapter03.model.User;

/**
 * Servlet implementation class loginServlet
 */
public class loginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public loginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=GBK");
		String action = request.getParameter("action");
		if ("login".equals(action)){
			login(request, response);
		}
		else if ("check".equals(action)){
			check(request, response);
		}
		else if ("logout".equals(action)){
			logout(request, response);
		}
		else{
			unlogin(request, response);
		}
	}
	
	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String loginUrl = "/login.html";
		String fileUrl = "/login?action=check";
		HttpSession session = request.getSession();
		String userName = request.getParameter("user_name");
		String password = request.getParameter("login_password");
		// 验证用户ID和密码是否合法,略...
		ServletContext context = getServletContext();
		if (userName == null || userName.equals("")) {
			RequestDispatcher dispatcher = 
		context.getRequestDispatcher(loginUrl);
			response.setContentType("text/html;charset=GBK");
			dispatcher.forward(request, response);
			return;
		}
		// 登录成功,设置session中的属性
		else{
			User user = new User();
			user.setUserName(userName);
			session.setAttribute("User", user);
			RequestDispatcher dispatcher = context.getRequestDispatcher(fileUrl);
			dispatcher.forward(request, response);
		}
	}
	private void check(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("User");
		if (user == null) {  // 若未登陆,则先跳转到登录页面
			response.setContentType("text/html;charset=GBK");
			response.setHeader("Refresh", "3; URL=login.html");
			PrintWriter out = response.getWriter();
			out.println("<FONT color='red'>请先登录!</FONT><BR>");
			out.println("<FONT color='blue'>3秒后跳转到登录页面...</FONT>");
			out.close();
			return;
		}
		else if(user.getUserName().equals("sss"))
		{
			String loginURL = "login?action=login";
			String logoutURL = "login?action=logout";
			response.setContentType("text/html;charset=GBK");
			PrintWriter out = response.getWriter();
			out.println("<HTML><HEAD><TITLE>MailServlet:check</TITLE></HEAD>");
			out.println("<body>");
			out.println("当前用户为:" + user.getUserName());
			out.println("<p><p>");
			out.println("你的邮箱中有100封邮件");
			out.println("<p><p>");
			out.println("<a href='" + loginURL + "'>登陆</a>");
			out.println("<a href='" + logoutURL + "'>注销</a>");
			out.println("</body></html>");
			out.close();
		}
		else
		{
			String loginURL = "login?action=login";
			response.setContentType("text/html;charset=GBK");
			PrintWriter out = response.getWriter();
			out.println("<HTML><HEAD><TITLE>MailServlet:check</TITLE></HEAD>");
			out.println("<body>");
			out.println(user.getUserName() + "您好!");
			out.println("<p><p>");
			out.println("<FONT color='red'>您无权访问邮件系统。</FONT>");
			out.println("<p><p>");
			out.println("请重新<a href='" + loginURL + "'>登陆</a>邮件系统!!");
			out.println("</body></html>");
			out.close();
		}
	}
	private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("User");
		String name = user.getUserName();
		session.invalidate();
		String loginURL = "login?action=login";
		PrintWriter out = response.getWriter();
		out.println("<HTML><HEAD><TITLE>MailServlet:logout</TITLE></HEAD>");
		out.println("<body>");
		out.println(name + ",再见!");
		out.println("<p><p>");
		out.println("<a href='" + loginURL + "'>重新登录邮件系统</a>");
		out.println("</body></html>");
		out.close();
	}
	
	private void unlogin(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String loginURL = "login?action=login";
		response.setContentType("text/html;charset=GBK");
		PrintWriter out = response.getWriter();
		out.println("<HTML><HEAD><TITLE>MailServlet:unlogout</TITLE></HEAD>");
		out.println("<body>");
		out.println("您好!");
		out.println("<p><p>");
		out.println("请您先<a href='" + loginURL + "'>登陆</a>邮件系统!");
		out.println("</body></html>");
		out.close();
	}

}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...