Home > Web Front-end > CSS Tutorial > Content-Type disguise - disguise jsp as css

Content-Type disguise - disguise jsp as css

高洛峰
Release: 2017-02-23 10:40:25
Original
2090 people have browsed it

1. Early theoretical preparation

1) Purpose:
Dynamically generate css statements in jsp, and then output them to the browser for parsing and rendering.
2) The basis for the browser to parse the file:
After the page is loaded, the browser will initiate various requests to download various resources.
For example, download the css file and then parse the document according to the css parsing rules. If the Content-Type of the downloaded file does not match, the browser will automatically block it.

Content-Type伪装 - 将jsp伪装成css

2. Prerequisites for turning jsp into css

After knowing the browser’s parsing rules, all jsp needs to do is to disguise its own Content-Type into "text/css".

3. Directly upload the code:

index.html

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<title>jsp文件输出为css文件</title>
<link type="text/css" rel="stylesheet" href="./css.jsp" />
</head>
<body>
    <p class="demo">wall say: hello!</p>
</body>
</html>
Copy after login

css.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="false"%>
<%    // 伪装响应的http头部
    response.setHeader("Content-Type", "text/css");    
    // 输出css样式
    out.clear();
    out.print("@charset \"utf-8\";\n");
    out.print("p{color:red;}\n");
%>
Copy after login

4. Result

Content-Type伪装 - 将jsp伪装成css
The jsp was successfully disguised as a css file, and the browser parsed the style successfully!

5. Extension

According to this disguise rule, jsp can be disguised as any file format, as long as the browser can parse it.

For example, if it is disguised as js, set the Content-Type to "application/x-javascript"

For more Content-Type disguises - Disguise jsp as css, please pay attention to the PHP Chinese website for related articles!

Related labels:
css
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template