老师看下我哪里错了
1 2 3 4 5 6 7 8 9 10 11 | <!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > < title >Insert title here</ title > </ head > < body > < input type = "text" name = "word" placeholder = "请输入要查询的单词" > < input type = "submit" value = "查询" > </ body > </ html > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package com.imooc.servlet; 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 CheckServlet */ @WebServlet ( "/cs" ) public class CheckServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public CheckServlet() { super (); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String key=request.getParameter( "word" ); HashMap<String,String> word= new HashMap<String,String>(); word.put( "apple" , "苹果" ); word.put( "pear" , "梨" ); word.put( "orange" , "橙子" ); response.setContentType( "text/html;charset=utf-8" ); boolean flag=word.containsKey(key); if (flag== true ) { request.setAttribute( "key" , word.get(key)); request.getRequestDispatcher( "/success.jsp" ).forward(request, response); } else { HttpSession session=request.getSession(); session.setAttribute( "fail" , "没有找到对应的单词解释" ); request.getRequestDispatcher( "/fail.jsp" ).forward(request, response); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <%@ 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 > <% out.println(request.getAttribute("key")); %> </ body > </ html > <%@ 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 > <% out.println(session.getAttribute("fail")); %> </ body > </ html > |
3
收起
正在回答
3回答
同学你好,1、404错误,是没有找到对应地址下的文件,这里同学在地址栏中输入CheckWord/index.html没有找到,同学是否是文件名写错了呐,建议同学将自己的目录结构截图贴出,这里老师测试贴出代码,是能够打开这个index.html页面呐。
2、在index.html页面中,注意form表单的提交,否则请求无法正常发送到CheckServlet中,修改建议如下:
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧