欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

session会话跟踪

程序员文章站 2022-03-14 23:48:20
...

首先,index页面有 登录/访问test 两个链接,但是在没有登陆的状态下是不能够进入test里的,这个时候会强制用户登录。

index.html:
< a href = “login”>登录< /a>< br/>
< a href = “Test”>访问test< /a>< br/>

servlet:
1、登录servlet(loginservlet)
主要代码:
String name = req.getParameter(“name”);
String psw = req.getParameter(“password”);
//判断该用户是否在数据库内
//这里假设name=“admin” password = “12345”
if(“admin”.equals(name)&&“12345”.equals(password)){//设置session
HttpSession session = req.getSession();
session.setAttribute(“User”,name);
resp.setContentType(“text/html;charset=UTF-8”);
resp.getWriter().write(“欢迎您,”+name+"< a href=‘index.html’>返回< /a>");

}else{//重新登录
resp.sendRedirct(“login.html”);
}

package com.sy;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class loginservlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
		throws ServletException, IOException {
	req.setCharacterEncoding("UTF-8");
	String name = req.getParameter("name");
	String password = req.getParameter("password");
	if("admin".equals(name) && "12345".equals(password)){
		HttpSession session = req.getSession();
		session.setAttribute("User", name);
		//session.setAttribute("password", password);
		//1、获取输出流
		resp.setContentType("text/html;charset=UTF-8");
		resp.getWriter().write("欢迎您,"+name+"!<a href='index.html'>返回首页</a>");
	}else{
		resp.sendRedirect("login.html");
	}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
		throws ServletException, IOException {
	doPost(req,resp);
}
}

2、testServlet
主要代码:
首先获取req的session
HttpSession session = req.getSession(false);
if(session == null || session.getAttribute(“User”) == null){//用户没有登录(session里没有该User)
resp.sendRedirect(“login”);//强制回到登录界面
}else{
String name = (String)session.getAttribute(“User”);
resp.setContentType(“text/html;charset=UTF-8”);
resp.getWriter().write(“welcome “+name+”!返回”)
}

package com.sy;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class testservlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
		throws ServletException, IOException {
	HttpSession session = req.getSession(false);
	if(session == null || session.getAttribute("User") == null){
		System.out.println("只有登录过后才可以访问噢!");
		resp.sendRedirect("login");
	}else{
		String name = (String)session.getAttribute("User");
		resp.setContentType("text/html;charset=UTF-8");
		resp.getWriter().write("welcome,"+name+"!<a href='index.html'>返回</a>");
	}
}
@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		doPost(req,resp);
	}
}

web.xml:

<servlet>
  <servlet-name>loginservlet</servlet-name>
  <servlet-class>com.sy.loginservlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>loginservlet</servlet-name>
  <url-pattern>/login</url-pattern>
  </servlet-mapping>
  
  <servlet>
  <servlet-name>testservlet</servlet-name>
  <servlet-class>com.sy.testservlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>testservlet</servlet-name>
  <url-pattern>/Test</url-pattern>
  </servlet-mapping>