使用 servlet 和“setHeader"重定向请求方法不起作用 [英] Redirecting a request using servlets and the "setHeader" method not working

查看:21
本文介绍了使用 servlet 和“setHeader"重定向请求方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to servlet development, and I was reading an ebook, and found that I can redirect to a different web page using

setHeader("Location", "http://www.google.com")

但这不起作用,因为我已将此代码编写为:

But this is not working, as I have written this code as:

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();
        }
}

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

i have checked the headers using my program to get the headers of the webpage which is as under:

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

但我没有从浏览器重定向到 google 页面.

But I was not redirected to google's page from my browser.

提前致谢:)

推荐答案

如您所见,响应仍然是 HTTP/1.1 200 OK.要指示重定向,您需要发回 302 状态代码:

As you can see, the response is still HTTP/1.1 200 OK. To indicate a redirect, you need to send back a 302 status code:

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

这篇关于使用 servlet 和“setHeader"重定向请求方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆