访问页面时,判断用户是否登录,若没有登录,将跳转到登录页面(一)

当用户没有登录,直接访问servlet容器时,就会跳转到登陆页面去让用户登录。(还可以用filter过滤器进行统一过滤,那种方法很简单,建议使用过滤器拦截吗,见访问页面时,判断用户是否登录,若没有登录,将跳转到登录页面(二)

 

1.登录页面login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>

<% 
String authority = (String)request.getAttribute("authority");
%>
<% out.print(authority); %>
<form action="UserLoginServlet" method="post">
    username:<input type="text" value="<%=null == request.getAttribute("username")?"" : request.getAttribute("username") %>" name="username"><br>
    password:<input type="text" name="password"><br>
    
    authority:<select  name="authority">
    
        <option value="1" <%="1".equals(authority)?"selected = selected" : "" %>>common useroption>
        <option value="2" <%="2".equals(authority)?"selected = selected" : "" %>>adminstratoroption>
    select>
    <br>
    <input type="submit">


    
form>


>

<%String error = request.getParameter("error");

    String authority1 = request.getParameter("authority");
    
%>




<form action="UserLoginServlet" method="post">
    username:<input type="text" value="<%=null == request.getParameter("username")?"" : request.getParameter("username") %>" name="username"><br>
    password:<input type="text" name="password"><br>
    
    authority:<select  name="authority">
    
        <option value="1" <%="1".equals(authority1)?"selected = selected" : "" %>>common useroption>
        <option value="2" <%="2".equals(authority1)?"selected = selected" : "" %>>adminstratoroption>
    select>
    <br>
    <input type="submit">


    
form>
body>
html>
View Code

2.web.xml文件配置

 
  
  <servlet>
      <servlet-name>UserLoginServletservlet-name>
      <servlet-class>com.servlet.UserLoginServletservlet-class>
  servlet>
  
  <servlet-mapping>
      <servlet-name>UserLoginServletservlet-name>
      <url-pattern>/session/UserLoginServleturl-pattern>
  servlet-mapping>
  
  <servlet>
      <servlet-name>QueryServletservlet-name>
      <servlet-class>com.servlet.QueryServletservlet-class>
  servlet>
  
  <servlet-mapping>
      <servlet-name>QueryServletservlet-name>
      <url-pattern>/session/QueryServleturl-pattern>
  servlet-mapping>
  
  
web.xml
3.servlet( com.servlet.LoginServlet
package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

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

public class LoginServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
       String username = req.getParameter("username");
       String password = req.getParameter("password");
       String repassword = req.getParameter("repassword");
       int age = Integer.parseInt(req.getParameter("age"));
       
       String result = "";
       if(age > 18)
       {
           result = "success";
       }
       else
       {
           result += "age <= 18";
       }
       if(!password.equals(repassword))
       {
           result += "password != repassword";
       }
       
       req.setAttribute("username", username);
       req.setAttribute("age", age);
       
       //c重定向
       resp.sendRedirect("result.jsp");
       //转发
      // RequestDispatcher rd = req.getRequestDispatcher("result.jsp");
       
    
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        doGet(req, resp);
    }
}
LoginServlet

4.主页面(index.jsp)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="com.javabean.User" %>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<a href="QueryServlet">querya><br>
<% 
User user = (User)session.getAttribute("user");
if(null == user)
{
    response.sendRedirect("login.jsp");
}
    if("2".equals(((User)session.getAttribute("user")).getAuthority())){
            
%>
<a href="UpdateServlet">updatea>

<%} %>
body>
html>
index.jsp

5.QueryServlelt.java

package com.servlet;

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;

import com.javabean.User;

public class QueryServlet extends HttpServlet
{
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        HttpSession session = req.getSession();
        
        if(null == session.getAttribute("user"))
        {
            resp.sendRedirect("login.jsp");
            return ;
        }
        User user = (User)session.getAttribute("user");
        
        if("1".equals(user.getAuthority()))//表示普通用户
        {
            System.out.println("sihbai");
        }else
        {
            System.out.println("chenggong");
        }
        
    }
}
QueryServlet

6.UpdateServlet.java

package com.servlet;

import java.io.IOException;

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

import com.javabean.User;

public class UserLoginServlet extends HttpServlet
{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        String username = (String)req.getParameter("username");
        String password = (String)req.getParameter("password");
        String authority = (String)req.getParameter("authority");
        
        if("1".equals(authority))
        {
            //登录的是普通用户
            if("zhangsan".equals(username) && "123".equals(password))
            {
                //将用户的信息放置到session中
                User user = new User();
                user.setUsername(username);
                user.setPassword(password);
                user.setAuthority(authority);
                
                HttpSession session = req.getSession();
                session.setAttribute("user", user);
                req.getRequestDispatcher("index.jsp").forward(req, resp);
                return;
            }
            
        }
        
        else  if("2".equals(authority))
        {
             //登录的是系统管理员
            if("lisi".equals(username) && "456".equals(password))
            {
                //将用户的信息放置到session中
                User user = new User();
                user.setUsername(username);
                user.setPassword(password);
                user.setAuthority(authority);
                
                HttpSession session = req.getSession();
                session.setAttribute("user", user);
                req.getRequestDispatcher("index.jsp").forward(req, resp);
                return;
                
            }
        }
        //表示登录失败
      
        System.out.println("shibai");
        
        //请求转发模式
//            req.setAttribute("username", username);
//            req.setAttribute("authority",authority);
//            RequestDispatcher rd = req.getRequestDispatcher("login.jsp");
//            rd.forward(req, resp);
        
        //重定向
        resp.sendRedirect("login.jsp?error=true&username="+username+"&authority="+authority);
    
    }
}
UpdateServlet

 

 

转载于:https://www.cnblogs.com/feiguo/p/3802356.html

你可能感兴趣的:(访问页面时,判断用户是否登录,若没有登录,将跳转到登录页面(一))