jsp的九大内置对象和El表达式常见api

jsp的九大内置对象为:

  1. pageContext

  1. request

  1. response

  1. session

  1. out

  1. page

  1. application

  1. config

  1. exception

一、pageContext的使用

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    example01



<%
    pageContext.setAttribute("users","王祖贤");
    HttpServletRequest request1=(HttpServletRequest)pageContext.getRequest();
    request1.setAttribute("goods","打印机");

    HttpSession session1=(HttpSession) pageContext.getSession();
    session1.setAttribute("animals","十块");

%>

页面上下文对象:<%=pageContext.getAttribute("users")%>

请求对象数据: <%=request1.getAttribute("goods") %>

最爱的宠物: <%=session1.getAttribute("animals")%>

二、request的使用

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


<%request.setAttribute("users","小红");%>
<%=request.getAttribute("users")%>
 <%=request.getCharacterEncoding()%>

<%=request.getLocalAddr()%> <%=request.getLocalPort()%> <%=request.getMethod()%> <%=request.getProtocol()%> <%=request.getServletContext()%> <%=request.getRequestURL()%> <%=request.getRequestDispatcher("index.jsp")%>

三、response的使用

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Response服务器对客户端的响应


<%=response.setCharacterEncoding("UTF-8")%>
<%=response.sendRedirect("index.jsp")%>

<%
    PrintWriter out1 =response.getWriter();
    out1.print("Hello World!!");
%>

四、session的使用

<%@ page contentType="text/html;charset=UTF-8" language="java" import="com.openlab.*" %>


    Session



<%
    Card card=new Card();
    card.setId(1);card.setName("Coco");card.setNum(001);
    session.setAttribute("cards",card);
%>

<%=((Card)session.getAttribute("cards")).getId()%>
<%=((Card)session.getAttribute("cards")).getName()%>
<%=((Card)session.getAttribute("cards")).getNum()%>
<%=session.getId()%>
<%=session.getCreationTime()%>
<%=session.getLastAccessedTime()%>

五、out的使用

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Out


<%=out.println("我是页面默认的输出对象")%>


六、page的使用

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Page



<%=page.hashCode()%>


七、application的使用

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Application



<%application.setAttribute("man","大帅哥");%>
<%session.setAttribute("woman","大美女");%>

<%=application.getContextPath()%>

<%=application.getRealPath("index.jsp")%> <%=application.getSessionTimeout()%> < <%=application.getServletContextName()%>

八、config的使用

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Config



<%=config.getInitParameter("param1")%>
<%=config.getInitParameter("param2")%>

<%=config.getServletContext().getContextPath()%>

九、exception的使用

<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>


    exception




El表达式常见api

配置EL的方式:

  1. 在<%page %>里加上 isELIgnored="false" 。

  1. 在web.xml里加上代码块


        
            *.jsp
            false
        
    

EL表达式

<%pageContext.setAttribute("age","23");%>
<%request.setAttribute("username","张明");%>
<%session.setAttribute("salary","6000");%>
<%application.setAttribute("address","西安");%>

${age}
${pageScope.age}

常见的api

  1. 算数运算符

算数运算符

====================================================================================== ${2+2}
${2*2}
${100/9}
${100%10}
  1. 比较运算符

比较运算符

===================================================================================== ${10>100}
${10<9}
${20<=20}
${100>90 && 200<300}
${100>90 and 200<300}
${empty requestScope.username}
${100>99?1:2}

你可能感兴趣的:(Java,Web,java,tomcat,servlet,eclipse,java-ee)