老师看看还有什么需要修改的吗???
<%@ 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/iq" method="post"> <input name="name" placeholder="请输入你要查询的单词"> <input type="submit" value="查询"> </form> </body> </html>
package com.imooc.servlet.test;
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 javax.servlet.http.HttpSession;
import javax.websocket.Session;
/**
* Servlet implementation class InquireServlet
*/
@WebServlet("/iq")
public class InquireServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public InquireServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String, String> map=new HashMap<String, String>();
map.put("apple", "苹果");
map.put("banana", "香蕉");
map.put("peach", "桃子");
String name= request.getParameter("name");
if(map.containsKey(name)) {
request.setAttribute("name", map.get(name));
request.getRequestDispatcher("/inquire/success.jsp").forward(request, response);
}else {
HttpSession session=request.getSession();
session.setAttribute("name", "没有找到对应的单词解释");
response.sendRedirect("/servlet_advanced/inquire/fail.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>
<%
String name=(String) request.getAttribute("name");
out.println("<h1>"+name+"</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>
<%
String name=(String)session.getAttribute("name");
out.println("<h1 style='color:red'>"+name+"</h1>");
%>
</body>
</html>



0
收起
正在回答 回答被采纳积分+1
2回答
慕尼黑7895541
2019-03-20 15:43:11
为什么你请求转发会到成功页面会改变地址栏中的url?
chrismorgen
2019-03-04 15:01:58
效果完成的很棒,加油,祝学习愉快~
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星