---
title: "JSP实现用户登录"
author: "cakin24"
date: "2017-10-02T07:26:33Z"
tags: ["JSP", "用户登录"]
categories: ["编程语言"]
slug: "jsp"
draft: false
lastmod: "2017-10-02T07:26:33Z"
id: 2395228
url: "https://cakin24.iteye.com/blog/2395228"
---

一 需求

用户名admin，密码admin，登录成功则使用服务器内部转发到login\_success.jsp页面，并且提示登录成功的用户名。如果登录失败则请求重定向到login\_failure.jsp页面。

 

二代码

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="dologin.jsp" 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 dologin.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+"/";

  String username ="";

  String password ="";

  request.setCharacterEncoding("utf-8");//防止中文乱码

 

  username = request.getParameter("username");

  password = request.getParameter("password");

 

  //如果用户和密码都等于admin,则登录成功

  if("admin".equals(username)&&"admin".equals(password))

  {

     session.setAttribute("loginUser", username);

     request.getRequestDispatcher("login\_success.jsp").forward(request, response);

    

  }

  else

  {

     response.sendRedirect("login\_failure.jsp");

  }

%&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;

4 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;

 

三运行效果

![](http://dl2.iteye.com/upload/attachment/0127/2237/b0a5c9a8-bfad-3d0d-824e-c5b783f11e8b.png)