使用 jsp 模板生成 java 源代码,并将生成的 java 代码二次编译并运行

使用 jsp 模板生成 java 源代码,并将生成的 java 代码二次编译并运行

上篇文章中,我将 tomcat 中编译 jsp 并生成 servlet 的路线给单独走通了,从而实现了不需要启动 tomcat,而直接将 jsp 模板渲染为 html 的功能。

这一次,我突发奇想,如果使用 jsp 生成 java 源码,能否对生成的 java 代码进行二次编译,从而实现俄罗斯套娃呢?

由上篇文章可以得知,渲染 jsp 模板就是先翻译为 servlet 源码,然后编译之并实例化,接着手动调用其 init、service、destroy 方法即可。所以如果我能够想办法,在编译的时候,将原本的 servlet 源码替换为生成的 java 代码,那么就可以实现狸猫换太子了。

使用示例

事实上,也确实如此。这里先示范一下如果使用。
首先 pom.xml 引用依赖(需要自行下载安装):

        <dependency>
            <groupId>com.kfyty</groupId>
            <artifactId>k-jte</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

然后在 resources 文件夹下创建 template 文件夹,接着在其中创建 HelloWorldImpl.jsp 文件,其中的 HelloWorld 接口是为了后面方便调用而存在的。毕竟这个类是根据模板生成的,不写个接口的话就只能用反射了:

package com.kfyty.jsp.template;

import com.kfyty.demo.template.HelloWorld;

public class HelloWorldImpl implements HelloWorld {
    @Override
    public void hello(String name) {
        System.out.println("hello: " + name);
    }
}

编写主函数,尝试调用 HelloWorldImpl.hello(name):

package com.kfyty.demo;

import com.kfyty.demo.template.HelloWorld;
import com.kfyty.kjte.JstlRenderEngine;
import com.kfyty.kjte.JstlTemplateEngine;
import com.kfyty.kjte.config.JstlTemplateEngineConfig;

public class Main {

    public static void main(String[] args) throws Exception {
        JstlTemplateEngineConfig config = new JstlTemplateEngineConfig("/template/HelloWorldImpl.jsp");
        config.setCompiler(true);
        config.setSuffix(".java");
        JstlTemplateEngine engine = new JstlTemplateEngine(config);
        JstlRenderEngine renderEngine = new JstlRenderEngine(engine, engine.load());
        renderEngine.doRenderTemplate();
        HelloWorld helloWorld = (HelloWorld) engine.getCompileClass().get("com.kfyty.jsp.template.HelloWorldImpl").newInstance();
        helloWorld.hello("tom");
    }
}

运行之后,控制台输出如下:

五月 02, 2021 1:50:09 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
13:50:11.574 [main] WARN com.kfyty.kjte.compiler.JteJDTCompiler - compiler problem of C:\Users\fyty\AppData\Local\Temp\HelloWorldImpl_jsp.java: The import javax.servlet.jsp is never used
13:50:11.579 [main] WARN com.kfyty.kjte.compiler.JteJDTCompiler - compiler problem of C:\Users\fyty\AppData\Local\Temp\HelloWorldImpl_jsp.java: The serializable class HelloWorldImpl_jsp does not declare a static final serialVersionUID field of type long
13:50:11.579 [main] WARN com.kfyty.kjte.compiler.JteJDTCompiler - compiler problem of C:\Users\fyty\AppData\Local\Temp\HelloWorldImpl_jsp.java: The value of the local variable session is not used
13:50:11.579 [main] WARN com.kfyty.kjte.compiler.JteJDTCompiler - compiler problem of C:\Users\fyty\AppData\Local\Temp\HelloWorldImpl_jsp.java: The value of the local variable application is not used
13:50:11.579 [main] WARN com.kfyty.kjte.compiler.JteJDTCompiler - compiler problem of C:\Users\fyty\AppData\Local\Temp\HelloWorldImpl_jsp.java: The value of the local variable config is not used
13:50:11.579 [main] WARN com.kfyty.kjte.compiler.JteJDTCompiler - compiler problem of C:\Users\fyty\AppData\Local\Temp\HelloWorldImpl_jsp.java: The value of the local variable page is not used
13:50:11.580 [main] DEBUG com.kfyty.kjte.compiler.JteJDTCompiler - Compiled C:\Users\fyty\AppData\Local\Temp\HelloWorldImpl_jsp.java 1132ms
13:50:11.965 [main] DEBUG com.kfyty.kjte.compiler.JteJDTCompiler - Compiled C:\Users\fyty\AppData\Local\Temp\HelloWorldImpl.java 70ms
hello: tom

Process finished with exit code 0

感兴趣的可以看一下源码:
github 地址:https://github.com/kfyty/k-jte

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值