使用ServletContext存取数据

LoginServlet:
 

package com.yuming.servlet;

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

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


public class LoginServlet extends HttpServlet {



	/**
	 * request : 包含请求的信息
	 * 
	 * response : 响应数据给浏览器, 就靠这个对象
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.获取数据
		String usernName = request.getParameter("username");
		String passWord = request.getParameter("password");
		System.out.println("usernName="+usernName+"--passWord="+passWord);
		//2.校验数据
		//向客户端输出内容
//		if("admin".equals(usernName) && "123456".equals(passWord)){
//			System.out.println("登录成功");
//		}else{
//			System.out.println("登录失败");
//		}
		PrintWriter pw = response.getWriter();
		if("admin".equals(usernName) && "123456".equals(passWord)){
			//pw.write("login success");
			
			//3. 成功的次数累加		
			//获取以前存的值 , 然后在旧的值基础上  + 1 
			Object obj = getServletContext().getAttribute("count");
			
			//默认就是0次
			int totalCount = 0;
			if(obj != null){
				int count = 0;
				totalCount = (int)count;
			}
			
			System.out.println("已知登录成功的次数是:"+totalCount);
			
			//给这个count赋新的值
			getServletContext().setAttribute("count", totalCount+1);
			
			//2. 跳转到成功的界面 
			//设置状态码? 302重新定位 状态码
			response.setStatus(302);
			//定位跳转的位置是哪一个页面。
			response.setHeader("Location", "success.html");
		}else{
			pw.write("login failed");
		}
	}


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

}

login.html:
 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	<h2>登录功能</h2>
	<form action="LoginServlet" method="get">
		用户名:<input type="text" name="username"/><br/>
		密    码:<input type="password" name="password"/><br/>
		<input type="submit" value="登录"/>
	</form>

</body>
</html>

success.html:
 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	<h2>登录成功进来的页面</h2>
	<a href="CountServlet">登录成功后的次数</a>

</body>
</html>

CountServlet:
 

package com.yuming.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CountServlet
 */
public class CountServlet extends HttpServlet {
	

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1. 取值
		int count = (Integer) getServletContext().getAttribute("count");
				
		//2. 输出到界面
				
		response.getWriter().write("login success count ===:"+count);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>ServletLogin</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<servlet>
		<description></description>
		<display-name>LoginServlet</display-name>
		<servlet-name>LoginServlet</servlet-name>
		<servlet-class>com.yuming.servlet.LoginServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>LoginServlet</servlet-name>
		<url-pattern>/LoginServlet</url-pattern>
	</servlet-mapping>
	<servlet>
		<description></description>
		<display-name>CountServlet</display-name>
		<servlet-name>CountServlet</servlet-name>
		<servlet-class>com.yuming.servlet.CountServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>CountServlet</servlet-name>
		<url-pattern>/CountServlet</url-pattern>
	</servlet-mapping>
</web-app>


### 使用ServletContext存取数据

1. 定义一个登陆的html页面, 定义一个form表单.   login.html

2. 定义一个Servlet,名为LoginServlet

3. 针对成功或者失败,进行判断,然后跳转到不一样的网页

###ServletContext存取值分析

 

##细节:

        <!--     
        A路径: Servlet的路径
            http://localhost:8080/Demo4/login
        
        B路径: 当前这个html的路径:
            http://localhost:8080/Demo4/login.html -->
        
        
        <form action="login" method="get">
            账号:<input type="text" name="username"/><br>
            密码:<input type="password" name="password"/><br>
            <input type="submit" value="登录"/>
        </form>

###ServletContext 何时创建, 何时销毁?

服务器启动的时候,会为托管的每一个web应用程序,创建一个ServletContext对象

从服务器移除托管,或者是关闭服务器。 

* ServletContext 的作用范围

> 只要在这个项目里面,都可以取。 只要同一个项目。 A项目 存, 在B项目取,是取不到的? ServletContext对象不同。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值