<%%>,可以在其中写Java代码
用<%%>是写在方法中的,写在service方法中
- <%
- List list = new ArrayList();
- list.add(1);
- list.add(2);
- list.add(3);
- list.add("a");
-
- for (Object o:list) {
- System.out.println(o);
- }
-
- //定义一个私有变量,不能有修饰符,方法中不能有修饰符修饰的变量
- // private String name;
- // public String namee;
- String name1;
- %>
\<%!%>,可以写java代码 是写在service方法的外面 第一种的里面可以用这个定义的共有变量
- <%!
- private String name;
- public int age = 17;
-
- %>
想把Java对象展示到浏览器上,<%=%>
- <%!
- private String name;
- public int age = 17;
-
- %>
-
- <%=age%>
- el出现的目的
- 方便我们从域中取出对象
使用el表达式获取域中的值,直接把key放在大括号中
默认规则:如果没有指定在哪个域中取数据,默认先从最小的域中取,如果没有再去取大一点的域中找
指定的域中找: el表达式内置域对象
- <%
- //像各个域中存放数据
- pageContext.setAttribute("username","pageContext");
- request.setAttribute("username","request");
- session.setAttribute("username","session");
- application.setAttribute("username","application");
-
- %>
- ${pageScope.username}
- ${requestScope.username}
- ${sessionScope.username}
- ${applicationScope.username}
在域中使用el表达式获取Java对象
使用el表达式获取person对象:底层依据,依赖的是get方法
- //获取person对象
- <%
- Person person = new Person();
- person.setName("zs");
- person.setAge(17);
- person.setAddress("成都市金牛区");
- //把Java对象放在域中,请求域
- request.setAttribute("person",person);
- %>
-
- ${person.name}
- ${person.age}
- ${person.address}
- ${person.sala}
-
-
-
- <%--如何从map从取数据--%>
-
- <%
- HashMap<String, String> map = new HashMap<>();
- map.put("username","admin");
- map.put("password","123");
- map.put("sex","1");
- // 把map放在请求域中
- request.setAttribute("map",map);
-
- %>
-
-
- <%--获取map中的值--%>
- ${map.username}
- ${map.password}
- ${map.sex}
-
-
- <%
- ArrayList<String> list = new ArrayList<>();
- list.add("1");
- list.add("2");
- list.add("3");
- request.setAttribute("list",list);
- %>
-
-
- <%--获取集合中的数据--%>
- ${list[0]}
- ${list[1]}
- ${list[2]}