---
title: "使用Servlet实现登录小例子"
author: "cakin24"
date: "2017-10-13T20:10:29Z"
tags: []
categories: ["编程语言"]
slug: "servlet"
draft: false
lastmod: "2017-10-13T20:10:29Z"
id: 2396238
url: "https://cakin24.iteye.com/blog/2396238"
---

一 实体类

package com.po;

//用户类

public class Users {

 

        private String username;

        private String password;

        

        public Users()

        {

                

        }

 

        public String getUsername() {

                return username;

        }

 

        public void setUsername(String username) {

                this.username = username;

        }

 

        public String getPassword() {

                return password;

        }

 

        public void setPassword(String password) {

                this.password = password;

        }

        

        

}

二 Servlet类

package servlet;

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.po.Users;

public class LoginServlet extends HttpServlet {

    /\**

     * Constructor of the object.

     \*/

    public LoginServlet() {

        super();

    }

    /\**

     * Destruction of the servlet. &lt;br&gt;

     \*/

    public void destroy() {

        super.destroy(); // Just puts "destroy" string in log

        // Put your code here

    }

    /\**

     * The doGet method of the servlet. &lt;br&gt;

     *

     * 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 {

        doPost(request,response);

    }

    /\**

     * The doPost method of the servlet. &lt;br&gt;

     *

     * 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 {

        Users u = new Users();

        String username = request.getParameter("username");

        String password = request.getParameter("password");

        u.setUsername(username);

        u.setPassword(password);

        //判断用户名和密码是否合法

        if(u.getUsername().equals("admin")&&u.getPassword().equals("admin"))

        {

            response.sendRedirect(request.getContextPath()+"/login\_success.jsp");

        }

        else

        {

            response.sendRedirect(request.getContextPath()+"/login\_failure.jsp");

        }

    }

    /\**

     * Initialization of the servlet. &lt;br&gt;

     *

     * @throws ServletException if an error occurs

     \*/

    public void init() throws ServletException {

        // Put your code here

    }

}

 

三 界面

1 login.jsp

&lt;%@ page language="java" import="java.util.\*" contentType="text/html; charset=utf-8"%&gt;

&lt;%

   String path = request.getContextPath();

   String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

&lt;html&gt;

        &lt;head&gt;

                &lt;!-- Page title --&gt;

                &lt;title&gt;imooc - Login&lt;/title&gt;

                &lt;!-- End of Page title --&gt;

                &lt;!-- Libraries --&gt;

                &lt;link type="text/css" href="css/login.css" rel="stylesheet" /&gt;   

                &lt;link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" /&gt;   

                &lt;script type="text/javascript" src="js/jquery-1.3.2.min.js"&gt;&lt;/script&gt;

                &lt;script type="text/javascript" src="js/easyTooltip.js"&gt;&lt;/script&gt;

                &lt;script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"&gt;&lt;/script&gt;

                &lt;!-- End of Libraries --&gt;   

        &lt;/head&gt;

        &lt;body&gt;

        &lt;div id="container"&gt;

                &lt;div class="logo"&gt;

                        &lt;a href="#"&gt;&lt;img src="assets/logo.png" alt="" /&gt;&lt;/a&gt;

                &lt;/div&gt;

                &lt;div id="box"&gt;

                        &lt;form action="servlet/LoginServlet" method="post"&gt;

                        &lt;p class="main"&gt;

                                &lt;label&gt;用户名: &lt;/label&gt;

                                &lt;input name="username" value="" /&gt;

                                &lt;label&gt;密码: &lt;/label&gt;

                                &lt;input type="password" name="password" value=""&gt;   

                        &lt;/p&gt;

                        &lt;p class="space"&gt;

                                &lt;input type="submit" value="登录" class="login" style="cursor: pointer;"/&gt;

                        &lt;/p&gt;

                        &lt;/form&gt;

                &lt;/div&gt;

        &lt;/div&gt;

        &lt;/body&gt;

&lt;/html&gt;

2 login\_success.jsp

&lt;%@ page language="java" import="java.util.\*" contentType="text/html; charset=utf-8"%&gt;

&lt;%

   String path = request.getContextPath();

   String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

&lt;html&gt;

        &lt;head&gt;

                &lt;!-- Page title --&gt;

                &lt;title&gt;imooc - Login&lt;/title&gt;

                &lt;!-- End of Page title --&gt;

                &lt;!-- Libraries --&gt;

                &lt;link type="text/css" href="css/login.css" rel="stylesheet" /&gt;   

                &lt;link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" /&gt;   

                &lt;script type="text/javascript" src="js/jquery-1.3.2.min.js"&gt;&lt;/script&gt;

                &lt;script type="text/javascript" src="js/easyTooltip.js"&gt;&lt;/script&gt;

                &lt;script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"&gt;&lt;/script&gt;

                &lt;!-- End of Libraries --&gt;   

        &lt;/head&gt;

        &lt;body&gt;

        &lt;div id="container"&gt;

                &lt;div class="logo"&gt;

                        &lt;a href="#"&gt;&lt;img src="assets/logo.png" alt="" /&gt;&lt;/a&gt;

                &lt;/div&gt;

                &lt;div id="box"&gt;

                  &lt;%

                     String loginUser = "";

                     if(session.getAttribute("loginUser")!=null)

                     {

                        loginUser = session.getAttribute("loginUser").toString();

                     }

                  %&gt;

                     欢迎您&lt;font color="red"&gt;&lt;%=loginUser%&gt;&lt;/font&gt;,登录成功！

                &lt;/div&gt;

        &lt;/div&gt;

        &lt;/body&gt;

&lt;/html&gt;

3 login\_failure.jsp

&lt;%@ page language="java" import="java.util.\*" contentType="text/html; charset=utf-8"%&gt;

&lt;%

   String path = request.getContextPath();

   String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

&lt;html&gt;

        &lt;head&gt;

                &lt;!-- Page title --&gt;

                &lt;title&gt;imooc - Login&lt;/title&gt;

                &lt;!-- End of Page title --&gt;

                &lt;!-- Libraries --&gt;

                &lt;link type="text/css" href="css/login.css" rel="stylesheet" /&gt;   

                &lt;link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" /&gt;   

                &lt;script type="text/javascript" src="js/jquery-1.3.2.min.js"&gt;&lt;/script&gt;

                &lt;script type="text/javascript" src="js/easyTooltip.js"&gt;&lt;/script&gt;

                &lt;script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"&gt;&lt;/script&gt;

                &lt;!-- End of Libraries --&gt;   

        &lt;/head&gt;

        &lt;body&gt;

        &lt;div id="container"&gt;

                &lt;div class="logo"&gt;

                        &lt;a href="#"&gt;&lt;img src="assets/logo.png" alt="" /&gt;&lt;/a&gt;

                &lt;/div&gt;

                &lt;div id="box"&gt;

                     登录失败！请检查用户或者密码!&lt;br&gt;

                  &lt;a href="login.jsp"&gt;返回登录&lt;/a&gt;  

                &lt;/div&gt;

        &lt;/div&gt;

        &lt;/body&gt;

&lt;/html&gt;

四 测试

![](http://dl2.iteye.com/upload/attachment/0127/3646/b06dba46-05b9-378c-8f89-62afa8af1fbf.png)