未解决HTML字符集问题

跳转到jsp 需要:

import javax.servlet.*;

        HttpHandler myHandler = new HttpHandler() {
            public void handle(HttpExchange t) throws IOException {
                // Set response headers
                t.getResponseHeaders().add("Content-Type", "application/json");
                t.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
                t.getResponseHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
                t.getResponseHeaders().add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

                // Set response body
                String response = "{\"message\": \"Hello, World!\"}";
                t.sendResponseHeaders(200, response.length());
                OutputStream os = t.getResponseBody();
                os.write(response.getBytes());
                os.close();
            }
        };

不显示JSON,显示HTML

changed the Content-Type header to text/html to indicate that the response body is HTML.

 We have also replaced the JSON string with an HTML string that contains a simple h1 heading 

t.getResponseHeaders().add("Content-Type", "text/html; charset=UTF-8");
String response = "<html><body><h1>This is a DELETE request!</h1></body></html>";

若显示中文,需要设置字符集

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>My HTML Page</title>
  </head>
  <body>
    <h1>Hello, World!</h1>
  </body>
</html>

试了没用,

string内容单独出来成HTML文件,浏览器能够正常,

 编辑器默认也是utf8,换成gbk,gb开头的依旧不行,

没招了。

import java.io.IOException;
import java.io.OutputStream;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.net.InetSocketAddress;

public class Test_Restful {
    public static void main(String[] args) throws IOException {

        // Define HttpHandler for DELETE method
        HttpHandler deleteHandler = new HttpHandler() {
            public void handle(HttpExchange t) throws IOException {
                // Set response headers
                t.getResponseHeaders().add("Content-Type", "text/html; charset=UTF-8");
                t.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
                t.getResponseHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
                t.getResponseHeaders().add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

                // Set response body
                String str = "<p>Click <a href=\"https://www.example.com\">here</a> to go to example.com</p>\n";
                String mid = "\">here</a> ";
                String left = "<p>Click <a href=\"";
                String right = "</p>\n";  
                String name1 = "index 2021";//写中文需要设置<meta charset="UTF-8">
                String url1 = "https://1.snttw.com/2021"; 

                String s1 = left + url1 + mid + name1 + right;

                String body = str + s1;
                String character = "<html><head><meta charset=\"UTF-8\"><title>boss直聘</title></head><body><h1>";//中文不行,去掉‘直聘’就行了
                String response = character + body + "</h1></body></html>";
                t.sendResponseHeaders(200, response.length());
                OutputStream os = t.getResponseBody();
                os.write(response.getBytes());
                os.close();
            }
        };

        
        // Create HttpServer and bind it to a port
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);

        server.createContext("/delete", deleteHandler);
        server.setExecutor(null); // creates a default executor
        server.start();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值