JSP登录中Session的用法

JSP登录中Session的用法

登录页面

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

Insert title here

账号:

密码:

检测账号密码以及设置session的IndexServlet

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

/**

* Servlet implementation class IndexServlet

*/

@WebServlet("/IndexServlet")

public class IndexServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

/**

* @see HttpServlet#HttpServlet()

*/

public IndexServlet() {

super();

// TODO Auto-generated constructor stub

}

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

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

// TODO Auto-generated method stub

response.getWriter().append("Served at: ").append(request.getContextPath());

}

/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

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

// TODO Auto-generated method stub

request.setCharacterEncoding("utf-8");

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

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

String path = request.getContextPath();

HttpSession session=request.getSession();

if ("1".equals(user) && "1".equals(password)) {

session.setAttribute("name", user);

response.sendRedirect(path + "/success.jsp");

}else{

response.sendRedirect(path + "/Index.jsp");

}

}

}

成功登录页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

%>

<%

Object name = session.getAttribute("name");

if(name==null){

response.sendRedirect(path+"/Index.jsp");

}

%>

成功页面

恭喜你,骚年,<%=session.getAttribute("name") %>,成功登陆了!

注销

注销功能的jsp

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

Insert title here

<%

String path = request.getContextPath();

%>

<%

session.removeAttribute("name");

response.sendRedirect(path+"/Index.jsp");

%>

相关推荐

制冷剂多长时间补充一次?制冷剂多长时间加一次?
今年这 12 款 App 是我们眼中的年度应用
365淘房APP官网下载

今年这 12 款 App 是我们眼中的年度应用

📅 06-27 👁️ 8578
向日葵远程软件收费版与免费版的区别分析
365bet官网欧洲

向日葵远程软件收费版与免费版的区别分析

📅 06-30 👁️ 9952
瘰疬是什么病呢?
365bet官网欧洲

瘰疬是什么病呢?

📅 06-28 👁️ 842
黑户有逾期哪里能借到钱啊急用?精选10个征信黑、大量逾期能借到钱的平台
华数宽带好用吗?看完这篇文章你就知道了
365bet官网欧洲

华数宽带好用吗?看完这篇文章你就知道了

📅 06-30 👁️ 5638
华数宽带好用吗?看完这篇文章你就知道了
365bet官网欧洲

华数宽带好用吗?看完这篇文章你就知道了

📅 06-30 👁️ 5638
资治通鉴
365淘房APP官网下载

资治通鉴

📅 06-28 👁️ 7700
死亡之组
365淘房APP官网下载

死亡之组

📅 06-30 👁️ 8473