使用 Glassfish 应用服务器设置上下文根

Setting Context-Root with Glassfish Application Server

我有一个带有 Glassfish 应用程序服务器的 EJB WEB 应用程序。 现在我想要这样的上下文根“/”。 我当前的 URL 是“http://localhost:8080/Make” 但我想要这个:"http://localhost:8080" without the "Make" as my Application Name currently is. But hen i deploy it and tip "http://localhost:8080" 我从 glassfish 得到了 "Server is running" 页面 所以我想弄清楚我能做什么。我在我的 WEB-INF 文件夹

中创建了一个 glassfish-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish   Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-  web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
<context-root>/</context-root>
</glassfish-web-app>

没有任何反应。比我在同一文件夹中创建一个 sun-web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC 
 "-//Sun Microsystems, Inc.//DTD GlassFish Application Server 3.0 Servlet 3.0//EN"   
 "http://www.sun.com/software/appserver/dtds/sun-web-app_3_0-0.dtd">
<sun-web-app error-url="">
<context-root>/path/to/our/App</context-root>
</sun-web-app>

我还尝试了一条 glassfish-application.xml 和 application.xml。 那么我必须怎么做才能达到这个效果呢?

PS:这是我的 web.xml,我部署为 EAR 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 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>Make</display-name>
<mime-mapping>
<extension>xhtml</extension>
<mime-type>application/xml</mime-type>
</mime-mapping>
<welcome-file-list>
 <welcome-file>anmeldung.xhtml</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
 <error-page>
 <exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/anmeldung.xhtml</location>
</error-page>
<context-param>
<param-name>org.apache.myfaces.SERIALIZE_STATE_IN_SESSION</param-name>
<param-value>false</param-value>
 </context-param>
 <context-param>
<param-name>primefaces.THEME</param-name>
<param-value>bootstrap</param-value>
 </context-param>
 <context-param>
<param-name>javax.faces.WEBAPP_RESOURCES_DIRECTORY</param-name>
<param-value>/WEB-INF/resources</param-value>
 </context-param>
 <context-param>
 <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
 </servlet-mapping>
 </web-app>

编辑:WEB-INF 文件夹中的 application.xml

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" version="6">
<application-name>Make</application-name>
<display-name>Make</display-name>
<module>
<web>
  <web-uri>Make.war</web-uri>
  <context-root>/</context-root>
</web>
 </module>
<module>
  <ejb>makeITown.jar</ejb>
</module>
<library-directory>lib</library-directory>
</application>

你发的glassfish-web.xml好像没问题

如果您将应用程序部署为 EAR,您可能需要 application.xml 像这样:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" version="6">
  <application-name>something</application-name>
  <display-name>something</display-name>
  <module>
    <web>
      <web-uri>your_webapplication.war</web-uri>
      <context-root>/</context-root>
    </web>
  </module>
  <module>
    <ejb>your_ejb_services.jar</ejb>
  </module>
  <library-directory>lib</library-directory>
</application>

这只是一个模板示例。您还可以有多个 web 模块和 ejb 模块。

如果您通过 Glassfish Admin 进行部署UI,您可以在部署时设置上下文根:

如果您使用 asadmin 进行部署,您可以像这样设置上下文根:

asadmin deploy --name something --contextroot / /path/of/your/war.war

将上下文根设置为 / 不足以仅通过键入 http://yourdomain.org 访问您的 Web 应用程序,因为 Glassfish 将显示 "Your server is running" 在 [=19] 中找到的消息=]`.

启动您的应用程序。将 Configuration | server-config | Virtual Servers | server(在 Web 控制台中)中的 Default Web Module 下拉菜单设置为您的网络应用程序的名称。

我希望我没有迟到我一直在为同样的问题而苦苦挣扎。现在有一段时间,无论我尝试什么,服务器都会吐出默认的 your server is running page。 需要做什么才能访问 example.com 而不必输入 example.com/example 其中示例是 contextroot 在您的 glassfish-web.xml 中定义的是 ip 表重新路由并且可以完成为如下。

假设您在 ubuntu 服务器 20.04LTS 上以 root

执行以下命令

root@example.com:~$

root@example.com:~$ iptables -A INPUT -p tcp --dport 80 -j ACCEPT
root@example.com:~$ iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080
root@example.com:~$ iptables -A INPUT -p tcp --dport 443 -j ACCEPT
root@example.com:~$ iptables -t nat -A PREROUTING -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 8181

您必须将其永久化:

root@example.com:~$ iptables-save -c > /etc/iptables.rules
root@example.com:~$ iptables-restore < /etc/iptables.rules

并在启动时调用,

root@example.com:~$ vi /etc/network/if-pre-up.d/iptablesload

将以下命令复制并粘贴到 vi 打开的 iptablesload 文件中 以上

#!/bin/sh
iptables-restore < /etc/iptables.rules
exit 0

不要忘记使用

使 iptablesload 可执行
root@example.com:~$ chmod +x /etc/network/if-pre-up.d/iptablesload!