seesion完成简单的登陆登出

首先是 login.html

<!DOCTYPE html>
<html>
<head>
    <title>后台登陆系统</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="padding-all">
    <div class="header">
        <h1><img src="images/logo.png" alt=" "> 管理员登陆</h1>
    </div>

    <div class="design-w3l">
        <div class="mail-form-agile">
            <form action="LoginServlet" method="post">
                <input type="text" name="name" placeholder="账号" required=""/>
                <input type="password"  name="password" class="LoginPadding" placeholder="密码" required=""/>
                <input type="text" name="check_num" placeholder="验证码" required=""/>
                <img alt="验证码" src="CheckServlet">
                <input type="submit" value="登陆">

            </form>
        </div>
        <div class="clear"> </div>
    </div>
</div>
</body>
</html>

然后是LoginServlet

package com.caizhen.Servlet;

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;

import com.caizhen.pojo.User;

/**
 * Servlet implementation class login
 */
@WebServlet("/login")
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 {
		String name=request.getParameter("name");
		String password=request.getParameter("password");
		if("caizhen".equals(name)&&"123456".equals(password)) {
			User user=new User();
			user.setName(name);
			user.setPassword(password);
			request.getSession().setAttribute("user", user);
		    response.sendRedirect("success.html");
		
	}else {
		response.sendRedirect("error.html");
	}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

然后是登陆成功的界面

success.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>恭喜成功登陆 <a href="LogoutServlet">退出</a>
</body>
</html>

然后是登出操作

package com.caizhen.Servlet;

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;
@WebServlet("/LogoutServlet")
public class LogoutServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public LogoutServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getSession().removeAttribute("user");
response.sendRedirect("login.html");
	
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		doGet(request, response);
	}

}

然后是web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.caizhen.Servlet.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/webapp/LoginServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>LogoutServlet</servlet-name>
    <servlet-class>com.caizhen.Servlet.LogoutServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LogoutServlet</servlet-name>
    <url-pattern>/webapp/LogoutServlet</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
</web-app>

User.java

package com.caizhen.pojo;

public class User {
	private String password;
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}

login.html的样式文件

html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,dl,dt,dd,ol,nav ul,nav li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline;}
article, aside, details, figcaption, figure,footer, header, hgroup, menu, nav, section {display: block;}
ol,ul{list-style:none;margin:0px;padding:0px;}
blockquote,q{quotes:none;}
blockquote:before,blockquote:after,q:before,q:after{content:'';content:none;}
table{border-collapse:collapse;border-spacing:0;}
/* start editing from here */
a{text-decoration:none;}
.txt-rt{text-align:right;}/* text align right */
.txt-lt{text-align:left;}/* text align left */
.txt-center{text-align:center;}/* text align center */
.float-rt{float:right;}/* float right */
.float-lt{float:left;}/* float left */
.clear{clear:both;}/* clear float */
.pos-relative{position:relative;}/* Position Relative */
.pos-absolute{position:absolute;}/* Position Absolute */
.vertical-base{	vertical-align:baseline;}/* vertical align baseline */
.vertical-top{	vertical-align:top;}/* vertical align top */
nav.vertical ul li{	display:block;}/* vertical menu */
nav.horizontal ul li{	display: inline-block;}/* horizontal menu */
img{max-width:100%;}
/*end reset*/
/****-----start-body----****/
body{
text-align: center;
background: url("../images/1.jpg") 0px 0px no-repeat;
background-size:cover;
background-attachment: fixed;
}

@font-face{
font-family: "Audiowide-Regular";
src:url(../fonts/Audiowide-Regular.ttf);
}
@font-face{
font-family: "Asap-Regular";
src:url(../fonts/Asap-Regular.ttf);
}
.padding-all{
padding:100px;
}

.header {
text-align: center;
padding-bottom:75px;
}
.header h1 img{
width:7%;
}

p ,a{
font-family: "Asap-Regular";
}
.header h1 {
font-size:50px;
color: #fff;
font-family: "Audiowide-Regular";
letter-spacing:3px;
margin:0 auto;
}


.design-w3l{
width:36%;
margin:0 auto;
}

.mail-form-agile {
    padding: 50px 40px;
    text-align: center;
    margin:0px;
    background: rgba(23, 218, 218, 0.18);
    color: #000;
	margin:0 auto;
}

.padding {
margin: 5px 0 30px;
}

.LoginPadding {
    margin: 20px 0 30px;
}

.mail-form-agile input[type="text"], .mail-form-agile input[type="password"] {
padding: 13px 10px;
width: 92.5%;
font-size: 16px;
outline: none;
background:transparent;
border:0px;
border-bottom: 1px solid #fff;
border-radius: 0px;
font-family: "Asap-Regular";
letter-spacing:1.6px;
color:#fff;
}
::-webkit-input-placeholder{
   color:#b4b0b0 !important;
   font-weight:400;
}
.mail-form-agile input[type="submit"]{
font-size: 18px;
padding: 10px 20px;
letter-spacing:1.2px;
border: none;
text-transform: capitalize;
outline: none;
border-radius: 4px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
background: #D65B88;
color: #fff;
cursor: pointer;
margin: 0 auto;
font-family: "Asap-Regular";
-webkit-transition-duration: 0.9s;
transition-duration: 0.9s;
}

.mail-form-agile input[type="submit"]:hover {
 -webkit-transition-duration: 0.9s;
 transition-duration: 0.9s;
 background:rgba(91, 157, 214, 0.76);
}

.mail-form-agile div{
    display: inline;
    font-size: 18px;
    padding: 11px 20px;
    letter-spacing:1.2px;
    border: none;
    text-transform: capitalize;
    outline: none;
    border-radius: 4px;
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    background: #D65B88;
    color: #fff;
    cursor: pointer;
    margin: 0 auto;
    font-family: "Asap-Regular";
    -webkit-transition-duration: 0.9s;
    transition-duration: 0.9s;
}

.mail-form-agile div:hover {
    -webkit-transition-duration: 0.9s;
    transition-duration: 0.9s;
    background:rgba(91, 157, 214, 0.76);
}

.footer{
text-align: center;
padding-top:75px;
letter-spacing:1.6px;
line-height:22px;
}
.footer p {
font-size: 16px;
color: #fff;
margin: 0px;
letter-spacing:1.4px;
}
.footer p a {
color:#D65B88;
font-weight:blod;
-webkit-transition-duration: 0.8s;
transition-duration: 0.8s;
}
.footer p a:hover {
color: #51cbe1;
 -webkit-transition-duration: 0.8s;
 transition-duration: 0.8s;
}

猜你喜欢

转载自blog.csdn.net/cainame/article/details/81227689
今日推荐