Session用户登陆后再次登录免密登陆

主要是Eclipse实现Javaweb页面登陆后不需要再次登录的功能,后续会实现10天内免登录功能。

先上效果图,在浏览器登录后,另输入在地址栏进index.jsp可以直接进入登录名页面。

若关闭浏览器再次打开地址栏进index.jsp则会跳转到login.jsp页面要救扽登录账号和密码。

此项目账号和密码在ActionServlet中均设置为固定值111。

login.jsp代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="login.do" method="post">
	用户名:<input name="uname"/><br><br>
	密码:<input name="pwd" type="password"/><br><br>
	<input type="submit" value="登录"/> 
	</form>
</body>
</html>

index.jsp代码:

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

<%
	Object uname = session.getAttribute("uname");
if(uname == null) {
	response.sendRedirect("login.jsp");
	return;
}
%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>首页</h3>欢迎你:<%=uname.toString() %><br>
<a href="logout.do">登出</a>
</body>
</html>

ActionServlet代码:

package web;

import java.io.IOException;
import java.io.PrintWriter;

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 javax.servlet.http.HttpSession;

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

	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");//发送过来的请求用utf-8解码
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String uri = request.getRequestURI();
		String action = uri.substring(uri.lastIndexOf("/") + 1,uri.lastIndexOf("."));
		HttpSession session = request.getSession();//创建Session
		System.out.println(session.getId());
		//判读动作是否为登录
		if(action.equals("login")) {
			String name = request.getParameter("uname");
			String pwd =  request.getParameter("pwd");
			//用户名密码正确
			if(name.equals("111") && pwd.equals("111")) {
				session.setAttribute("uname", name);//在Session中保存用户名
				response.sendRedirect("index.jsp");
			} else {
				request.setAttribute("msg", "用户名或密码错误");
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}
			
		} else if (action.equals("logout")) {
			//session失效
			session.invalidate();
			response.sendRedirect("login.jsp");
		}
	}

}

CountServlet代码:

package web;

import java.io.IOException;
import java.io.PrintWriter;

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 javax.servlet.http.HttpSession;

@WebServlet("/CountServlet")
public class CountServlet extends HttpServlet {
	public CountServlet(){
		
	}
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		HttpSession session = request.getSession();
		System.out.println(session.getId());
		
		Integer count = (Integer)session.getAttribute("count");
		if(count==null) {
			count=1;
		} else {
			count++;
		}
		session.setAttribute("count", count);
		
		out.println("这是第"+count+"次访问");
		out.close();
	}

}

web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<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_3_1.xsd" metadata-complete="true" version="3.1">
  <servlet>
    <servlet-name>CountServlet</servlet-name>
    <servlet-class>web.CountServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>CountServlet</servlet-name>
    <url-pattern>/count</url-pattern>
  </servlet-mapping>
  
    <servlet>
    <servlet-name>ActionServlet</servlet-name>
    <servlet-class>web.ActionServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ActionServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  
</web-app>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值