请教老师,Servlet页面怎么存取HashSet键值对?
//Servlet页面怎么存取HashSet键值对?
//success.jsp页面怎么写?
//求代码,求代码,求代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>查询页面</title>
</head>
<body>
<form action="/servlet_advanced/search" method="get">
<input type="text" name="word" placeholder="请输入要查询的单词" />
<input type="submit" value="查询" />
</form>
</body>
</html>
package com.imooc.exercise;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
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;
@WebServlet("/search")
public class SearchWord extends HttpServlet {
/**
* Constructor of the object.
*/
public SearchWord() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取表单中的单词
String word = request.getParameter("word");
//创建集合
Set set = new HashSet();
set.add("banana");
set.add("apple");
set.add("pear");
if(set.contains(word)){
request.getRequestDispatcher("/success.jsp");
}else{
request.getRequestDispatcher("/fail.jsp");
}
}
}
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>正确页面</title>
</head>
<body>
request.setAttribute("word",word);
<h2 style="color:blue">word</h2>
</body>
</html>
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>错误页面</title>
</head>
<body>
<h2 style="color:red">没有找到对应的单词解释!</h2>
</body>
</html>
正在回答
同学你好,HashSet无法存键值对,HashMap可以存键值对。
1、SearchWord类中doGet方法,代码如下:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取表单中的单词 String word = request.getParameter("word"); //创建集合 Map map = new HashMap<String, String>(); map.put("apple","苹果"); map.put("banana","香蕉"); map.put("pear","梨"); boolean flag = map.containsKey(word); if(flag){ request.setAttribute("word", map.get(word)); request.getRequestDispatcher("/success.jsp").forward(request,response); }else{ //获取到用户会话session对象 HttpSession session = request.getSession(); session.setAttribute("alert", "没有找到对应的单词解释"); response.sendRedirect("fail.jsp"); } }
2、查询成功success.jsp,代码如下:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta charset="UTF-8"> <title>正确页面</title> </head> <body> <% request.getAttribute("word"); String word = (String) request.getAttribute("word"); out.println("<h1 style='color:blue;'>"); out.println(word); out.println("</h1>"); %> </body> </html>
3、查询失败fail.jsp,代码如下:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta charset="UTF-8"> <title>错误页面</title> </head> <body> <% HttpSession session1 = request.getSession(); String alert = (String)session1.getAttribute("alert"); out.println("<h1 style='color:red;'>"); out.println(alert); out.println("</h1>"); %> </body> </html>
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星