请教老师,Servlet页面怎么存取HashSet键值对?

请教老师,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>


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,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>

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 慕粉1465475474 提问者 #1
    boolean flag = map.containsKey(word); 和 request.setAttribute("word", map.get(word));里面的word是什么意思?我怎么老是往英文(apple,banana,pear)上理解呢?
    2020-03-29 20:52:29
  • 好帮手慕阿慧 回复 提问者 慕粉1465475474 #2
    同学你好, 1、html页面有:<input type="text" name="word" placeholder="请输入要查询的单词" />。所以表单提交时会提交这个输入框的数据, 2、在servlet中request.getParameter("word");是在获取表单中的数据,所要查询的单词。 3、 map.containsKey(word); 是在查看map中是否有键word(如:apple)。 4、request.setAttribute("word", map.get(word));是在将查询到的结果存储到request中。其中键为word字符串,值为在map中根据键word获取的值(如:苹果)。
    2020-03-30 09:42:34
  • 慕粉1465475474 提问者 #3
    好的,我明白了,感谢老师!
    2020-03-30 10:20:04
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师