Java Web base notes (4)

 Eight expression language ----------------------------------------- ----- -------------------------------------------------- ---------
    expression language is JSP2.0 the new features, the use of the expression language can easily access flag (providing a total page (pageContext) in the jsp, request, session and application four flag species)
    expression using language attribute name} {$ If the output is automatically null empty string "" indicates
    ----------------------- ----------------- --------------------------- built-in object expression language ---------------------------
    provides a number of built-in expression language objects:
        pageContext represent pageContext object
        pageScope means the search range output from the page properties
        requestScope a lookup request output attribute from the attribute range
        sessionScope a lookup output attribute from the session attribute range
        applicationScope lookup output attribute indicates attribute range from application
        param accepts parameters passed to this page
        paramValues receiving a set of parameters passed to this page
        receiving a data header information header
        headerValues receiving a data group header
        cookie in the cookie data extracted
        initParam initialization parameters acquired configuration
    $ {name} attribute properties can be obtained from various attributes stored range, the default order is page-> request- > session-> application
    if you want to find the range specified properties can be $ {requestScope format attribute name}
    call built-in objects: use pageContext available request, session, application examples
        pageContext.session format accomplished by reflection
        <% @ contentType = Page "text / HTML" pageEncoding = "GBK"%>
        <HTML>
        <head> <title> www.mldnjava.cn, MLDN high-end Java training </ title> </ head>
        <body>
        <h3> IP address : $ {pageContext.request.remoteAddr} </ H3>
        <H3> the SESSION ID: $ {pageContext.session.id} </h3>
        <h3>是否是新session:${pageContext.session.new}</h3>
        </body>
        </html>

Receiving {param. Request Parameter Name Parameter} $ $ {paramValues.} Parameter name
    directly receiving a single output parameter, a plurality of parameters required to specify the index
   

     <%@ page contentType="text/html" pageEncoding="GBK"%>
        <html>
        <head><title></title></head>
        <body>
        <%    
            request.setCharacterEncoding("GBK") ;
        %>//参数名为inst
        <h3>第一个参数:${paramValues.inst[0]}</h3>
        <h3>第二个参数:${paramValues.inst[1]}</h3>
        <h3>第三个参数:${paramValues.inst[2]}</h3>
        </body>
        </html>

Collective operation
    output collication
   

    <%@ page contentType="text/html" pageEncoding="GBK"%>
        <%@ page import="java.util.*"%>
        <html>
        <head><title> </title></head>
        <body>
        <%
            List all = new ArrayList() ;
            all.add("qwe") ;
            all.add("www.MLDNJAVA.cn") ;
            all.add("[email protected]") ;
            request.setAttribute("allinfo",all) ;    // 集合保存在request范围
        %>
        <h3>第一个元素:${allinfo[0]}</h3>
        <h3>第二个元素:${allinfo[1]}</h3>
        <h3>第三个元素:${allinfo[2]}</h3>
        </body>
        </html>

Map output by $ {info.mldn} or $ {info [ "mldn"]} way
   

    <%@ page contentType="text/html" pageEncoding="GBK"%>
        <%@ page import="java.util.*"%>
        <html>
        <head><title> </title></head>
        <body>
        <%
            Map map = new HashMap() ;
            map.put("qwe","qwe") ;
            map.put("www","www.M.cn") ;
            map.put("email","[email protected]") ;
            request.setAttribute("info",map) ;    // 集合保存在request范围
        %>
        <h3>KEY为qwe的内容:${info["qwe"]}</h3>
        <h3>KEY为www的内容:${info.www}</h3>
        <h3>KEY为email的内容:${info["email"]}</h3>
        </body>
        </html>

In MVC application expression language that can be stored in the range of reflection properties of Java objects through direct access content
 

       <%@ page contentType="text/html" pageEncoding="GBK"%>
        <%@ page import="org.lxh.eldemo.vo.*"%>
        <html>
        <head><title> </title></head>
        <body>
        <%    // 现在假设这些代码是由Servlet完成
            Dept dept = new Dept() ;
            dept.setDeptno(10) ;
            dept.setDname(" 教学部") ;
            dept.setLoc(" 西城区") ;
            request.setAttribute("deptinfo",dept) ;
        %>
        <h3>部门编号:${deptinfo.deptno}</h3>
        <h3>部门名称:${deptinfo.dname}</h3>
        <h3>部门位置:${deptinfo.loc}</h3>
        </body>
        </html>

Transfer request attributes Servlet
       

import com.hsd.eldemo.vo.* ;
        import javax.servlet.* ;
        import javax.servlet.http.* ;
        public class ELServlet extends HttpServlet {
            public void doGet(HttpServletRequest request,HttpServletResponse response) throws java.io.IOException,ServletException{
                Dept dept = new Dept() ;
                dept.setDeptno(10) ;
                dept.setDname(" 教学部") ;
                dept.setLoc(" 西城区") ;
                request.setAttribute("deptinfo",dept) ;
                request.getRequestDispatcher("dept_info.jsp").forward(request,response) ;
            }
            public void doPost(HttpServletRequest request,HttpServletResponse response) throws java.io.IOException,ServletException{
                this.doGet(request,response) ;
            }
        }

// JSP reception
 

        <%@ page contentType="text/html" pageEncoding="GBK"%>
        <html>
        <head><title> </title></head>
        <body>
        <h3>部门编号:${deptinfo.deptno}</h3>
        <h3>部门名称:${deptinfo.dname}</h3>
        <h3>部门位置:${deptinfo.loc}</h3>
        </body>
        </html>

 

Guess you like

Origin blog.csdn.net/qq_39156722/article/details/93406270