6-3 自由编程

来源:6-3 自由编程

jia_蛙

2019-11-08 22:22:40

index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	<form action="/dictservlet/dict/verify" method="get">	
		<input name="word" placeholder="请输入要查询的单词">
		<input type="submit" value="查询">
	</form>
</body>
</html>
success.jsp

<%@ page contentType="text/html; charset=utf-8" %>
<span style="color:red; font-size:2em"><%=session.getAttribute("value")%></span>
fail.jsp

<%@ page contentType="text/html; charset=utf-8" %>
<%
	String word = (String) request.getAttribute("word");
%>
<label style="color:blue; font-size:2em"><%=word%></label>
DictVerify.java

package com.zhou.servlet.dict;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class DictVerify
 */
@WebServlet("/dict/verify")
public class DictVerify extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DictVerify() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//准备工作:定义HashMap并存入一些单词
		HashMap<String,String> hm = new HashMap<String,String>();
		hm.put("apple", "苹果");
		hm.put("banana","香蕉");
		hm.put("orange","橘子");
		
		//使用getParameter()方法获取属性值
		String word =  request.getParameter("word");
		boolean isNo = hm.containsKey(word);
		
		if(isNo) {
			request.setAttribute("word", hm.get(word));
			System.out.println(hm.get(word));
			request.getRequestDispatcher("/result/fail.jsp").forward(request, response);
		}else {
			HttpSession session = request.getSession();
			session.setAttribute("value", "没有找到对应的单词解释");
			response.sendRedirect("/dictservlet/result/success.jsp");
		}
	}
	

}

http://img.mukewang.com/climg/5dc57a0809c2109503340408.jpg

老师 你看一下哪有有没有需要修改的


写回答

1回答

好帮手慕柯南

2019-11-09

同学你好!

  1. 代码功能完成正确

  2. 目录结构整洁明了,文件以及类命名规范

    加油,很棒!

祝学习愉快~

0

0 学习 · 9666 问题

查看课程

相似问题