Web登录界面、ServletContext、登录次数计数器

summarize

Servlet生命周期:

创建:启动服务器,第一次访问Servlet创建对象    销毁:关闭服务器或者移除服务器

ServletContext 承接上下文的   封装整个web项目,一个web只有一个ServletContext  一个web有多个Servlet对象

Servlet 两个方法 

1.getRealPath:根据相对路径 得到绝对路径

2.setAttribute 域里面设置值,getAttribute  域里面取值

创建Servlet配置

new一个dynamic(动态)

注意:version:一定择 2.5  不然不会自动创建 web.xml

好处:src下直接创建一个Servlet,会自动把web.xml配好

缺点:只会自动创建web.xml,不会自动删除 。但如果把Servlet删除了,web.xml是不会自动删除,若在建一个同名的Servlet会冲突。

 快捷键添加Servlet模板

windows--preferences--java--Editor--Templates--New

使用:   敲入  ser  +alt+/

package ${enclosing_package};
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ${primary_type_name} extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().write("hello sensen...");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

 JSP登录功能

 步骤:

1.WebContent下new一个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 here</title>
</head>
<body>
<!-- 建一个form表单 -->
    <form action="/WEB04/LoginServlet" method="post"><!-- 提交方式:一般写post安全 -->
        用户名:<input type="text" name="username"><br>
        用户名:<input type="password" name="pwd"><br>
        <input type="submit" value="登录">
    </form>
</body>
</html>

 2.建包  dao、service、tools、web

3.导jar驱动包:WebContent--WEB-INF--lib

4.在web层,建一个名为LoginServlet,然后Ctrl+a 全删。输入Servlet模板

package com.oracle.web;

import java.io.IOException;

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

import com.oracle.service.UserService;

public class LoginServlet extends HttpServlet {
    private UserService userService=new UserService();
    public void init() throws ServletException {
//        获取ServletContext对象
        ServletContext context=getServletContext();
//        定义计数器
        int count=0;
//        将计数器存入ServletContext对象 中 
        context.setAttribute("count", count);
    }    
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String uname=request.getParameter("username");
        //获取密码
        String pwd=request.getParameter("pwd");
        //调用service登录方法
        int row=userService.loginUser(uname, pwd);
        if(row>0){
            //登录成功
//            获取ServletContext对象
            ServletContext context=getServletContext();
            int count=(int)context.getAttribute("count");
            count++;
            context.setAttribute("count",count);
            response.getWriter().write("you are the person success");
        }else{
            //登录失败
            response.getWriter().write("fail");
        }        
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

 

 pm:

ServletContext对象

1.什么是ServletContext对象

代表是一个web应用的环境(上下文)对象,ServletContext对象内部封装是该web应用的信息,ServletContext对象一个web应用只有一个

问题:一个web应用有几个servlet对象?----多个

ServletContext对象的生命周期

创建:该web应用被加载(服务器启动或发布web应用(前提,服务器启动状 态))

销毁web应用被卸载服务器关闭,移除该web应用)

2.获得ServletContext对象

1)Servlet的init方法中获得ServletConfig

ServletContext servletContext = config.getServletContext();

 2)

ServletContext servletContext = this.getServletContext();

四、ServletContext的作用

1、获得web应用全局的初始化参数;

(1)获得web应用中任何资源的绝对路径(服务器上的绝对路径)重要 重要 重要

(给一个服务器的相对路径,得到一个在服务器上的绝对路径 getRealPath方法

String path = context.getRealPath(相对于该web应用的相对地址);

 

注意:在web项目中 分别在不同目录建立 src下a.txt、webContent下 b.txt、WEB-INF下 c.txt、在项目下d.txt 

当启动服务器时,a、b、c.txt 会分别映射到服务器目录相对  但是没有项目下的d.txt 是不会显示出来,所以尽量静态放WebContent,代码放src

代码展示:

public class Servlet01 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext context =getServletContext();
        //获取相对于服务器的相对路径获取绝对路径
        String patha=context.getRealPath("WEB-INF/classes/a.txt");
        String pathb=context.getRealPath("b.txt");
        String pathc=context.getRealPath("WEB-INF/c.txt");
        //d.txt创建在WEB04文件下,不会在服务器上找到的。以后静态资源创建在WebContent下,项目文件、配置文件在src下
        System.out.println(patha);
        System.out.println(pathb);
        System.out.println(pathc);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

 运行结果: 给一个相对路径,获取服务器的绝对路径

(2) ServletContext是一个域对象(存储数据的区域):

原理图

ServletContext域对象的作用范围:整个web

(所有的web资源都可以随意向 ServletContext 域中存取数据,数据可以分享)。

域对象的通用方法: 展示代码  往域里存值 从域里取值

setAtrribute(String name,Object obj);
getAttribute(String name);
removeAttribute(String name);

建立两个Servlet 02、03 在02里面设置值,从03里面取出来

public class Servlet02 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext context=getServletContext();    
        //往ServletContext域中设置值
        context.setAttribute("name", "zs");        
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
public class Serlvlet03 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext context=getServletContext();
        //取ServletContext域中的值
        String name=(String)context.getAttribute("name");
        response.getWriter().write(name);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

运行顺序:先访问 02 往域里存值  在访问 03从域里取值

结果

显示登录计数器 (登录成功显示登录的次数)

 在Web层的LoginServlet

public class LoginServlet extends HttpServlet {
    private UserService userService=new UserService();
    public void init() throws ServletException {
//        获取ServletContext对象
        ServletContext context=getServletContext();
//        定义计数器
        int count=0;
//        将计数器存入ServletContext对象 中 
        context.setAttribute("count", count);
    }    
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String uname=request.getParameter("username");
        //获取密码
        String pwd=request.getParameter("pwd");
        //调用service登录方法
        int row=userService.loginUser(uname, pwd);
        if(row>0){
            //登录成功
//            获取ServletContext对象
            ServletContext context=getServletContext();
            int count=(int)context.getAttribute("count");
            count++;
            context.setAttribute("count",count);
            response.getWriter().write("you are the "+count+" person success");
        }else{
            //登录失败
            response.getWriter().write("fail");
        }        
    }

运行效果

刷新一次就会记录一次

原文地址:https://www.cnblogs.com/zs0322/p/11117209.html