基础进阶
Java Web基础进阶
EL表达式
EL表达式:Expression Language(表达式语言),目的是替换JSP页面中的复杂代码。
EL表达式语法
${变量名} 变量是request域中存储的对象(在servlet中通过request.setAttribute()存储)
代码示例
在另一个页面中显示:
姓名:${name}
年龄:${age}
servlet:
public class ELServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String age = request.getParameter("age");
//将获取到的数据保存到request域中
request.setAttribute("name", name);
request.setAttribute("age", age);
request.getRequestDispatcher("/2.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
JSTL标签
JSTL概述
JSTL:(JavaServerPage Standard Tag Library)Java标准标签库
JSTL通常会与EL表达式合作实现JSP页面的编码
使用JSTL,需要在JSP页面中添加taglib指令:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
为什么要使用JSTL标签
-
在jsp中不建议直接写java代码(规范性问题)
-
EL表达式虽然可以解决不用书写Java代码的问题,但是对于复杂的数据(比如数组、集合等取值会很麻烦
-
使用JSTL标签配合EL表达式能够很好的解决复杂类型数据的问题,简化代码书写
通用标签
set标签:将值保存到指定范围里
<c:set var="username" value="张三" scope="scope" />
将value值存储到范围为scope的变量var中
out标签:将结果输出显示
<c:out value="value" />
remove标签:删除指定域内的数据
<c:remove var="username" scope="session" />
示例代码
<c:set var="username" value="张三" scope="request" />
<c:out value="${username}" />
<c:remove var="username" scope="request" />
<c:out value="${username}" />
条件标签
示例代码
<c:set var="age" value="13" scope="request" />
<c:if test="${age==12 }">
年龄为12岁
</c:if>
hello world
<c:choose>
<c:when test="${age==12 }">
您的年龄是12岁
</c:when>
<c:otherwise>
你的年龄不是12岁
</c:otherwise>
</c:choose>
迭代标签
示例代码
<c:forEach items="${list}" var="Map">
</c:forEach>
| 产品 | 产地 | 价格 |
| ${Map.shopName} | ${Map.address} | ${Map.price} |
public class JSTLServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("shopName", "联想笔记本");
map1.put("address", "北京");
map1.put("price", 4999.99);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("shopName", "神州笔记本");
map2.put("address", "南京");
map2.put("price", 3999.99);
List<Map<String, Object>> list = new ArrayList<>();
list.add(map1);
list.add(map2);
request.setAttribute("list", list);
request.getRequestDispatcher("/5.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Ajax入门
什么是Ajax
通过在后台与服务器进行少量数据交换,Ajax可以使网页实现异步更新
传统的网页(不使用Ajax)如果需要更新内容的话,必须重载整个网页页面。
Ajax的好处
- 更新时只需要局部刷新,用户体验度好
- 由于只需要刷新局部的数据,对后台服务器的压力较小
Ajax语法总结
- url:请求资源的地址
- type:请求时数据的传递方式(常用的有get/post)
- data:用来传递的数据(建议使用json传递)
- success:与servlet交互成功后要执行的方法
- dataType:ajax接收后台数据的类型(建议使用json)
注意事项
ajax和后台交互的时候,后台是不能直接跳转到其他页面的
代码示例
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、首先获取jsp页面传递过来的参数信息
String username = request.getParameter("username");
String password = request.getParameter("password");
//2、如果username="15912345678",password="12345678"则登录成功,否则登录失败
JSONObject jsonObject = null;
if("15912345678".equals(username) && "12345678".equals(password)){
System.out.println("username="+username);
System.out.println("password="+password);
jsonObject = new JSONObject("{flag:true}");
}else{
//如果登录失败,则给ajax返回数据
jsonObject = new JSONObject("{flag:false}");
}
response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
jsp页面中的代码
<%--

浙公网安备 33010602011771号