admin 管理员组

文章数量: 1087649

欢迎来到Jsp编程课时十——Servlect+Jsp基础回顾。

欢迎来到Jsp编程课时四 

 


 


 


 模块一:Servlect的注册回顾与周期回顾。

知识点:

 


代码编译:

package com.servlet;import java.io.IOException;import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;public class OneServlet implements Servlet  {public OneServlet() {System.out.println("OneServlet的抽象方法");}@Overridepublic void destroy() {System.err.println("OneServlet释放内存的方法内容");}@Overridepublic ServletConfig getServletConfig() {System.out.println("获得静态数据的方法");return null;}@Overridepublic String getServletInfo() {// TODO Auto-generated method stubSystem.out.println("getServletInfo() 的 Servlect的方法内容");return null;}@Overridepublic void init(ServletConfig arg0) throws ServletException {// TODO Auto-generated method stubSystem.out.println("Servlect init 的方法内容");}@Overridepublic void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {// TODO Auto-generated method stub//等待浏览器访问System.out.println("servlect处于运行状态");}}

wel.xml文件

 模块二:Servlect对象@1ServletConfig静态数据

package com.Servlet;import java.io.IOException;import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/*** Servlet implementation class Servlet2*/
@WebServlet(urlPatterns = "/B",initParams = {@WebInitParam(name="aaaa",value="张三"),@WebInitParam(name="age",value="23"),@WebInitParam(name="height",value="179cm"),@WebInitParam(name="weight",value="60kg"),@WebInitParam(name="x",value="我是第二种获取静态数据的方式晕染")
})
public class Servlet2 extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public Servlet2() {System.out.println("Servlet2值一送到");}@Overridepublic void init() throws ServletException {// TODO Auto-generated method stubsuper.init();//定义五个静态数据ServletConfig config=getServletConfig();String a=config.getInitParameter("aaaa");String b=config.getInitParameter("age");String c=config.getInitParameter("height");String e=config.getInitParameter("weight");String f=config.getInitParameter("x");System.out.println(a+"===="+b+"#######"+c+"-----"+e);//String d=config.getInitParameter("money");System.out.println(d);System.out.println(e);System.out.println(f+"我是第二种获取静态数据的方式晕染");}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubresponse.getWriter().append("Served at: ").append(request.getContextPath());}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);}}

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xsi:schemaLocation=" .xsd" id="WebApp_ID" version="3.0"><display-name>Day03jsp</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>B.jsp</welcome-file><!-- <welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file> --></welcome-file-list><!--  --><!-- 在web.xml文件中定义静态数据的方式 --><servlet><servlet-name>Servlet1</servlet-name><servlet-class>com.Servlet.Servlet1</servlet-class><!-- 注意<init-param>写在里面 --><init-param><param-name>home</param-name><param-value>二家酒店</param-value></init-param><init-param><param-name>money</param-name><param-value>200万</param-value></init-param><init-param><param-name>h</param-name><param-value>173cm</param-value></init-param><init-param><param-name>a</param-name><param-value>我是静态数据的获取方式a</param-value></init-param></servlet><servlet-mapping><servlet-name>Servlet1</servlet-name><!--设置浏览器地址  --><url-pattern>/A</url-pattern><!-- 设置地址访问一定加/asd --></servlet-mapping><servlet><servlet-name>Servlet4 </servlet-name><servlet-class>com.Servlet.Servlet4</servlet-class><!--静态数据写人web.xml  --><init-param><param-name>h</param-name><param-value>10000</param-value></init-param><init-param><param-name>b</param-name><param-value>2000</param-value></init-param></servlet><servlet-mapping><servlet-name>Servlet4</servlet-name><url-pattern>/D</url-pattern></servlet-mapping></web-app>

模块三Servlect对象二@ServletContext.共享动态数据,文件数据,静态数据。


 运行图

 

 


 

package com.servlet;import java.io.IOException;import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
/*** Servlet implementation class Servlet1*/
@WebServlet(urlPatterns = "/ServletA",initParams = {//增加静态数据的第一个学生@WebInitParam(name="a",value="我是获取静态数据的方式")})
/*** 12/4回顾获取静态数据和学习动态数据的方式* @author MZFAITHDREAM**/
public class Servlet1 extends HttpServlet {private static final long serialVersionUID = 1L;/*** Default constructor. */public Servlet1() {// TODO Auto-generated constructor stub}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/@Overridepublic void init() throws ServletException {// TODO Auto-generated method stubsuper.init();//定义4个静态数据ServletConfig config=getServletConfig();String a=config.getInitParameter("a");System.out.println(a);}/** 浏览器none do-get请求* 第二个对象
ServletContext {con 上下文  }
tomcat 服务器为每个项目创建一个对象  ServletContext
1个web项目只有一个ServletContext对象
ServletContext 服务器开始创建   tomcat服务器 结束是 没有ServletContext 作用   给同一个项目的所有同一个项目共享数据。
1  共享静态数据
2  共享动态数据
3  共享文件数据* */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//设置 发送给浏览器的文本信息HTML文本格式//response.setContentType("text/html");response.setContentType("text/html");//设置发送给浏览器文本utf-8//response.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");//因为浏览器是使用get请求//所以Servlet do-get//servlet1相打篮球//1获取ServletContext对象ServletContext  sc=getServletContext();//获取web.xml文件中的镜头数据String a=sc.getInitParameter("lq");String c= (String) sc.getInitParameter("dt");//获得servlet4共享的排球String b= (String) sc.getAttribute("pq");String d= (String) sc.getAttribute("as");//删除排球sc.removeAttribute("pq");//给浏览器发送数据response.getWriter().append("<h1>Servlet1获得静态数据用来访问lq:"+a+"</h1><br>");response.getWriter().append("<h1>Servlet1获得静态数据用来访问dt:"+c+"</h1><br>");response.getWriter().append("<h1>response.getWriter().append方法打印内容</h1>");response.getWriter().append("<h1>Servlet4获得动态态数据用来访问pq:"+b+"</h1><br>");response.getWriter().append("<h1>Servlet4获得动态态数据用来访问as:"+d+"</h1><br>");}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);}}

package com.servlet;
/** ServletContext {con 上下文  }
tomcat 服务器为每个项目创建一个对象 ServletContext
1个web项目只有一个ServletContext对象
ServletContext 服务器开始创建    结束是 没有ServletContext 作用   同一个项目共享数据。
1  共享静态数据
2  共享动态数据 写在java代码
3  共享文件数据* 创建对象*  ServletContext sc=getServletContext();获取对象* sc.setAttribute("pq", "排球"); 接收对象 *	 删除对象 sc.removeAttribute("pq");*/
import java.io.IOException;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 Servlet4*/
@WebServlet("/Servlet4")
public class Servlet4 extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()* 由个人共享出来的动态数据*/public Servlet4() {super();// TODO Auto-generated constructor stub}/*** 创建对象 ServletContext sc=getServletContext();获取对象* sc.setAttribute("pq", "排球"); 接收对象 * 删除对象 sc.removeAttribute("pq");* * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//servlet4共享班级 共享数据//共享动态数据response.setContentType("text/html");//设置发送给浏览器文本utf-8//response.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");//获取ServletContext 对象ServletContext sc=getServletContext();//将排球共享数据 setAttribute方法共享数据。//在java中object是任何数据类型的父类。//身份的多种类型;向上转型sc.setAttribute("pq", "我是共享的数据内容排球");sc.setAttribute("as", "我是共享的数据as采用的是setAttribut获得");//身份的多种类型;向上转型(强制转型)//获得动态数据//疯狂java讲义//getimitParameter只能获得静态数据String  a= (String) sc.getAttribute("pq");String  b= (String) sc.getAttribute("as");response.getWriter().append("Servlet4获得动态数据 "+a);response.getWriter().append("Servlet4获得动态数据 "+b);}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);}}

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xsi:schemaLocation=" .xsd" id="WebApp_ID" version="3.0"><display-name>Day04JSp</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>B.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自己静态数据<init-param>--><!--动态数据的获取方式  --><context-param><param-name>lq</param-name><param-value>篮球</param-value></context-param><context-param><param-name>zq</param-name><param-value>足球</param-value></context-param><context-param><param-name>dt</param-name><param-value>恭喜你获得了我的动态数据的内容</param-value></context-param></web-app>

 


模块四Servlect对象三Response对象。

运行图(仔细观察) 


 

 代码编译

package com;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 Servlet1*/
@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public Servlet1() {super();System.out.println("Servlet1 在运行HttpServletRequest 的方法");// TODO Auto-generated constructor stub}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获得浏览器的请求,得到请求信息//得到请求的URL地址String a=request.getRequestURL().toString();System.out.println(a);//得到请求的数据String b=request.getRequestURI();System.out.println(b);//得到请求的URL地址中附带的参数String c=request.getQueryString();System.out.println(c);//得到来访者的IP地址String d=request.getRemoteAddr();System.out.println(d);//来访者的主机名String e=request.getRemoteHost();System.out.println(e);//来访者的端口号int f=request.getRemotePort();System.out.println(f);//得到请求URL地址时使用的方法(重要)String h=request.getMethod();System.out.println(h);//获取WEB服务器的IP地址String l=request.getLocalAddr();System.out.println(l);//获取WEB服务器的主机名String n=request.getLocalName();System.out.println(n);//获取WEB服务器的端口号int m=request.getLocalPort();System.out.println(m);}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);}}
package com;import java.io.IOException;
import java.io.PrintWriter;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 Servlet4*/
@WebServlet("/Servlet4")
public class Servlet4 extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public Servlet4() {super();// TODO Auto-generated constructor stub}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html");response.setCharacterEncoding("UTF-8");PrintWriter pw=response.getWriter();String a=(String) request.getAttribute("apple");String b=(String) request.getAttribute("banner");String c =(String) request.getAttribute("ma");pw.print("<h1 style=\"color: pink;\">收到Servlet3的请求,回复消息给浏览器,任务已完成,获得Servlet3携带的数据为:"+a+"</h1>");pw.print("<h1 style=\"color: red;\">收到Servlet3的请求,回复消息给浏览器,任务已完成,获得Servlet3携带的数据为:"+b+"</h1>");pw.print("<h1 style=\"color: yellow;\">收到Servlet3的请求,回复消息给浏览器,任务已完成,获得Servlet3携带的数据为:"+c+"</h1>");}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);}}

模块五Servlect对象五Cookie和Session对象。

package com.Servlect;
/*** A202020895* 10/29* */
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/*** Servlet implementation class Demo01Servlet*/
@WebServlet("/CookieServlet")
public class CookieServlet extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public CookieServlet() {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 stubresponse.getWriter().append("Served at: ").append(request.getContextPath());}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取文本框中的内容String username = request.getParameter("username");String password = request.getParameter("password");//首先判断账号密码是否正确if("hug".equals(username) && "123456".equals(password)){//登录成功之后判断用户是否点击了记住我String remember = request.getParameter("remember");if (remember != null) { //因为单选框默认是null,选上了之后value是on//如果用户点击了,就存到Cookie中Cookie cookie = new Cookie("username",username);Cookie cookie1 = new Cookie("password",password);//设置访问路径cookie.setPath(request.getContextPath()+"/index.html");cookie1.setPath(request.getContextPath()+"/index.html");//设置生存时间cookie.setMaxAge(60);cookie1.setMaxAge(60 * 60 * 24);//把cookie内容传到浏览器响应头response.addCookie(cookie);response.addCookie(cookie1);}//登录成功,跳转到成功页面response.sendRedirect("ServlectA");}else{//登录失败,跳转到失败页面System.out.println("登录失败,跳转到失败页面");response.sendRedirect("ServlectB");}}}
package com.Servlect;
/*** 作业:创建一个新项目,仿QQ登录,
完成浏览器上的登录功能,
并使用session对象完成记住密码和
自动登录功能.*/
import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/*** A202020895* 10/29* */
/*** Servlet implementation class sessionDemo1*/
@WebServlet("/sessionDemo1")
public class sessionDemo1 extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public sessionDemo1() {super();System.out.println("进入sessionDemo1登录界面");System.out.println("这里sessionDemo1运行的抽象方方");// TODO Auto-generated constructor stub}@Overridepublic void init(ServletConfig config) throws ServletException {// TODO Auto-generated method stubsuper.init(config);System.out.println("这里是init方法用来加载数据");}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubresponse.getWriter().append("Served at: ").append(request.getContextPath());}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//因为有密码,为了安全保障用do-post方法response.setContentType("text/html");response.setCharacterEncoding("UTF-8");//首先获取文本框String username = request.getParameter("username");String password = request.getParameter("password");/** 创建对象HttpSession*/HttpSession session=request.getSession();/*** 建立二个动态数据*/session.setAttribute("username", "hug");session.setAttribute("password", "123");/*** 获得动态数据*/String a=(String) session.getAttribute("username");String b=(String) session.getAttribute("password");String aa=request.getParameter("username");String bb=request.getParameter("password");System.out.println(aa+"获得浏览器index.jsp文件中数据");System.out.println(bb+"获得浏览器index.jsp文件中数据");PrintWriter pw =response.getWriter();if (session.isNew()) {pw.print("浏览器是第一次访问服务器");}else if (a.equals(aa)&&b.equals(bb)) {/*** 判断浏览器与我创建的服务器中的数据是否相等*/pw.print("浏览器是第一次访问服务器");response.sendRedirect("ServlectA");}else{//登录失败,跳转到失败页面System.out.println("登录失败,跳转到失败页面");response.sendRedirect("ServlectB");}  	}@Overridepublic void destroy() {// TODO Auto-generated method stubsuper.destroy();System.out.println("运行完毕后销毁servlect");}}

<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>用户登录</title>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">.*{font-size:20px;font-weight: 20px; border: 5px;}
</style>
</head> 
<body>
<h2  style='color:green' align ='center'>用户登录</h2>
<form action="CookieServlet" method="post" id="loginForm" style='color:brown' align ='center'><table style='color:brown' align ='center'><tr><td width="60">用户名</td><td><input type="text" name="username" id="username"/></td></tr><tr><td>密码</td><td><input type="password" name="password" id="password"/></td></tr><tr><td>记住我</td><!--没有value属性的前提下,点中它的值是on--><td><input type="checkbox" name="remember" /></td></tr><tr><td colspan="2" align="center"><input type="submit" value="登录"/></td></tr></table>
</form>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">.*{font-size:20px;font-weight: 20px; border: 5px;}
</style>
</head>
<body>
<h2  style='color:red' align ='center'>用户登录</h2>
<form action="sessionDemo1" method="post" id="loginForm" style='color:brown' align ='center'><table style='color:brown' align ='center'><tr><td width="60">用户名</td><td><input type="text" name="username" id="username"/></td></tr><tr><td>密码</td><td><input type="password" name="password" id="password"/></td></tr><tr><td>记住我</td><!--没有value属性的前提下,点中它的值是on--><td><input type="checkbox" name="remember"/></td><td>自动登录</td><td><input type="checkbox" name="df"/></td></tr><tr><td colspan="2" align="center"><input type="submit" value="登录"/></td></tr></table>
</form></body>
</html>

 

模块六:Servlect过滤器对象六(对比学习内容)

 


package co;import java.io.IOException;
import java.io.PrintWriter;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 Servlect2*/
@WebServlet("/Servlect4")
public class Servlect4 extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public Servlect4() {super();System.out.println("这是Servlect4的抽象方法");// TODO Auto-generated constructor stub}@Overridepublic void init() throws ServletException {// TODO Auto-generated method stubSystem.out.println("Servlect4加载数据");super.init();}@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubSystem.out.println("这里开始运行Servlect4的方法");super.service(req, resp);}@Overrideprotected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubsuper.doDelete(req, resp);System.out.println("Servlect4的删除方法");}@Overridepublic void destroy() {// TODO Auto-generated method stubsuper.destroy();System.out.println("Servlect4销毁数据");
}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub//response.getWriter().append("Served at: ").append(request.getContextPath());response.setContentType("text/html");response.setCharacterEncoding("UTF-8");PrintWriter pw =response.getWriter();pw.print("<h1>我是第一个servlect4</h1>");}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);}}
package com;import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;/*** Servlet Filter implementation class MyFilter1*/
//你要阻止Servlect2
@WebFilter("/Servlect2")public class MyFilter1 implements Filter {/*** Default constructor. */public MyFilter1() {// TODO Auto-generated constructor stubSystem.out.println("构造方法");}/*** @see Filter#destroy()*/public void destroy() {// TODO Auto-generated method stubSystem.out.println("销毁数据");}/*** @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)*/public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {response.setContentType("text/html");response.setCharacterEncoding("UTF-8");System.out.println("我是myfilter1开始拦截请求地址哦哦");//实施访问chain.doFilter(request, response);}/*** @see Filter#init(FilterConfig)*/public void init(FilterConfig fConfig) throws ServletException {// TODO Auto-generated method stubSystem.out.println("加载数据");}}

 

 


模块七listener对象七

 

 

 

 

 

package com.Listener;import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;public class MyServlectListener implements ServletContextListener  {public MyServlectListener() {// TODO Auto-generated constructor stubSystem.out.println("MyServlectListener的构造方法");}@Overridepublic void contextInitialized(ServletContextEvent sce) {System.out.println("Listener程序初始化程序开启");System.out.println("WEB ServletContextListener程序初始化程序开启ServletContext"+ "Listener开始@@程序开始运行@@");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {System.out.println("Listener程序初始化程序结束");System.out.println("WEB contextDestroyed==程序初始化程序in the endontextDestroyed程序结束"+ "程序在结束中");}}
package com.Listener;import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;public class MySessionListener implements  HttpSessionListener {private static int count=0;public MySessionListener() {// TODO Auto-generated constructor stubSystem.out.println("MySessionListener()   构造方法");}@Overridepublic void sessionCreated(HttpSessionEvent se) {// TODO Auto-generated method stubSystem.out.println("当sesssion创建时执行程序 sessionCreated()构造方法");se.getSession().getServletContext().setAttribute("count", ++count);}@Overridepublic void sessionDestroyed(HttpSessionEvent se) {System.out.println("当sesssion销毁时执行程序  sessionDestroyed()构造方法");}}
package com.Listener;import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;public class User  implements HttpSessionBindingListener{@Overridepublic void valueBound(HttpSessionBindingEvent event) {System.out.println("HttpSession 与 User 绑定");// 有用户登陆成功// 判断是否是第一个登陆的人ServletContext servletContext = event.getSession().getServletContext();Integer count = (Integer) servletContext.getAttribute("count");// 第一个登陆为1,非第一个则++count = null == count ? 1 : (count += 1);servletContext.setAttribute("count", count);}@Overridepublic void valueUnbound(HttpSessionBindingEvent event) {System.out.println("HttpSession 与 User 解绑");// 有用户注销登陆ServletContext servletContext = event.getSession().getServletContext();Integer count = (Integer) servletContext.getAttribute("count");// count--是因为在同浏览器下重复登陆时session.setAttribute(name, value)// 每次会覆盖value值进而触发监听器valueBound()的count++servletContext.setAttribute("count", count);}//定义三个数据类型 private int id;private String username;private String password;public User() {}public User(int id, String username, String password) {this.id = id;this.username = username;this.password = password;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return "User{" +"id=" + id +", username='" + username + '\'' +", password='" + password + '\'' +'}';}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xsi:schemaLocation=" .xsd" id="WebApp_ID" version="4.0"><display-name>ZhuJie</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file><welcome-file>ZhuJie</welcome-file><!--    <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>com.Listener.MyServlectListener</welcome-file><welcome-file>com.Listener.MySessionListener</welcome-file></welcome-file-list><filter><filter-name>MyFilter1</filter-name><filter-class>com.Servlect.MyFilter1</filter-class></filter><filter-mapping><filter-name>MyFilter1</filter-name><!-- 这表示可以拦截任何请求 --><url-pattern>/Servlect2</url-pattern></filter-mapping><!-- listener的注册 --><listener><listener-class>com.Listener.MyServlectListener</listener-class></listener><listener><listener-class>com.Listener.MySessionListener</listener-class></listener>
</web-app>

模块七jsp第一课的学习

 

 


 

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
*{
font-size: 30px;
}
</style>
<title>Insert title here</title>
</head>
<body>
<!--   application对象数据跟服务器有关 只要服务器不停址 数据都能共享  -->
<%--applicatio  --%>
<%//application.setAttribute("d", "我第四个数据");
/*  对象四 application*/
pageContext.setAttribute("d", "我是pageContext最大的数据,数据与服务器的关闭有关", pageContext.APPLICATION_SCOPE);
String d=(String)application.getAttribute("d");
out.write("<br>");
out.write(d);
%><%-- 对象三 pageContext --%><%
pageContext.setAttribute("aa", "我是pageContext数据中范围第四小的数据只能自己使用");
String aa=(String)pageContext.getAttribute("aa");
out.write("<br>");
out.write(aa);
%>
<%-- 对象二 request --%>
<%//request对象中的数据是可以形成多个页面共享数据,但是要通过请求转发的方式打开其他页面才能形成共享数据//写法一//request.setAttribute("b", "我是request的数据");//写法二//第一个参数表示要添加的数据的名称 第二个参数表示要存储的数据  第三个参数表示要把数据存储给哪个对象pageContext.setAttribute("bb", "我是request的存放的数据可以形成多个页面共享数据,但是要通过请求转发的方式打开其他页面才能形成共享数据", PageContext.REQUEST_SCOPE);String bb=(String)request.getAttribute("bb");out.write("<br>");out.write(bb);//通过请求转发的方式跳转至five.jsp/* request.getRequestDispatcher("b.jsp").forward(request, response);*/
%><h1>获取到了对象四</h1>
<%
/* application 对象四   */
/* String d=(String)application.getAttribute("d"); */
String d=(String)pageContext.getAttribute("d",pageContext.APPLICATION_SCOPE);
out.write(d);
%><h1>获取了对象三</h1>
<%
String b = (String)pageContext.getAttribute("bb",pageContext.REQUEST_SCOPE);
out.write(b);%><h1>对象二</h1>
<%
//方法一    request对象自己直接获得数据
//String b=(String)request.getAttribute("b");
//方法二   pageContext帮request获得数据
String c=(String)pageContext.getAttribute("b", PageContext.REQUEST_SCOPE);
out.write("我获得了four.jsp页面的request对象的数据:"+c);%><form action="index.jsp" method="post" name="form1">
输入用户名称:
<input type="text" name="loginName" size="20"/>
<br>输入用户密码:
<input type="password" name="password" size="20"/>
<br>在次输入密码:
<input type="passwords" name="passwords" size="20"/>
<br>:输入信息:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="submit" value="提交" size="12">
<input type="reset" name="reset" value="重值" size="12">
</form><h1 style="background-color: pink;align: center; text-align: center;">九九乘法表</h1>
<br><%for(int i=1;i<=9;++i){%><%for(int j=1;j<=i;++j){ %><%=i+"*"+j+"="+i*j+" " %><%} %><br><%} %><hr></body>
</html>

 模块八.jsp九大内置对象(重要的****)

<%@page import="java.util.Calendar"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
*{font-size: 30px;font-weight: bold;}
h4{
color: red;
}
</style>
<title>Insert title here</title>
</head>
<body>
<h1>九大内置对象的写法</h1><%//out.write("aaaaa");pageContext.getOut().write("这是由pageContext管理的对象哦");//requestpageContext.getRequest();//responsepageContext.getResponse();//sessionpageContext.getSession();//exceptionpageContext.getException();//pagepageContext.getPage();//configpageContext.getServletConfig();//applicationpageContext.getServletContext();%><hr><h4>pageContext是可以存储数据,但是其作用域只能供其本身的jsp页面使用</h4><!--  对象一pageContext  --><%	pageContext.setAttribute("aaa", "我是pageContext的存放范围最小的数据");pageContext.setAttribute("aa", "1001");String info=(String)pageContext.getAttribute("aaa");String inf=(String)pageContext.getAttribute("aa");out.write("<br>");out.write(info); out.write(inf); %><!-- 对象二 request -->
<h4>request对象中的数据是可以形成多个页面共享数据,但是要通过请求转发的
方式打开其他页面才能形成共享数据</h4><%request.setAttribute("b", "我是request的数据");//pageContext.setAttribute("bbb", "我是request的数据存放的范围是第二小的数据", PageContext.REQUEST_SCOPE);String infob=(String)request.getAttribute("b");out.write("<br>");out.write(infob);//request.getRequestDispatcher("BB.jsp").forward(request, response);  %><br><h4>说明session对象能不能共享数据和浏览器有关</h4><!--  对象三  Session--><%session.setAttribute("ccc", "我是session的数据存放的范围是第二大的数据");session.setAttribute("cc", "我是session的数据存放的范围是第二大的数据1003@@@");String c=(String)session.getAttribute("ccc");	String cc=(String)session.getAttribute("cc");	out.write(c);out.write(cc);%><h4>这是application对象的数据与服务器有关,只要服务器不停止,数据都能共享</h4><!-- 对象四  范围最大 --><%pageContext.setAttribute("ddd", "我是application的数据存放的范围是第一大的数据",PageContext.APPLICATION_SCOPE);
String d=(String)application.getAttribute("ddd");
application.setAttribute("dd", "我是application创建的第二个数据类型是one.two");
String dd=(String)application.getAttribute("dd");
out.write("<br>");
out.write(d);
pageContext.getOut().write(dd);application.setAttribute("e", "我是application创建的第三个数据类型是one.tree");
String e=(String)application.getAttribute("e");
pageContext.getOut().write(e);application.setAttribute("f", "我是application创建的第三个数据类型是one.four");
String f=(String)application.getAttribute("f");
pageContext.getOut().write(f);%><!-- //out.write("aaaaa");pageContext.getOut().write("这是由pageContext管理的对象哦"); --><h1>--------------------对象五的获取 pageContext.getOut()</h1><%
//创建日历类Calendar  calendar =Calendar.getInstance();int AM_PM=calendar.get(calendar.AM_PM);if(AM_PM==calendar.AM){pageContext.getOut().write("<h1 style=' color:red'>获取上午时间</h1>");}else if(AM_PM==calendar.PM){pageContext.getOut().write("<h1 style=' color:green'>获取下午时间</h1>");}%>
<h1>-------------page-------------对象六 </h1><%
pageContext.getOut().write((page.equals(this)?"page是当前对象":"page不是当前对象"));
%>con
<h1>--------------------------对象7 ----Confign  </h1><%
String name = config.getServletName();
out.write(name);%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
*{font-size: 30px;font-weight: bold;}
h1{
color: red;
}</style>
<title>Insert title here</title>
</head>
<body><h1>--------------------对象二  request的获取</h1>
<%  
//方法一    request对象自己直接获得数据
String b=(String)request.getAttribute("b");
//方法二   pageContext帮request获得数据
//String b=(String)pageContext.getAttribute("b", PageContext.REQUEST_SCOPE);
out.write("我获得了AA.jsp页面的request对象的数据:"+b);%><h1>--------------------对象一不能获取成功</h1><%/*  String info=(String)pageContext.getAttribute("aaa");
out.write(info);  */%><hr>
<h1>--------------------对象三的获取</h1>
<%//方法一    使用session直接获得数据
String c=(String)session.getAttribute("ccc");
//方法二   pageContext帮session获得数据
String cc=(String)pageContext.getAttribute("cc", PageContext.SESSION_SCOPE);
out.write("我获得了AA.jsp页面的session对象的数据:"+c);
out.write("我获得了AA.jsp页面的session对象的数据:"+cc);%><hr>
<h1>--------------------对象四的获取</h1>
<%
//方法一
//String ddd=(String)application.getAttribute("ddd");String ddd=(String)pageContext.getAttribute("ddd",PageContext.APPLICATION_SCOPE);
out.write("我获得了AA.jsp页面的application对象的数据:"+ddd);String e=(String)application.getAttribute("e");
pageContext.getOut().write(e+"这是对象e");
%>
<hr>
<h1></body>
</html>

模块九jsp:四大作用域

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>数据类</title>
<style type="text/css">
*{color: red;
}</style>
</head>
<body>
<h1>方案四:利用二个JSP动作将数据存储起来</h1>
<!--完全使用jsp动作将数据放入四大域 存储起来 -->
<!-- 创建javaBean类的对象  -->
<jsp:useBean id="people" class="com.People.People" scope="application"></jsp:useBean>
<jsp:setProperty property="id" name="people" value="1001"/>
<jsp:setProperty property="name" name="people" value="小红一号"/>
<jsp:setProperty property="age" name="people" value="23"/>
<jsp:setProperty property="sex" name="people" value="男"/>
<jsp:setProperty property="phone" name="people" value="123456678"/>
<jsp:setProperty property="addr" name="people" value="江西南昌"/>
<jsp:setProperty property="height" name="people" value="189cm"/>
<jsp:setProperty property="weight" name="people" value="64kg"/>
<jsp:setProperty property="phone" name="people" value="123456678"/>
<jsp:setProperty property="a" name="people" value="a属性是人皮肤的色彩pink"/>
<jsp:setProperty property="b" name="people" value="b属性是人穿衣服的color"/>
<jsp:setProperty property="c" name="people" value="b属性是人类未知的属性"/><hr>
<h2>创建第二个人</h2>
<jsp:useBean id="people2" class="com.People.People" scope="application"></jsp:useBean>
<jsp:setProperty property="id" name="people2" value="1002"/>
<jsp:setProperty property="name" name="people2" value="小明二号"/>
<jsp:setProperty property="age" name="people2" value="23"/>
<jsp:setProperty property="sex" name="people2" value="男"/>
<jsp:setProperty property="phone" name="people2" value="123456678"/>
<jsp:setProperty property="addr" name="people2" value="江西南昌"/>
<jsp:setProperty property="height" name="people2" value="189cm"/>
<jsp:setProperty property="weight" name="people2" value="64kg"/>
<jsp:setProperty property="phone" name="people2" value="123456678"/>
<jsp:setProperty property="a" name="people2" value="a属性是人皮肤的色彩green"/>
<jsp:setProperty property="b" name="people2" value="b属性是人穿衣服的color"/>
<jsp:setProperty property="c" name="people2" value="b属性是人类未知的属性"/><h2>创建第三个人</h2>
<jsp:useBean id="people3" class="com.People.People" scope="application"></jsp:useBean>
<jsp:setProperty property="id" name="people3" value="1003"/>
<jsp:setProperty property="name" name="people3" value="小户三号"/>
<jsp:setProperty property="age" name="people3" value="23"/>
<jsp:setProperty property="sex" name="people3" value="男"/>
<jsp:setProperty property="phone" name="people3" value="123456678"/>
<jsp:setProperty property="addr" name="people3" value="江西南昌"/>
<jsp:setProperty property="height" name="people3" value="189cm"/>
<jsp:setProperty property="weight" name="people3" value="64kg"/>
<jsp:setProperty property="phone" name="people3" value="123456678"/>
<jsp:setProperty property="a" name="people3" value="a属性是人皮肤的色彩red"/>
<jsp:setProperty property="b" name="people3" value="b属性是人穿衣服的color"/>
<jsp:setProperty property="c" name="people3" value="b属性是人类未知的属性"/><h2>创建第四个人</h2>
<jsp:useBean id="people31" class="com.People.People" scope="application"></jsp:useBean>
<jsp:setProperty property="id" name="people31" value="1004"/>
<jsp:setProperty property="name" name="people31" value="小户三号"/>
<jsp:setProperty property="age" name="people31" value="23"/>
<jsp:setProperty property="sex" name="people31" value="男"/>
<jsp:setProperty property="phone" name="people31" value="123456678"/>
<jsp:setProperty property="addr" name="people31" value="江西南昌"/>
<jsp:setProperty property="height" name="people31" value="189cm"/>
<jsp:setProperty property="weight" name="people31" value="64kg"/>
<jsp:setProperty property="phone" name="people31" value="123456678"/>
<jsp:setProperty property="a" name="people31" value="a属性是人皮肤的色彩red"/>
<jsp:setProperty property="b" name="people31" value="b属性是人穿衣服的color"/>
<jsp:setProperty property="c" name="people31" value="b属性是人类未知的属性"/></body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="">
*{
color: red;
font-size: 30px;
font-weight: bold;}
</style>
</head>
<body>
<h1> 取出数据内容</h1><jsp:useBean id="people" class="com.People.People" scope="application"></jsp:useBean>
<P>出生编号:<jsp:getProperty property="id" name="people"/></P>
<P>姓名:<jsp:getProperty property="name" name="people"/></P>
<P>年龄:<jsp:getProperty property="age" name="people"/></P>
<P>性别:<jsp:getProperty property="sex" name="people"/></P>
<P>手机号:<jsp:getProperty property="phone" name="people"/></P>
<P>家庭地址:<jsp:getProperty property="addr" name="people"/></P>
<P>身高:<jsp:getProperty property="height" name="people"/></P>
<P>体重:<jsp:getProperty property="weight" name="people"/></P>
<P>属性a:<jsp:getProperty property="a" name="people"/></P>
<P>属性b:<jsp:getProperty property="b" name="people"/></P>
<P>属性c:<jsp:getProperty property="c" name="people"/></P>
<hr>
<jsp:useBean id="people2" class="com.People.People" scope="application"></jsp:useBean>
<P>出生编号:<jsp:getProperty property="id" name="people2"/></P>
<P>姓名:<jsp:getProperty property="name" name="people2"/></P>
<P>年龄:<jsp:getProperty property="age" name="people2"/></P>
<P>性别:<jsp:getProperty property="sex" name="people2"/></P>
<P>手机号:<jsp:getProperty property="phone" name="people2"/></P>
<P>家庭地址:<jsp:getProperty property="addr" name="people2"/></P>
<P>身高:<jsp:getProperty property="height" name="people2"/></P>
<P>体重:<jsp:getProperty property="weight" name="people2"/></P>
<P>属性a:<jsp:getProperty property="a" name="people2"/></P>
<P>属性b:<jsp:getProperty property="b" name="people2"/></P>
<P>属性c:<jsp:getProperty property="c" name="people2"/></P>
<hr>
<jsp:useBean id="people3" class="com.People.People" scope="application"></jsp:useBean>
<P>出生编号:<jsp:getProperty property="id" name="people3"/></P>
<P>姓名:<jsp:getProperty property="name" name="people3"/></P>
<P>年龄:<jsp:getProperty property="age" name="people3"/></P>
<P>性别:<jsp:getProperty property="sex" name="people3"/></P>
<P>手机号:<jsp:getProperty property="phone" name="people3"/></P>
<P>家庭地址:<jsp:getProperty property="addr" name="people3"/></P>
<P>身高:<jsp:getProperty property="height" name="people3"/></P>
<P>体重:<jsp:getProperty property="weight" name="people3"/></P>
<P>属性a:<jsp:getProperty property="a" name="people3"/></P>
<P>属性b:<jsp:getProperty property="b" name="people3"/></P>
<P>属性c:<jsp:getProperty property="c" name="people3"/></P><jsp:useBean id="people31" class="com.People.People" scope="application"></jsp:useBean>
<P>出生编号:<jsp:getProperty property="id" name="people31"/></P>
<P>姓名:<jsp:getProperty property="name" name="people31"/></P>
<P>年龄:<jsp:getProperty property="age" name="people31"/></P>
<P>性别:<jsp:getProperty property="sex" name="people31"/></P>
<P>手机号:<jsp:getProperty property="phone" name="people31"/></P>
<P>家庭地址:<jsp:getProperty property="addr" name="people31"/></P>
<P>身高:<jsp:getProperty property="height" name="people31"/></P>
<P>体重:<jsp:getProperty property="weight" name="people31"/></P>
<P>属性a:<jsp:getProperty property="a" name="people31"/></P>
<P>属性b:<jsp:getProperty property="b" name="people31"/></P>
<P>属性c:<jsp:getProperty property="c" name="people31"/></P>
</body>
</html>

模块10EL表达式


 

 

 

 

 


<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
*{
color: black;
font-size: 45px;
}
</style>
<title>Insert title here</title>
</head>
<body>
<!-- 算数运算 -->${4+6 }${4/6 }${4*6 }<br>${3222*89 }<br>${23 mod 5 }<br>${23/5 }<br><%request.setAttribute("age", 56);	%>${age-5 }<br><br><!-- 关系运算符 -->${age/11>7 }<br>${age/11<=7 }<br>${age/11>=7 }<br>${age/11<7 }<br>${age/11==7 }<br>
<!-- 逻辑运算符 -->${ age mod 3==0 && age/5 ge 2}<br>
${ age mod 3 ==0||  age/5 ge 2}<br>
<%--  空运算符--%>
<%
int [] nums =null;
pageContext.setAttribute("n", nums);
%>
${empty(n) }<br><!-- 三元运算符   -->
${age/2+3-6>45?"AAA":"BBB" }<br><!--练习题  -->
<h2>${23+67*90 }<br>
${3>2?3:2 }<br>
</h2></body>
</html>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
*{font-size: 34px;
}
</style>
</head>
<body><%Map<String,String>map =new HashMap<>();
map.put("book1","JAVA程序设计");
map.put("book2","JAVA程序设计1");
map.put("book3","JAVA程序设计2");
map.put("book4","JAVA程序设计3");
map.put("book5","JAVA程序设计4");
request.setAttribute("bo", map);Map<String,String>map1 =new HashMap<>();
map1.put("boy1","AAAA");
map1.put("boy2","BB");
map1.put("boy3","CC");
map1.put("boy4","DD");
request.setAttribute("boy", map1);List<Map<String,String>>oList =new ArrayList<>();
oList.add(map);
oList.add(map1);
pageContext.setAttribute("ot", oList);
%>
<!-- MAP集合查找 -->
${bo.book1}<br>
${bo.book2}<br>
${bo.book3}<br>
${bo.book4}<br>
${bo.book5}<br>
<h1 style="color: red;">===============</h1>
${boy.boy1}<br>
${boy.boy2}<br>
${boy.boy3}<br>
${boy.boy4}<br><h1 style="color: green;">===============</h1>
${ot[0].book3} ,<br>
<%-- ${0t[1]["boy2"] } --%>
<!-- 集合中 -->
${ot[1].boy1}<br>
${ot[1].boy2}<br>
${ot[1].boy3}<br>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
*{
font-size:30px;
font-weight: bold;
background:pink;}
h1{
background-color: green;
color: black;
font-weight: bold;}
h2{
color: black;
border: 2px solid;
background-color: yellow;
}
h3{
color: pink;
background-color: red;
}
</style>
</head>
<body>
<h1>
## seesion  浏览器不切## application  服务器不关闭## request:请求转发跳转页面的情况,只能两个页面共享数据## pageContext:该对象中只能页面自己用## 才能用setAttribute的方法## 将四作用域排序**## **pageContext request seesionn application** 
</h1>
<!-- JSP 中的四大域  存放数据称为四大域 
按照作用域从小到大
1 pageContext:页面本身使用,其他页面并不能共享数据。
2 resquest; 只能在请求转发的情况下共享数据
3 Session 在不切浏览器的情况下
4 application 在不关闭浏览器的前提下,任何都能共享数据。
-->
<h1>===========这个是什么?=====${pageScope.a  }
${sessionScope.a  }<br>
${sessionScope.a  }<br>
${applicationScope.a  }<br></h1>
<%
pageContext.setAttribute("a", "pageContext@1APPLE");
request.setAttribute("b", "request@2.BROW 123");
session.setAttribute("c", "session@3.DFER true");
application.setAttribute("d", "application@4.PINK");
%><!--   方式一 获取-->
<%=pageContext.getAttribute("a") %><br>
<%=request.getAttribute("b") %><br>
<%=session.getAttribute("c") %><br>
<%=application.getAttribute("d") %><br>
<br><br><hr><%=pageContext.getAttribute("a") %><br>
<%=pageContext.getAttribute("b",pageContext.REQUEST_SCOPE ) %><br>
<%=pageContext.getAttribute("c",pageContext.SESSION_SCOPE ) %><br>
<%=pageContext.getAttribute("d",pageContext.APPLICATION_SCOPE ) %><br>
<h1>====================代码实例0=========================</h1>
<!-- 方式二  -->
<%=pageContext.getAttribute("a") %><br>
<%=pageContext.getAttribute("a",pageContext.REQUEST_SCOPE ) %><br>
<%=pageContext.getAttribute("a",pageContext.SESSION_SCOPE ) %><br>
<%=pageContext.getAttribute("a",pageContext.APPLICATION_SCOPE ) %><br>
<br><br><hr>
<!-- -方式三  快速查找  findAttribute  
改方法查找数据作用域从小到大 pageContext   resquest;   Session  application--><h3>====================代码实例一=========================</h3>
<%=pageContext.findAttribute("a") %><br>
<%=pageContext.findAttribute("b") %><br>
<%=pageContext.findAttribute("b") %><br>
<%=pageContext.findAttribute("b") %><br>
<br><br><hr><h3>==================代码实例二===========================</h3>
<%=pageContext.findAttribute("a") %><br>
<%=pageContext.findAttribute("b") %><br>
<%=pageContext.findAttribute("c") %><br>
<%=pageContext.findAttribute("c") %><br>
<br><br><hr><h3>===================代码实例三==========================</h3>
<%=pageContext.findAttribute("a") %><br>
<%=pageContext.findAttribute("b") %><br>
<%=pageContext.findAttribute("c") %><br>
<%=pageContext.findAttribute("d") %><br>
<br><br><hr><h2>==========================方案四======================</h2>
<!-- 方式四 -->
<!--  EL表达式语法 --> 
<!-- 该表达式其内部就是 findAttribute 快速查找 请看下面代码 可以获得八大数据类型-->
<!-- findAttribute  === ${a }<br> -->
<h2>================代码实例0=============================</h2>
${a }<br>
${a }<br>
${a}<br>
${a}<br>
<h2>================代码实例1============================</h2>
${a }<br>
${b }<br>
${b}<br>
${b}<br>
<h2>================代码实例2============================</h2>
${a }<br>
${b }<br>
${c}<br>
${c}<br>
<h2>================代码实例3============================</h2>
${a }<br>
${b }<br>
${c}<br>
${d}<br>
<h3>-------------------------------解决问题内容--------------------------------</h3>
<!-- 解决上面的问题  -->
<!-- EL pageContextScope -->
${pageScope.a  }<br>
<!-- requestScope  -->
${sessionScope.a  }<br>
<!-- sessionScope -->
${sessionScope.a  }<br>
<!-- applicationScope -->
${applicationScope.a  }<br></body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
*{
font-size: 34px;
}
</style>
</head>
<body>
<!-- JSP 中的四大域  存放数据称为四大域 
按照作用域从小到大
1 pageContext:页面本身使用,其他页面并不能共享数据。
2 resquest; 只能在请求转发的情况下共享数据
3 Session 在不切浏览器的情况下
4 application 在不关闭浏览器的前提下,任何都能共享数据。
-->
<%
pageContext.setAttribute("a", "pageContext@1APPLE");
request.setAttribute("a", "request@2.BROW");
session.setAttribute("a", "session@3.DFER");
application.setAttribute("a", "application@4.PINK");
%><!--   方式一 获取-->
<%=pageContext.getAttribute("a") %><br>
<%=request.getAttribute("a") %><br>
<%=session.getAttribute("a") %><br>
<%=application.getAttribute("a") %><br>
<br><br><hr><!-- 方式二  -->
<%=pageContext.getAttribute("a") %>
<%=pageContext.getAttribute("a",pageContext.REQUEST_SCOPE ) %><br>
<%=pageContext.getAttribute("a",pageContext.SESSION_SCOPE ) %><br>
<%=pageContext.getAttribute("a",pageContext.APPLICATION_SCOPE ) %><br>
<br><br><hr>
<!-- -方式三  快速查找  findAttribute    -->
<%=pageContext.findAttribute("a") %><br>
<%=pageContext.findAttribute("a") %><br>
<%=pageContext.findAttribute("a") %><br>
<%=pageContext.findAttribute("a") %><br>
<br><br><hr></body>
</html>

模块十一JSTL标签库。

 


 

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!-- @1使用taglib   url:在线标签库的地址@2prefix 本地的前首写字母 --><%@taglib  uri="" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL标签库的使用一通用标签库</title>
<style type="text/css">
*{
color: black;
font-size: 30px;
font-weight: bolder;}
h2{
color: white;
background-color: black;}
</style>
</head>
<body>
<h2 style="align-content: center; text-align: center;">JSTL标签库的使用一通用标签库1</h2>
<%request.setAttribute("a", "我是数据a定义了一个数据为HellowA") ;%>
<%request.setAttribute("b", "我是数据b定义了一个数据为HellowB") ;%>
<!-- El表达式 -->
<h2>用El表达式获取的数据</h2>
<h1>${a }</h1><br>
<h1>${b }</h1><br>
<h3>通用标签set使用</h3>
<!-- <!-- var:对应数据名称
value:数据的值scope:四大域的名称page:pageContextresquest: resquestsession:sessionapplication: application--> 
<%-- <c:set var="a1" value="hellow1" scope="resquest"></c:set>
标签库${a1 }
<c:set var="b1" value="hellow1" ></c:set> --%>
<%-- ${pageScrope.a } --%>
<h2>表达式标签库</h2>
<h2>
1 scope="四大作用域"。
2 var="对应的变量名" 。
3 value"对应的值" 。
@1用表达式$(a1)获取。
</h2>
<c:set var="a1" value="我是一只小花猫" scope="application"></c:set>
<h2>@1获取数据用EL表达式</h2>
${a1 }
<br>
<h2>@2指定作用域pageScope sessionScope</h2>
<c:set var="b" value="我是一只小花狗" ></c:set> ${pageScope.b } 
<br>
<!-- value 省去 -->
<h2>@3省去 value的值 </h2>
<c:set var="c" scope="session">Hellow World Jsp</c:set>
<h2>指定作用域</h2>
<br>
${sessionScope.c }
<br>
<h2>@4跳转作用域来获取数据</h2>
<c:set scope="application" var="apple" value="${a1 }"></c:set>
$(applicationScope.apple)<br>
<hr><h2>@5删除数据内容</h2>
<c:remove var="c"/>
${empty(c) }
<!-- out 输出数据 通过EL表达式 -->
<h2>@ 6out 输出数据 通过EL表达式</h2>
<c:out value="${c }"></c:out>
<br>
<c:out value="${b }"></c:out>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@taglib  uri="" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
*{
color: black;
font-size: 34px;
font-weight: bold;}
h1{
color: yellow;
background-color: black;}
h2{
color: white;
background-color: black;}
</style>
<title>JSTL标签库的使用一通用标签库2</title>
</head>
<body><h1 style="color: red; font-size: 45px;text-align: center;"> 标签库和JSp动作合起来使用 </h1>
<h2 style="align-content: center; text-align: center;">JSTL标签库的使用二通用标签库2</h2>
<jsp:useBean id="car" class="com.abc.Car" scope="page"></jsp:useBean>
<c:set target="${car }" property="name" value="红旗"></c:set>
<c:set target="${car }" property="price" value="36000"></c:set>
<c:set target="${car }" property="type" value="pink"></c:set>
${car.name }<br>
${car.price }<br>
${car.type}<br>
<hr></body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@taglib  uri="" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
*{
color: black;
font-size: 34px;
font-weight: 900px;}
h1{
color: yellow;
background-color: black;}
h2{
color: white;
background-color: black;}
</style>
<meta charset="UTF-8">
<title>JSTL标签库的使用一流程标签库</title>
</head>
<body>
<article><h1>
表达式标签:out set remove 
流程标签 if choose when otherwise
循环标签 forEach  forTokens
url 标签  import    url   redirect parm
</h1><h2 style="align-content: center;">JSTL标签库的使用三流程标签库</h2>
<hr>
</article>
<!-- 条件标签库 -->
<!--  判断一个数的基数 和偶数-->
<h2>判断一个数的基数 和偶数</h2>
<c:set var="num" value="346" scope="page"></c:set><c:set var="nums" value="347" scope="page"></c:set>
<h3>test:是表达式,最后结果为true,false</h3>
<c:if test="${num%2 ==0}">
<h3 style="color: red;">这数为偶数偶数</h3>
</c:if>
<c:if test="${num%2 !=0}">
<h3 style="color: green;">这数为奇数</h3>
</c:if>
<hr>
<h2>方法二计算了一次</h2>
<c:if test="${nums%2 ==0}" var="jieguo" scope="page"></c:if><c:if test="${jieguo}">
<h3 style="color: red;">这数为偶数</h3>
</c:if>
<c:if test="${!jieguo}">
<h3 style="color: green;">这数为奇数</h3>
</c:if>
<hr><!--判断月份  -->
<c:set var="month" value="13" scope="session"></c:set>
<c:set var="months" value="8" scope="session"></c:set><c:choose>
<c:when test="${month>=1 && month<=3 }">
<h4 style="color: gray;">第一季节</h4>
</c:when><c:when test="${month>=4 && month<=6 }">
<h4 style="color: red;">第二季节</h4>
</c:when><c:when test="${month>=7 && month<=9 }">
<h4 style="color: green;">第三季节</h4>
</c:when><c:when test="${month>=10 && month<=12 }">
<h4 style="background-color: black;color: pink; font-size: 50px">第四季节冬季</h4>
</c:when><c:otherwise>
<h1>月份是有误的</h1>
</c:otherwise>
</c:choose><c:choose>
<c:when test="${months>=1 && months<=3 }">
<h4 style="color: gray;">第一季节</h4>
</c:when><c:when test="${months>=4 && months<=6 }">
<h4 style="color: red;">第二季节</h4>
</c:when><c:when test="${months>=7 && months<=9 }">
<h4 style="color:red; background-color: black;">第三季节</h4>
</c:when><c:when test="${months>=10 && months<=12 }">
<h4 style="background-color: black;color: pink; font-size: 50px">第四季节冬季</h4>
</c:when><c:otherwise>
<h1>月份是有误的</h1>
</c:otherwise>
</c:choose></body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@taglib  uri="" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
*{
color: black;
font-size: 34px;
font-weight: bold;}
h2{
color: white;
background-color: black;}
</style>
<title>循环标签 forEach</title></head>
<body>
<h2>
表达式标签:out set remove 
流程标签 if choose when otherwise
循环标签 forEach  forTokens
url 标签  import    url   redirect parm
</h2>
<h2>for循环标签</h2>
<%
String [] name ={"编号1001小碗","编号1002小花","编号1003小白","编号1004小坡","编号1005小(>^ω^<)喵","编号1006消费","编号1007呼叫","编号1008hello","编号1009need发","编号1010你好","编号1001011细看","编号10012加油","编号10013明白"};
application.setAttribute("name", name);
String [] names ={"小白","小坡","小(>^ω^<)喵","消费","呼叫","hello","need发","你好","细看","小胡"};
application.setAttribute("names", names);
%>
${names[0]},${names[1]},${names[2]},${names[3]},${names[4]}<br>
<h2>
forEach标签属性:
@1 items:要循环数据的集合or 数组。
@2 var:定义变量,用于存放每一次循环从数组中拿数据。
@3 begin从第几个数据开始循环。
@4 end控制下标在第几项结束。
@5 step增加的步长。
@6 varStatus设置没一次循环的次数
</h2><c:forEach items="${names}" var="nb" step="2" varStatus="s">
<c:if test="${s.count %2 !=0}">存放每一次循环从数组中拿数据${nb }<br></c:if>
</c:forEach><hr>
<c:forEach items="${name}" var="nbs" begin="2" end="10" >存放每一次循环从数组中拿数据${nbs }<br>
</c:forEach></body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@taglib  uri="" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
*{
color: black;
font-size: 30px;
font-weight: bold;}
h2{
color: white;
background-color: black;}
</style>
<title>forTokens</title>
</head>
<body>
<c:forTokens items="脸色,换货单号的,kjdkdkdk,hhsdhdj,哈哈哈" delims="," var="str">
<p style="color: green; background-color: black;">被分开的数据以,分开: ${str }</p>
</c:forTokens>
<br><c:forTokens items="脸色/,换货/单号的/,kjdkd/kd/k,hhsd/hdj,哈哈哈" delims="/" var="str1">
<p style="color: red; background-color: black;">被分开的数据以/分开: ${str1 }</p>
</c:forTokens>
<hr>
<c:forTokens items="hellow-orld-色-而退-京东快-递开-的亟-待解-决的撒娇-的角度-记得-看恢-复上-课了" delims="-" var="str2">
<p style=" background-color: green; color: pink;">被分开的数据以-分开: ${str2 }</p>
</c:forTokens>
<hr>
<c:forTokens items="h*ell*ow-*orl*d-色-而*退-京东*快-递*开-的*亟-待*解-*决的*撒娇-的*角度-记*得-看恢*-复*上-*课*了*" delims="*" var="str2">
<p style=" background-color: yellow; color: pink;">被分开的数据以*分开: ${str2 }</p>
</c:forTokens>
</body>
</html>

模块十二java与mysql


package com.db.text;import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.Properties;import javax.sql.DataSource;import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.sun.javafx.collections.MappingChange.Map;public class JDBCTemplateDemo {public static JdbcTemplate  jt =null;/*** sping框架*/static {//建立连接InputStream is=DruidDemo.class.getClassLoader().getResourceAsStream("com/db/text/druid.properties");Properties properties =new Properties();try {properties.load(is);DataSource ds=DruidDataSourceFactory.createDataSource(properties);jt=new JdbcTemplate(ds);//并不是断开连接,而是将用完的对象放入连接池} catch (Exception e) {e.printStackTrace();}}@Testpublic void test1() {//增加语句String sql="insert into tb_students(name,age) VALUES(?,?)";int i=	jt.update(sql,"我是mysql",25);if(i>0) {System.out.println("数据增加成功");}}//修改数据
@Test
public void text2() {String sql="update tb_students set age=? where id=?";int i=jt.update(sql ,24,23);if(i>0) {System.out.println("数据修改成功");}}@Test
public void text3() {String sql="DELETE FROM `studentdb`.`tb_student` WHERE ` id` = ?";int i=jt.update(sql ,1);if(i>0) {System.out.println("数据删除成功");}}@Test
public void text4() {String sql="select *from tb_students where id=?";Map<String, Object> map = (Map<String, Object>) jt.queryForMap(sql, 2);System.out.println(map);}@Test
public void test5() {String sql="select *from tb_students";List<java.util.Map<String, Object>> olList =jt.queryForList(sql);olList.forEach(map->System.out.println(map));}}

回顾了之前学习的重要代码。之后我会综合自己所学内容,将mysql,sevlect,jsp,html,javascript,jquery,csss3.前端+后端+数据库综合起来使用。还有一些框架将使用到项目中。一个项目需要不断的去构思。

 

 

 

 

本文标签: 欢迎来到Jsp编程课时十ServlectJsp基础回顾