页面显示null
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="servlet-advanced/AppleServlet"> <input type="text" placeholder="请输入要查询的单词"> <input type="submit" value="查询"> </form> </body> </html>
package com.imooc.servlet.apple;
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;
/**
* Servlet implementation class AppleServlet
*/
@WebServlet("/AS")
public class AppleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AppleServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String,String> apple=new HashMap<String,String>();
apple.put("Apple", "苹果");
apple.put("grape", "葡萄");
apple.put("orange", "橘子");
String ap=request.getParameter("ap");
//response.setContentType("text/html;charset=uft-8");
Boolean flag=false;
String result=null;
if(apple.containsKey(ap)) {
flag=true;
result=apple.get(ap);
}
else {
result="没有找到对应的单词!";
}
if(flag) {
request.setAttribute("result",result);
request.getRequestDispatcher("/success.jsp");
}
else {
HttpSession hs=request.getSession();
hs.setAttribute("result", result);
response.sendRedirect("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>
<p style="color:blue;font-size:30px;font-weight:bold;">
<%=request.getAttribute("result") %>
</p>
</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>
<p style="cplor:red;font-size:30px;font-weight:bole;">
<%=request.getAttribute("result") %>
</p>
</body>
</html>
第一次浏览地址:http://localhost:8080/servlet-advanced/Apple.html
第二次浏览地址:localhost:8080/servlet-advanced/AS
跳转:http://localhost:8080/servlet-advanced/fail.jsp,显示为null,请老师帮看下哪边问题。
正在回答 回答被采纳积分+1
package com.imooc.servlet.apple;
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;
/**
* Servlet implementation class AppleServlet
*/
@WebServlet("/AS")
public class AppleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AppleServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String,String> apple=new HashMap<String,String>();
apple.put("Apple", "苹果");
apple.put("grape", "葡萄");
apple.put("orange", "橘子");
String ap=request.getParameter("ap");
// response.setContentType("text/html;charset=uft-8");
Boolean flag=false;
String result=null;
if(apple.containsKey(ap)) {
flag=true;
result=apple.get(ap);
// response.getWriter().println(result);
}
else {
result="没有找到对应的单词!";
}
if(flag) {
request.setAttribute("result",result);
request.getRequestDispatcher("/success.jsp").forward(request,response);
}
else {
HttpSession hs=request.getSession();
hs.setAttribute("result", result);
response.sendRedirect("/fail.jsp");
}
}
}
http://localhost:8080/servlet-advanced/Apple.html
结果显示:
没有找到对应的单词!
还是没有获取到对应的值,哪边有问题?
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星