seesion完成简单的登陆登出

首先是 login.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





Insert title here


恭喜成功登陆 退出

然后是登出操作

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



  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
    LoginServlet
    com.caizhen.Servlet.LoginServlet
  
  
    LoginServlet
    /webapp/LoginServlet
  
  
    LogoutServlet
    com.caizhen.Servlet.LogoutServlet
  
  
    LogoutServlet
    /webapp/LogoutServlet
  
  
    30
  

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

 

你可能感兴趣的:(seesion完成简单的登陆登出)