使用HashMap没有获取到值
来源:2-9 自由编程
慕容6190480
2019-08-09 13:25:01
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${requestScope.d }
<c:choose>
<c:when test="${d=='Monday'}">
<h2 style="color:bulue"> 星期一</h2>
</c:when>
<c:otherwise>
<h2 style="color:red">内容不对!</h2>
</c:otherwise>
</c:choose>
</body>
</html>package com.imooc.jstl;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class JstlServlet
*/
@WebServlet("/Jstl")
public class JstlServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public JstlServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("score", 58);
request.setAttribute("grade", "D");
request.setAttribute("X", 5);
Map<String,String> day=new HashMap<String,String>();
day.put("Monday","星期一");
day.put("Tuesday", "星期二");
day.put("Wednesday", "星期三");
day.put("Thursday", "星期四");
day.put("Friday", "星期五");
day.put("Saturday", "星期六");
day.put("Sun", "星期日");
Set<String> d=day.keySet();
request.setAttribute("day",d);
request.getRequestDispatcher("/day.jsp").forward(request, response);
}
}
http://localhost:8080/jstl/day.jsp
页面显示这条信息,这个是要循环遍历才能实现吧?不知道该怎么操作了。1回答
好帮手慕柯南
2019-08-09
同学你好!
这个练习不需要写servlet呢,只需要在jsp页面中完成就可以
在jsp文件中使用<% %>java代码块,在其中保存一个值
使用<c:choose>来对比保存的值是星期几
同学可以参考这个问答完成一下:https://class.imooc.com/course/qadetail/128918
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题