检查作业
相关代码:
package com.imooc.homework;
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.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class Homework1
*/
@WebServlet("/Homework1")
public class Homework1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Homework1() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Map<String,String> map = new HashMap<String,String>();
map.put("apple", "苹果");
map.put("banana", "香蕉");
map.put("peach", "桃子");
HttpSession session = request.getSession();
System.out.println(request.getParameter("key"));
if(map.containsKey(request.getParameter("key")))
{
session.setAttribute("value", map.get(request.getParameter("key")));
}
else session.setAttribute("value", "没有找到对应的单词解释");
request.getRequestDispatcher("/homework.jsp").forward(request, response);
}
}
相关代码:
<%@ 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>
<%! static int i =0; %>
<%if(i==0) {%>
<form action="/FirstProject/Homework1">
<input name = "key" type = "text" placeholder = "请输入要查询的单词">
<input type = "submit" value = "查询">
</form>
<% }i++; %>
<%
out.println(""+session.getAttribute("value"));
%>
</body>
</html>
14
收起
正在回答
1回答
同学你好,同学的代码完成了题目效果,但是没有按照题目要求来写。题目要求中有三个页面,一个查询页面,一个查询成功的success.jsp页面,一个查询失败的fail.jsp页面。同学用一个页面完成了,造成访问页面时,查询框下会提示null,如:
另外题目要求,查询成功存储到request中,转发到success.jsp页面,查询失败,存储到session中,重定向到fail.jsp页面,如:
同学的代码无论查询成功或者失败,都存储到session中,转发到页面显示。
祝学习愉快~
java工程师2020版
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星