8.6 项目day03 servlet计数器,进行访问人数累加

上下文

 

四大作用域范围

 

 

源码:

 

\

结构:

源码:

LoginServlet  ,TestServlet

package com.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	//getServletContext()方法得到servlet上下文的引用
	System.out.println("LoginServlet页面的ServletContext:"+getServletContext().toString());
	
	// request.getParameter() 方法来获取表单参数的值。
	String uname = request.getParameter("uname");
	String pwd = request.getParameter("pwd");
	
	//上下文定义为context
	ServletContext context = getServletContext();
	
	//如果uname值为admin ,pwd值为admin
	if("admin".equals(uname)&& "admin".equals(pwd)){
		
		//定义一个计数count强转为string类型
		//Object getAttribute(String name)    返回servlet上下文中具有指定名字的对象(全局对象)
		String count = (String) context.getAttribute("count");
		
		Integer c = 0;//初始访问人数为0
		
		if(count == null){//如果无人访问,则当前设置为第一位访问者
			c=1; 
		}else{//如果先前有人访问,则在原基础上加一
			
			//把字符串count变成十进制表示的整数
			c = Integer.parseInt(count);
			c++;
		}
		//void setAttribute(String name,Object obj)    设置servlet上下文中具有指定名字的对象。
		
		context.setAttribute("count", c.toString());//将字符串count与c对应
		
		request.setAttribute("uname", uname);//将uname值赋予字符串uname
		
		//ServletContext.getRequestDispatcher(String url)中的url只能使用绝对路径; 
		//ServletRequest.getRequestDispatcher(String url)中的url可以使用相对路径。因为ServletRequest具有相对路径的概念;
		/*
		 * getRequestDispatcher()包含两个方法,分别是请求转发和请求包含。
			如下:
			RequestDispatcher rd = request.getRequestDispatcher("/MyServlet");
			请求转发: rd.forward( request , response );
			请求包含: rd.include( request  , response);
		 */
		request.getRequestDispatcher("success.jsp").forward(request, response);;//请求转发
	}else{
		request.setAttribute("msg", "用户或密码错误");//将msg的内容设置为:用户或密码错误
		request.getRequestDispatcher("login.jsp").forward(request, response);;//请求转发
	}
	
	}

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

}
package com.servlet;

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;

/**
 * Servlet implementation class TestServlet
 */

public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestServlet() {
        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
		System.out.println("TestServlet页面的ServletContext:"+getServletContext().toString());
	}

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

}
<%@ 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>登录页面</title>
<style type="text/css">
	span{
	color:red;
	}
</style>
</head>
<body>
<!-- onsubmit会调用一个函数来处理事件 -->
<form action="login" onsubmit="return login();" method="post">
<%
String msg =(String)request.getAttribute("msg");
if(msg != null){
%>
	<p>错误消息提示:<%=msg %></p>
	<% }%>
	用户名:<input type="text" id="uname" name="uname" /><span id ="username"></span><br/>
	密码:<input  type="password" id="pwd" name="pwd"><span id="password"></span><br/>
	<input type="submit" values="登录">
	
</form>
<script type="text/javascript">
	function login(){
		
		var username = document.getElementById("uname");
		var u_val = username.value;
		
		var password = document.getElementById("pwd");
		var p_val=password.value;
		
		if(u_val==null || u_val==""){
			document.getElementById("username").innerHTML="用户名不能为空喔~!";
			return false;
		}
		if(p_val=null || p_val==""){
			document.getElementById("password").innerHTML="密码不能为空喔~!";
			return false;
		}
		
		
		
	}
</script>



</body>
</html>
<%@ 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>登录成功页面</title>
</head>
<body>
<p> 欢迎<%=request.getParameter("uname") %>的到来!!</p>
<p>您是第<%=application.getAttribute("count") %>位访问者!</p>

</body>
</html>

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>day03cp</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>
    <servlet-name>login</servlet-name>
    <servlet-class>com.servlet.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>
  
  <servlet>
  <servlet-name>application</servlet-name>
  <servlet-class>com.servlet.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
 <servlet-name>application</servlet-name>
 <url-pattern>/testServlet</url-pattern>
  </servlet-mapping>
</web-app>

运行结果

LoginServlet页面的ServletContext:org.apache.catalina.core.ApplicationContextFacade@3bb009af
LoginServlet页面的ServletContext:org.apache.catalina.core.ApplicationContextFacade@3bb009af
TestServlet页面的ServletContext:org.apache.catalina.core.ApplicationContextFacade@3bb009af

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一身正气z

打赏随心就好

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值