重定向异常
built_in.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>
<form action="/servlet_advanced/servlet/enquery" method="get">
<input type="text" name="key1" id="key1" placeholder="请输入要查询的单词"/>
<input type="submit" value="查询">
</form>
</body>
</html>
EnqueryServlet.java
package com.imooc.servlet.enquire;
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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class EnqueryServlet
*/
@WebServlet({ "/EnqueryServlet", "/servlet/enquery" })
public class EnqueryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public EnqueryServlet() {
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> map=new HashMap<String,String>();
map.put("apple","苹果");
map.put("android","安卓");
map.put("HuaWei", "华为");
Set<String> keySet=map.keySet();
String key1=request.getParameter("key1");
//boolean flag=false;
String res=null;
String res1=null;
for(String key:keySet) {
if(key.equals(key1)) {
res=map.get(key1);
request.setAttribute("value", res);
request.getRequestDispatcher("/success.jsp").forward(request, response);
}else if(!(keySet.contains(key1))){
res1=" 没有找到对应的单词解释!";
HttpSession session=request.getSession();
session.setAttribute("value1", res1);
response.setContentType("text/html;charset:utf-8");
response.sendRedirect("/servlet_advanced/fail.jsp");
}
}
}
}
success.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>
<% response.getWriter().println("<h2 style='color:blue'>"+(String)request.getAttribute("value")+"</h2>");
%>
</body>
</html>
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>
<% out.println("<h2 style='color:red'>"+(String)session.getAttribute("value1")+"</h2>");
%>
</body>
</html>
正在回答 回答被采纳积分+1
同学你好,根据同学贴出的报错信息,Cannot call sendRedirect() after the response has been committed-->提交响应后不能调用sendRedirect()
对于http的response,如果已经进行了提交(重定向,请求转发),则不能对response再进行任何操作,比如修改,比如再次提交。而对于已经提交的resposne或者请求转发,该方法并不会结束,它依然会继续往下执行,所以如果你在提交后继续执行response.sendRedirect就会报出这个错误。
所以同学可以在response.sendRedirect后加一个return表示执行完成,不再继续
如上所示修改后来试试。
祝学习愉快!
- 参与学习 人
- 提交作业 9400 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星