不知道为什么,每次输入单词然后就自动回到eclipse界面了
package com.imooc.servlet.search;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
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 java.util.HashMap;
import java.util.HashMap;
/**
* Servlet implementation class Search
*/
@WebServlet("/Search")
public class Search extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Search() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HashMap<String, String> map=new HashMap<String, String>();
map.put("apple", "苹果");
map.put("banana", "香蕉");
map.put("watermelon","西瓜");
String dictionary=(String)request.getParameter("dictionary");
if(map.containsKey(dictionary)==true) {
String result=(String)map.get(dictionary);
request.setAttribute("dictionary", dictionary);
request.setAttribute("result", result);
request.getRequestDispatcher("/servlet_advanced/search/success.jsp").forward(request, response);
}else {
request.getRequestDispatcher("/servlet_advanced/search/fail.jsp").forward(request, response);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(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>
<form action="/servlet_advanced/Search">
<input type="text" name="dictionary" placeholder="请输入要查询的单词" >
<input type="submit" value="查询">
</form>
</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>
<%
String dictionary=(String)request.getAttribute("dictionary");
String result=(String)request.getAttribute("result");
out.println("<h1 style='blue'>"+dictionary+":"+result+"</h1>");
%>
</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 style="red">
没有找到对应的单词解释
</body>
</html>
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星