java—通过servlet动态修改jnlp并将其发送到客户端

tpgth1q7  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(215)

我已经编写了一个servlet来从服务器读取模板jnlp文件,并根据http请求中给出的参数对其进行修改。这部分工作正常。当我将jnlp文本发送回客户机时,它会尝试启动它,但仍会尝试在服务器上查找真正的jnlp文件http://localhost/launch.jnlp 因为某种原因而不是使用我在流中发送的。有人知道我在响应中做错了什么,这样它就知道我发送的是它应该使用的jnlp吗?我用tomcat10服务器托管。
edit:我可以通过将字符串写入一个文件并在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);

这是我得到的错误:

我的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", machineId, 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, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }

    @Override
    protected void doPost(
            HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(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">
    <information>
        <title>WebStartLauncher</title>
        <vendor>XYZ</vendor>
        <homepage href="" />
        <description>Application to launch my HMI on remote clients</description>
    </information>
    <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>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题