zoukankan      html  css  js  c++  java
  • Jsp入门学习笔记

    Jsp入门


    一、JSP基础语法

    1.JSP指令:

    page	inlcude		taglib
    

    2.JSP注释:

    a.html注释:	<!-- abcdefghijklmn -->
    b.jsp注释: <%-- abcdefghijklmn --%>
    c.jsp脚本内:	//abcdefghijk      /*abcdefghijk*/
    

    3.jsp脚本:

    <%   java代码   %>
    

    4.jsp声明:

    <%!  java代码   %>
    

    5.jsp表达式:

    <%= 表达式      %>
    

    二、JSP内置对象:

    0.补充

    (1)Web的请求---响应模式:

    Client(request)<------->Server(response)
    

    (2)表单提交方式:

    (3)中文乱码:

    a.jsp中设置contentType的charset为UTF-8
    b.request对象setCharacterEncoding设为utf-8
    c.URL传参配置tomcat的server.xml, connector中添加URIEncoding=utf-8
    

    (4)请求重定向与请求转发:

    a.重定向:客户端行为,两次请求,不保存前次请求内容,URL地址改变
    	//response.sendRedirect("page");
    b.转发:服务器端行为,一次请求,保存请求内容,URL不变
    	//request.getRequestDispatcher("page").forward(request,response);
    

    1.out对象

    (1)简介:

    a.JspWriter类的实例	
    

    (2)主要方法:

    a.println();
    b.clear();
    c.flush();
    d.getBufferSize();
    e.getRemaining();
    f.isAutoFlush();
    g.close();
    

    2.request对象

    (1)简介:

    a.HttpServeletRequest类的实例
    

    (2)主要方法:

    a.getParameter("_name");
    b.getParameterValues("_name");
    c.set/getCharacterEncoding("utf-8");
    d.setAttribute("attr_name","attr_value");    getAttribute("attr_name");
    e.getContentType();
    f.getProtocol();
    g.getServerName();		getServerPort();
    h.getContentLength();
    i.getRemoteAddr();
    j.getRealPath();
    k.getContextPath();
    

    3.response对象

    (1)简介:

    a.HttpServeletResponse类的实例
    b.具有页面作用域
    

    (2)主要方法:

    a.getCharacterEncoding();
    b.setContentType("type");
    c.getWriter();//打印时提前于out内置对象 
    d.sendRedirect(java.lang.String location)
    

    4.session对象--会话

    (1)简介:

    a.保存在服务器内存中
    b.不同用户对应不同session
    

    (2)常用方法:

    a.getCreateTime();
    b.getId();
    c.setAttribute ("attr_name","attr_value");
    d.getAttribute("attr_name");
    e.getValueNames();
    f.setMaxInactiveInterval();
    

    (3)生命周期:

    a.创建: 生成一个SessionId,在当前生存周期内对所有页面均一致
    b.活动:页面跳转啊什么的	
    c.销毁: session.invalidate()方法/web.xml中配置、 超时、 服务器重启
    

    5.application对象

    (1)简介:

    a.实现数据共享
    b.类似const/final
    c.ServerletContext类的对象
    

    (2)常用方法:

    a.setAttribute("attr_name",attr_value);
    b.getAttribute("attr_name");
    c.getAttributeNames();
    d.getServerInfo();
    

    6.Page对象

    a.Object类的实例
    b.似this指针
    c.方法同JAVA中的Object
    

    7.pageContext对象

    (1)主要方法:

    8.config对象

    a.getServletContext();
    b.getInitParameter("name");
    c.getInitParameterNames();
    

    9.Exception对象

    (1)简介:

    a.java.lang.Throwable的对象
    b.须设置isErrorPage="true"
    c.Page指令中配置errorPage属性
    

    (2)方法:

    a.getMessage();
    b.toString();
    c.printStackTrace();
    d.FillInStackTrace();
    

    三、Javabeans:

    一种特殊的java类

    1.设计原则:

    (1)类公有
    (2)成员私有
    (3)公有无参构造方法
    (4)getter/setter方法
    

    2.jsp动作元素:

    一个XML标签

    (1)与存取javabean有关的:

    <jsp:useBean> <jsp:setProperty> <jsp:getProperty>
    

    (2)基本元素:

    JSP1.2开始有的6个
    

    (3)与JSP Document相关的:

    (4)动态生成XML元素标签:

    (5)Tag File中的:

    3.Javabeans使用:

    (1)新建类的方式:

    a.跟使用普通JAVA类一样	
    b.遵循Javabeans设计原则就行
    c.jsp页面中用Page指令import进来就行
    

    (2)使用jsp的动作标签:

    a.usebean:
    	<jsp:useBean id="_id" class="class_name" scope="page" />;
    
    b.setProperty:
    	<jsp:setProperty name="javabean_name" property="*" />(跟表单关联,自动匹配);
      	<jsp:setProperty name="javabean_name" property="javabean_attrName" />(跟表单关联,部分匹配);
      	<jsp:setProperty name="javabean_name" property="javabean_attrName" value="beanVlaue"/>(手工设置);
      	<jsp:setProperty name="javabean_name" property="propertyName" param="request传参"/>(与request对象关联);
    c.getProperty:
    	<jsp:getProperty name="javabean_name" property="prop_name" />;
    

    4.javabeans作用域范围

    scope属性的取值:

    (1)page
    (2)session
    (3)request
    (4)applocation
    

    四、Jsp状态管理

    1.http状态:

    无状态性:服务器并不认识请求的来源
    

    2.Jsp保存用户状态的机制:

    (1)Session:服务器端 内存 Object 短时销毁
    (2)Cookie: 客户端 文本 String  长期保存
    

    3.Cookie:

    (1)作用:
    	a.追踪
    	b.保存记录
    	c.简化登录
    (2)创建与使用:
    	a.创建:new Cookie(String key, Object value);
    	b.写入:response.addCookie(cookie_Object);
    	c.读取:request.getCookies();//返回一个Cookie数组
    (3)常用方法:
    	a.setMaxAge(int time);//单位是秒
    	b.setValue(String value);
    	c.getName();
    	d.getValue();
    	e.getMaxAge();
    

    五、Jsp指令与动作

    1.include:

    (1)指令:

    <%@include file="url"%>;
    

    (2)动作:

    <jsp:inlcude page="url" flush="true|false"/>;
    

    (3)比较:

    2.forward:

    <jsp:forward page="url" />;
    <%request.getRequestDispatcher("url").forward(request,response)%>;
    

    3.param:

    <jsp:param value="" name="" />;
  • 相关阅读:
    sql月,年,统计报表sql报表
    Sql server在cmd下的使用
    c# 批量处理数据录入
    vmware安装64位系统“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”的问题
    以太坊去中心化淘宝智能合约案例
    智能合约入门
    智能资产构建去中心化的资产管理系统
    以太坊搭建联盟链
    web 前端
    EasyUI+bootsrtap混合前端框架
  • 原文地址:https://www.cnblogs.com/apolloqq/p/5442857.html
Copyright © 2011-2022 走看看