有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在Jetty中运行servlet时获得HTTP 500

我有一个servlet,直到几天前还运行得很好。但我唯一改变的是我为maven使用的nexus回购协议。我正在通过mvn jetty:run运行servlet

但当我试图访问该网站而不是查看主页时,我看到:

HTTP ERROR 500

Problem accessing /. Reason:

    jregex/Pattern

我可以访问其他url,比如/favicon。伊科。但是我找不到关于这个jregex/模式错误的任何信息,而且代码中似乎根本没有使用jregex库

我在日志中也没有看到任何问题。看起来对主页的请求并没有发送到我的servlet,但对其他页面的请求却在发送

这在Arch Linux和Mac OS X 10.7上都是如此

这几乎肯定是一个依赖性问题,因为用旧的~/.m2文件夹(带有旧nexus服务器的依赖性)替换我的~/.m2文件夹后,它就可以工作了

有时我也会:

HTTP ERROR: 503

Problem accessing /. Reason:

    SERVICE_UNAVAILABLE

共 (3) 个答案

  1. # 1 楼答案

    您正在运行的mvn命令是什么?您是否尝试过手动下载工件并在本地工件上运行mvn

    我会首先使用mvn下载工件。这将验证您的所有设置/权限设置是否正常且适当。为此,可以使用Maven Dependency Plugin (v2.4)将依赖项下载到本地文件。有关更多详细信息,请参见this post

    一旦你能确保你能在本地下载工件,试着在本地工件上运行jetty:run。如果这有效,那么你就知道你的回购协议有问题

    如果仍不起作用,则可能是镜像设置或repo配置有问题。例如,如果mvn需要一个本地没有的插件或依赖项,它将寻求第三方回购。你的设置。xml文件可能会将所有这些镜像到您的本地nexus服务器,该服务器可能未配置为从MvnCentral下载

    确保你没有任何依赖/插件下载问题。您可以轻松地从设置中指向mvncentral。完全绕过nexus服务器

  2. # 2 楼答案

    杰森,这是我的工作,这是我经常使用的,pom。xml(相关部分):

    <dependencies>
            <!-- Jetty dependencies -->
            <dependency>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-embedded</artifactId>
                <version>6.1.26</version>
            </dependency>
        </dependencies>
    
        <build>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>7.0.2.v20100331</version>
                <configuration>
                    <webAppConfig>
                        <contextPath>/jetty-example</contextPath>
                        <descriptor>${basedir}/src/main/webapp/WEB-INF/web.xml</descriptor>
                    </webAppConfig>
                    <scanIntervalSeconds>5</scanIntervalSeconds>
                    <stopPort>9966</stopPort>
                    <stopKey>foo</stopKey>
                    <connectors>
                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                            <port>9080</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>
                </configuration>
            </plugin>
        </plugins>
        </build>
    

    这里是网络。位于上述webappconfig中指定位置的xml作为描述符:

    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
        version="2.4">
    
        <display-name>HelloWorld Application</display-name>
        <description>
           lalala
        </description>
    
        <servlet>
            <servlet-name>HelloServlet</servlet-name>
            <servlet-class>com.mypackage.jetty.Hello</servlet-class>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>HelloServlet</servlet-name>
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    以及servlet本身:

    public final class Hello extends HttpServlet {
    
        private static final long serialVersionUID = 903359962771189189L;
    
        public void doGet(HttpServletRequest request,
                          HttpServletResponse response)
          throws IOException, ServletException {
    
            response.setContentType("text/html");
            PrintWriter writer = response.getWriter();        
            writer.println("<html>");
            writer.println("<head>");
            writer.println("<title>Sample Application Servlet Page</title>");
            writer.println("</head>");
            writer.println("<body bgcolor=white>");
    
            writer.println("<table border=\"0\" cellpadding=\"10\">");
            writer.println("<tr>");
            writer.println("<td>");
            writer.println("</td>");
            writer.println("<td>");
            writer.println("<h1>W00w I totally work</h1>");
            writer.println("</td>");
            writer.println("</tr>");
            writer.println("</table>");
    
            writer.println("</body>");
            writer.println("</html>");
        }
    } 
    

    您可以通过运行mvn jetty:run来运行服务器,并在http://localhost:9080/jetty-example/hello检查它

    此外,当您在芬兰构建项目时,您可以将执行添加到插件部分并启动jetty。无需每次手动mvn jetty:run

    <executions>
         <execution> <id>start-jetty</id> <phase>pre-integration-test</phase> <goals> <goal>run</goal> </goals> <configuration> <daemon>true</daemon> </configuration> </execution> <execution> <id>stop-jetty</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> </goals> </execution> 
    </executions>
    

    您还可以添加jetty配置文件,我将其用于数据库(用于不同的环境)。您可以在jetty插件的webAppConfig中添加文件位置,如下所示:

    <webAppConfig>
          <contextPath>/my-tool</contextPath>
          <descriptor>${basedir}/src/main/webapp/WEB-INF/jetty/web.xml                          </descriptor>
          <jettyEnvXml>${basedir}/src/main/webapp/WEB-INF/jetty/jetty-env.xml                           </jettyEnvXml>
    </webAppConfig>
    

    以及码头环境的样本含量。xml:

    <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd"[]>
    <Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
          <!-- PRIMARY DATABASE     -->
          <New id="devDS" class="org.eclipse.jetty.plus.jndi.Resource">
                <Arg>primaryDS</Arg>
                <Arg>
                      <!-- i.e. Postgress   -->
                      <New class="org.postgresql.ds.PGSimpleDataSource">
                            <Set name="User">myuser</Set>
                            <Set name="Password">password</Set>
                            <Set name="DatabaseName">database</Set>
                            <Set name="ServerName">database.stackoverflow.com</Set>
                            <Set name="PortNumber">5432</Set>
                      </New>
                </Arg>
          </New>
          <!-- BACKUP DATABASE      
          <New id="devDS" class="org.eclipse.jetty.plus.jndi.Resource">         
          <Arg>backupDS</Arg>       
          <Arg>             
                .....       
          </Arg>    
            -->
    </Configure>
    

    你应该擅长这个

  3. # 3 楼答案

    FWIW,你能用BeyondCompare(Scooter Software)这样的工具进行文件比较吗