您的位置:首页 > Web前端 > JavaScript

JSP基础

2018-04-02 22:07 597 查看

JSP

taglib

作用:在JSP页面中导入JSTL标签库,替换jsp中的java代码片段
需要导入jar包
jstl.jar  standard.jar
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


jsp的6个动作

<jsp:include > 动态包含
<jsp:forward> 请求转发
<jsp:param> 设置请求参数

<jsp:useBean> 创建一个对象
<jsp:setProperty> 给指定的对象属性赋值
<jsp:getProperty> 取出指定对象的属性值


jsp的9个内置对象

request
response
session
application
exception
page 当前servlet实例 this
config
out
pageContext 指当前页面 域对象 然并卵
void setAttribute(String name,Object o);
Object getAttribute(String name);
void removeAttribute(String name);


操作其它域对象的方法

void setAttribute(String name,Object o,int Scope);
Object getAttribute(String name,int Scope);
void removeAttribute(String name,int Scope);


scope的值

PageContext.PAGE_SCOPE
PageContext.REQUEST_SCOPE
PageContext.SESSION_SCOPE
PageContext.APPLICATION_SCOPE


域之间的查找顺序

pageContext.findAttribute("p")方法

<body>
<%
pageContext.setAttribute("p", "pageContext", PageContext.PAGE_SCOPE);
pageContext.setAttribute("p", "request",PageContext.REQUEST_SCOPE);
pageContext.setAttribute("p", "session",PageContext.SESSION_SCOPE);
pageContext.setAttribute("p", "Application",PageContext.APPLICATION_SCOPE);

%>

<%=pageContext.findAttribute("p") %>

<%=PageContext.PAGE_SCOPE %>
<%=PageContext.REQUEST_SCOPE %>
<%=PageContext.SESSION_SCOPE %>
<%=PageContext.APPLICATION_SCOPE %>
</body>


结果



注释掉PAGE_SCORE结果



再注释掉REQUEST_SCORE结果



再注释掉SESSION_SCORE结果



7session一旦建立 便会持续存在一段时间,不会随着代码的删除而消失

<body>
<%
//pageContext.setAttribute("p", "pageContext", PageContext.PAGE_SCOPE);
//pageContext.setAttribute("p", "request",PageContext.REQUEST_SCOPE);
//pageContext.setAttribute("p", "session",PageContext.SESSION_SCOPE);
pageContext.setAttribute("p", "Application",PageContext.APPLICATION_SCOPE);

%>

<%=pageContext.findAttribute("p") %>

<%=PageContext.PAGE_SCOPE %>
<%=PageContext.REQUEST_SCOPE %>
<%=PageContext.SESSION_SCOPE %>
<%=PageContext.APPLICATION_SCOPE %>
</body>


实际开发使用的域对象

PageContext : pageConext 存放的数据在当前页面有效。开发时使用较少。
ServletRequest: request  存放的数据在一次请求(转发)内有效。使用非常多。
HttpSession: session 存放的数据在一次会话中有效。使用的比较多。如:存放用户的登录信息
ServletContext: application 存放的数据在整个应用范围内都有效。因为范围太大,应尽量少用。


PageContext

该域对象 只能在本页面有效 出了页面获取不到
1.jsp:
<body>
<%
pageContext.setAttribute("page", "zhangfei");
%>
<%=pageContext.getAttribute("page")%>
</body>


结果截图



2.jsp:
<body>
<%=pageContext.getAttribute("page") %>
</body>


结果截图



域之间的获取关系

有一个pageContext域 便可以获取其它三个域
pageContext.getRequest();
pageContext.getSession();
pageContext.getServletContext();

有一个request域 可以获取其它两个域
request.getSession();
request.getServletContext();

有一个session域 可以获取另一个域
session.getServletContext();


jsp动作标签之转发

携带参数相当于在网址后面进行拼接
/2.jsp?username=zhangfei&password=123456
携带请求参数 尽量不要在jsp中间加注释

1.jsp
<body>
<jsp:forward page="/2.jsp">
<jsp:param name="username" value="zhangfei" />
<jsp:param name="password" value="123456" />
</jsp:forward>
</body

获取转发的参数
是保存在参数中 而不是域中
2.jsp
<body>
<%=request.getParameter("username") %>
<%=request.getParameter("password") %>
</body>


结果截图



el表达式

EL表达式:expression language 表达式语言
要简化jsp中java代码开发。
它不是一种开发语言,是jsp中获取数据的一种规范

功能
获取数据
EL表达式能获取存在4个作用域中的数据
${u} 相当于 pageContext.findAttribute("u");
EL获取对于null这样的数据,在页面中表现为空字符串

测试把对象存到域对象中是否能获取?
${u.name} == u.getName()方法
点(.) 运算符相当于调了getter方法,点后页面跟的是属性名。
属性导航
${u.address.city}
[]运算符:点能做的,它也能做; 它能做的,点不一定能做(测试把集合存储到域对象中能否取出来)
${student.name}== ${student['name']} == ${student["name"]}

${p}实际上就是使用pageContext.findAttribute这个方法在域中查找的


通过”.” 获取对象的属性值

建立一个user类,里面有两个属性 username password

<body>
<%
User user = new User();
user.setUsername("zhangfei");
user.setPassword("123456");
request.setAttribute("u", user);
%>

${u.username }
${u.password }
</body>


结果



通过中括号获取属性值

<body>
<%
User user = new User();
user.setUsername("zhangfei");
user.setPassword("123456");
Address a = new Address();
a.setCity("beijing");
user.setAddress(a);
request.setAttribute("u", user);
%>

${u.address.city }
${u["username"] }
${u['password'] }
</body>


结果



集合只能使用[]

<body>
<%
ArrayList<String> list = new ArrayList<String>();
list.add("aaaa");
list.add("bbbb");
list.add("cccc");
list.add("dddd");

HashMap<String,String> map = new HashMap<String,String>();
map.put("1", "qqqq");
map.put("2", "wwww");
map.put("3", "eeee");
map.put("4", "rrrr");

request.setAttribute("list", list);
request.setAttribute("map", map);
%>

${list[1] }

${map[1] }
${map[2] }

</body>


结果



${list[1] }

${map["1"] }
${map['2'] }


结果



使用el表达式 判断 empty 空值

只要为空 就返回true 只要里面没有存值 就返回空

<body>
<%
String str1 = null;
request.setAttribute("str1", str1);

String str2 = "";
request.setAttribute("str2", str2);

String str3 = "asdfg";
request.setAttribute("str3", str3);

ArrayList<String> list1 = null;
request.setAttribute("list1", list1);

ArrayList<String> list2 = new ArrayList<String>();
request.setAttribute("list2", list2);

ArrayList<String> list3 = new ArrayList<String>();
list3.add("aaaa");
request.setAttribute("list3", list3);
%>

${empty str1 }
${empty str2 }
${empty str3 }

${empty list1 }
${empty list2 }
${empty list3 }

${empty list3 ? "我是前面的":"我是后面的" }
</body>


结果



el的应用

根据域中的值 来默认选中男女
<body>
<%
String sex = "nan";
request.setAttribute("s", sex);
%>
<input type="radio" name="sex" va
e309
lue="nan" ${s=="nan" ? "checked=checked":"" } />男
<input type="radio" name="sex" value="nv" ${s=="nv" ? "checked=checked":"" } />女
</body>


结果



隐式对象

EL表达式 的隐式对象
pageContext(对象)

pageScope

requestScope

sessionScope

applicationScope

param ${param.userName } request.getParamemter("userName");

paramValues

header ${header["User-agent"]}

headerValues

cookie ${cookie.JSESSIONID.value } 测试不加value


<body>
<%
pageContext.setAttribute("p", "pageContext", PageContext.PAGE_SCOPE);
pageContext.setAttribute("p", "request", PageContext.REQUEST_SCOPE);
pageContext.setAttribute("p", "session", PageContext.SESSION_SCOPE);
pageContext.setAttribute("p", "application", PageContext.APPLICATION_SCOPE);
%>
${pageScope.p }
${requestScope.p }
${sessionScope.p }
${applicationScope.p }
</body>


结果如图



运用举例

8.jsp
<body>
${qwwee }

<form action="${pageContext.request.contextPath }/9.jsp">
用户名:<input type="text" name="username" /><br/>
密码:<input type="text" name="password" /><br/>
<input type="radio" name="hobby" value="篮球" />篮球<br/>
<input type="radio" name="hobby" value="唱歌" />足球<br/>
<input type="submit" value="提交" />
</form>
</body>
9.jsp
<body>
<%
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
%>

${param.username } <br/>
${param.password } <br/>
${paramValues.hobby[0] } <br/>
${header["User-Agent"] } <br/>
${cookie.JSESSIONID.value } <br/>
</body>






JSTL

JSTL
什么是JSTL
JSTL(JavaServerPages Standard Tag Library)JSP标准标签库
JSTL的作用
使用JSTL实现JSP页面中逻辑处理。如判断、循环等。
使用JSTL
需要在JSP页面添加taglib指令 添加核心库
通用标签: set、 out、 remove

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


<c:set var="num" value="100"></c:set>
${num }
<c:out value="${num1 }" default="145"></c:out>


结果如图



条件标签:if choose(没有 else)

<body>
<c:if test="${3<5}">
aaaa
</c:if>

<c:if test="${n>5}">
bbbb
</c:if>

<c:set var="n" value="10"></c:set>
<c:choose>
<c:when test="${n ==5 }">5</c:when>
<c:when test="${n == 10 }">10</c:when>
<c:otherwise>
xxxxx
</c:otherwise>
</c:choose>
</body>


<body>
<%
ArrayList<String> list = new ArrayList<>();
list.add("aaaa");
list.add("bbbb");
list.add("cccc");
list.add("dddd");
list.add("eeee");
request.setAttribute("list",list);
%>
<!-- items 你要遍历的容器 var表示容器中的对象 -->
<c:forEach items="${list }" var="l" step="2">
${l }<br/>
</c:forEach>

<!-- 循环 step -->
<c:forEach var="i" begin="0" end="10" step="2">
${i }<br/>
</c:forEach>

</body>




<body>
<%
ArrayList<String> list = new ArrayList<>();
list.add("aaaa");
list.add("bbbb");
list.add("cccc");
list.add("dddd");
list.add("eeee");
request.setAttribute("list",list);

%>
<table border=1>
<tr>
<th>数据</th>
<th>索引</th>
<th>计数</th>
<th>第一个</th>
<th>最后一个</th>
</tr>

<c:forEach items="${list}" var="l" varStatus="vs">
<tr>
<td>${l }</td>
<td>${vs.index }</td>
<td>${vs.count }</td>
<td>${vs.first }</td>
<td>${vs.last }</td>
</tr>
</c:forEach>
</table>
</body>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jstl jsp scope PageContext $