此网页有重定向循环错误

时间:2014-12-29 06:45:01

标签: jsp servlets redirect-loop

我在浏览器中收到错误'此网页有一个重定向循环' 当我刚刚运行我的网址为'http://localhost:9090/MyProj/todos/1'

不了解如何解决这个问题,请有人研究一下,并为我提供解决方案。

以下是我的代码:

的web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app>

    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>MyServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/todos/*</url-pattern>
    </servlet-mapping>

</web-app>

MyServlet.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MyServlet
 */
public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public MyServlet() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.sendRedirect("index.jsp");
    }

}

的index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
   <%@page import="org.json.JSONObject" %> 


<%

JSONObject obj = new JSONObject();

obj.put("description", "Pick up Milk");
obj.put("status", "incomplete");
obj.put("id", 1);

response.setContentType("application/json");

out.print(obj);
%>

2 个答案:

答案 0 :(得分:4)

更改

response.sendRedirect("index.jsp");

response.sendRedirect("/MyProj/index.jsp");

没有它,例如,如果你在/todo1上点击GET,它将调用Servlet,它将重定向到/todo1/index.jsp,它将再次映射到Servlet,因为这是你映射它的方式,或者你可能只想将请求转发给jsp

答案 1 :(得分:0)

您可以使用3种方式重定向到另一个页面。

1)发送重定向

response.sendRedirect("path/index.jsp");

2)设置标题

 String site = new String("http://www.google.com");
 response.setStatus(response.SC_MOVED_TEMPORARILY);
 response.setHeader("Location", site);    

3)请求调度程序

    RequestDispatcher rd=request.getRequestDispatcher("index.jsp");      
    rd.forward(request, response);//method may be include or forward  

我也看过你的servlet,你还没有覆盖servlet中的 doPost 方法。 试图改变覆盖 doGet doPost 方法的习惯。 并且只在 doPost 方法中编写逻辑。

<强> MyServlet.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class MyServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

/**
 * Default constructor. 
 */
public MyServlet() {
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response)
}
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.sendRedirect("index.jsp");
}

}