通过Servlet动态修改JNLP并发送给客户端

问题描述

我编写了一个 servlet 来从服务器读取模板 JNLP 文件,并根据 http 请求中给出的参数对其进行修改。这部分工作正常。当我将 JNLP 文本发送回客户端时,它会尝试启动它,但出于某种原因仍会尝试在 http://localhost/launch.jnlp 上查找真正的 jnlp 文件,而不是使用我在流中发送的文件.任何人都知道我在响应中做错了什么,以便它识别出我发送的内容是它应该使用的 JNLP?我使用 Tomcat 10 服务器托管。

编辑:我能够通过将字符串写入文件并在 servlet 末尾使用此代码发送文件来使其工作。虽然我不想实际将其写入文件,但更愿意直接从字符串发送。

    File blah = new File(getServletContext().getRealPath("/") + "launch.jnlp");

    blah.delete();
    blah.createNewFile();
    FileWriter myWriter = new FileWriter(getServletContext().getRealPath("/") + "launch.jnlp");
    myWriter.write(jnlp);
    myWriter.close();
    request.getRequestdispatcher("/launch.jnlp").forward(request,response);

这是我得到的错误

enter image description here

我的 Servlet

package jnlpservlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.servletexception;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class JnlpServlet
 */
public class JnlpServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    @Override
    public void init(final ServletConfig config) throws servletexception
    {
        super.init(config);
        getServletContext().log("init() called");
    }

    private void processRequest(
            HttpServletRequest request,HttpServletResponse response)
            throws servletexception,IOException
    {
        String machineId;
        String jnlp;
        String codebase;

        response.setContentType("application/x-java-jnlp-file");

        if (request.getParameter("machineid") != null)
        {
            machineId = request.getParameter("machineid");
        }
        else
        {
            machineId = "";
        }

        if (machineId.equals(""))
        {
            codebase = "/files/HMIApplication/LocalMachine/HMIApp";
            jnlp = readFile(System.getenv("FBHOME") + "/HMIApplication/LocalMachine/HMIApp/launch.jnlp",machineId,codebase);
        }
        else
        {
            codebase = "/files/" + machineId;
            jnlp = readFile(System.getenv("FBHOME") + "/" + machineId + "/launch.jnlp",codebase);
        }
        System.out.println(jnlp);
        PrintWriter out = response.getWriter();
        out.print(jnlp);
        out.close();

    }

    @Override
    public void destroy()
    {
        getServletContext().log("destroy() called");
    }

    @Override
    protected void doGet(
            HttpServletRequest request,IOException
    {
        processRequest(request,response);
    }

    @Override
    protected void doPost(
            HttpServletRequest request,response);
    }

    private static String readFile(String filePath,String machineId,String codebase)
    {
        String content = "";

        try
        {
            content = new String(Files.readAllBytes(Paths.get(filePath))).replace("${machineid}",machineId).replace("${codebase}",codebase);
        }
        catch (IOException e)
        {
            e.printstacktrace();
        }

        return content;
    }

}

我的 web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">

    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <description>
        </description>
        <display-name>JnlpServlet</display-name>
        <servlet-name>JnlpServlet</servlet-name>
        <servlet-class>jnlpservlet.JnlpServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>JnlpServlet</servlet-name>
        <url-pattern>/launchRemote</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

我的用于启动 JNLP 的 HTML 链接

<a machineid=\"+listofFolders[i]+\" href=\"javascript:deployJava.launchWebStartApplication('launchRemote?machineid="+listofFolders[i]+"');\"><button class=\"raise\">Launch Machine " + listofFolders[i] + "</button></a>

我的 JNLP 模板

<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" codebase="" href="launch.jnlp">
    <@R_234_4045@ion>
        <title>WebStartLauncher</title>
        <vendor>XYZ</vendor>
        <homepage href="" />
        <description>Application to launch my HMI on remote clients</description>
    </@R_234_4045@ion>
    <security>
        <all-permissions/>
    </security>
    <resources>
        <j2se version="1.8+" />
        <jar href="${codebase}/HMIApp.jar" main="true" download="eager"/>
    </resources>
    <application-desc main-class="com.mycompany.LaunchHMIApplication" name="HMIApplication">
    <argument>${machineid}</argument>
    </application-desc>
</jnlp>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)