码迷,mamicode.com
首页 > 其他好文 > 详细

【Servlet】Servlet简单登陆验证实例

时间:2015-04-21 14:42:42      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:servlet   登陆   

林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka

本文要实现Servlet简单登陆验证实例。结构如下

技术分享

1、LoginCheck.java

package com.mucfc;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");     
        String password = request.getParameter("password");        
          
        //服务器端打印信息  
        System.out.println("username=" + username);  
        System.out.println("password=" + password);  
        if(username!=null&&password!=null && username.equals("linbingwen")&&password.equals("1234")){
        	//将数据存储在request对象中,传递给下一个servlet使用
        	request.setAttribute("username", username);
        	//进行数据的转发
        	RequestDispatcher dispatcher=request.getRequestDispatcher("Welcome");
        	dispatcher.forward(request, response);
       
        }
        else {
			response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,"用户名或密码错误");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		  doGet(request, response);    
	}

}
2、Welcome
package com.mucfc;

import java.io.IOException;

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

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String username = request.getParameter("username");   
		//设置编码格式  
        response.setContentType("text/html;charset=GB18030");           
        //返回html页面  
        response.getWriter().println("<html>");  
        response.getWriter().println("<head>");     
        response.getWriter().println("<title>登录信息</title>");      
        response.getWriter().println("</head>");    
        response.getWriter().println("<body algin=center>");     
        response.getWriter().println("欢迎【" + username + "】用户登录成功!!!");    
        response.getWriter().println("</body>");    
        response.getWriter().println("</html>");  
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		  doGet(request, response);    
	}

}

3、index.jsp

<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>登录</title>  
</head>
<body>
<form action="servlet/LoginCheck" method="POST">  
            用户:<input type="text" name="username" /><br/>  
            密码:<input type="password" name="password" /><br/>  
            <input type="submit" value="登录" />           
        </form>  

</body>
</html>

4、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
 <!-- 初始的页面 -->
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 用户名和密码验证的servlet -->
  <servlet>
    <servlet-name>LoginCheck</servlet-name>
    <servlet-class>com.mucfc.LoginCheck</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginCheck</servlet-name>
    <url-pattern>/servlet/LoginCheck</url-pattern>
  </servlet-mapping>
  <!-- 显示欢迎的servlet -->
    <servlet>
    <servlet-name>Welcome</servlet-name>
    <servlet-class>com.mucfc.Welcome</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Welcome</servlet-name>
    <url-pattern>/servlet/Welcome</url-pattern>
  </servlet-mapping>
  
</web-app>


运行后:

技术分享

输入正确用户名(lingbingwen)和密码(1234)

技术分享

输入错误

技术分享



Servlet接口定义三个实现类,分别是FacesServlet, GenericServlet, HttpServlet。下面我们简单的介绍一下。

5、Servlet接口实现类

     (1)FacesServlet是一个用于管理利用JSF(JavaServer Faces)构建用户界面的web应用程序请求处理生命周期的Servlet。(FacesServletis a servlet that manages the request processing lifecycle for web applications that are utilizing JavaServer Faces to construct the user interface.),大家如果想深入的学习,可以点这里。

     (2)GenericServlet是HttpServlet的父类,包含了servlet的常用方法。GenericServlet使得程序员写servlets更加简单,它提供了初始化和销毁生命周期的方法,以及实现了写日志文件的方法。

     (3)HttpServlet指能够处理HTTP请求的servlet。这是开发人员在编写Servlet时,通常应继承这个类即可。    

        对于这三个实现类,只为大家做一个简单的介绍,如果想深入的研究,直接看API就好。


林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka

【Servlet】Servlet简单登陆验证实例

标签:servlet   登陆   

原文地址:http://blog.csdn.net/evankaka/article/details/45167773

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!