使用servlet重定向请求并且“setHeader”方法不起作用

时间:2011-03-05 15:34:56

标签: java tomcat servlets header

我是servlet开发的新手,我正在阅读一本电子书,发现我可以使用

重定向到另一个网页
setHeader("Location", "http://www.google.com")

但这不起作用,因为我把这段代码写成:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ModHelloWorld extends HttpServlet{
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
//              response.addHeader("Location", "http://www.google.com");
                response.setHeader("Location", "http://www.google.com");
                response.setContentType("text/html");
                PrintWriter pw = response.getWriter();
                pw.println("<html><head><title>Modified Hello World</title></head><body>");
                pw.println("<h1>");
                //getInitParameter function reads the contents ot init-param elements.
                pw.println(getInitParameter("message"));
                pw.println("</h1>");
                pw.println("</body></html>");
                pw.close();
        }
}

我已经使用我的程序检查了标题,以获取网页的标题,如下所示:

import java.net.*;
import java.io.*;
class getHeaders{
    public static void main(String args[]){
        URL url = null;
        URLConnection urc = null;
        try {
            url = new URL(args[0]);
            urc = url.openConnection();
            for(int i=0 ; ; i++) {
                String name = urc.getHeaderFieldKey(i);
                String value = urc.getHeaderField(i);
                if(name == null && value == null)//both null so end of header
                    break;
                else if(name == null){//first line of header{
                    System.out.println("Server HTTP version, Response code: ");
                    System.out.println(value);
                    System.out.println("ENd of first header field");
                } else {
                    System.out.println("name of header is: " + name + " and its value is : " + value);
                }
            }
        } catch(MalformedURLException e){
            System.out.println("Malformed URL " + e.getMessage());
        } catch(IOException e){
            e.printStackTrace();
        }
    }
}

我得到的输出为:

Server HTTP version, Response code: 
HTTP/1.1 200 OK
ENd of first header field
name of header is: Server and its value is : Apache-Coyote/1.1
name of header is: Location and its value is : http://www.google.com
name of header is: Content-Type and its value is : text/html
name of header is: Content-Length and its value is : 101
name of header is: Date and its value is : Sat, 05 Mar 2011 15:27:29 GMT

但我没有从浏览器重定向到谷歌页面。

提前致谢:)

4 个答案:

答案 0 :(得分:69)

哦不不!这不是你重定向的方式。它更简单:

public class ModHelloWorld extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
        response.sendRedirect("http://www.google.com");
    }
}

此外,在servlet中编写HTML代码是一种不好的做法。您应该考虑将所有标记放入JSP并使用以下方法调用JSP:

response.sendRedirect("/path/to/mynewpage.jsp");

答案 1 :(得分:21)

如您所见,回复仍为HTTP/1.1 200 OK。要指示重定向,您需要发回302状态代码:

response.setStatus(HttpServletResponse.SC_FOUND); // SC_FOUND = 302

答案 2 :(得分:0)

或者,您可以尝试以下操作,

resp.setStatus(301);
resp.setHeader("Location", "index.jsp");
resp.setHeader("Connection", "close");

答案 3 :(得分:-2)

如果要在指定的时间点之后重定向到任何url源,则执行此操作的另一种方法

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.*;

public class MyServlet extends HttpServlet


{

public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException

{

response.setContentType("text/html");

PrintWriter pw=response.getWriter();

pw.println("<b><centre>Redirecting to Google<br>");


response.setHeader("refresh,"5;https://www.google.com/"); // redirects to url  after 5 seconds


pw.close();
}

}