简体   繁体   English

无法制作简单的HelloWorld码头网络应用

[英]Can't make a simple HelloWorld jetty webapp

I am trying to deploy a simple Helloworld webapp in my jetty v9.1. 我正在尝试在码头v9.1中部署一个简单的Helloworld Web应用程序。 I am having problems when i am trying to access the servlet, I recive an error message: 当我尝试访问servlet时遇到问题,我收到一条错误消息:

HTTP ERROR 404 HTTP错误404

Problem accessing /HelloServlet/servlet. 访问/ HelloServlet / servlet时出现问题。 Reason: 原因:

Not Found

I know i am doing something wrong here but i can't tell what. 我知道我在这里做错了,但我不知道该怎么办。

here is my file structure from Webbapp in jetty: 这是码头上Webbapp的文件结构:

webapps web应用

+example 例如+

+hello +你好

hello.xml index.html +WEB-INF hello.xml index.html + WEB-INF

web.xml +classes web.xml +类

HelloServlet.class HelloServlet.class

index.html +WEB-INF (reprezent a folder,WEB-INF and a file index.html, in same folder hello) index.html + WEB-INF(代表一个文件夹,WEB-INF和一个文件index.html,在同一文件夹中,您好)

here is my web.xml 这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app
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/web-app_2_5.xsd" 
version="2.5">
<display-name>Example</display-name>

<!-- Declaraa existenta unui servlet. -->
<servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>HelloServlet</servlet-class>

</servlet>

<!-- Map URLs to that servlet. -->
<servlet-mapping>
    <servlet-name>servlet</servlet-name>
    <url-pattern>/servlet</url-pattern>
</servlet-mapping>


HelloServlet.java HelloServlet.java

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HelloServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

   String input=request.getParameter("input");
   PrintWriter out=response.getWriter();

   out.println("<html>");
   out.println("<body>");
   out.println("The parameter input was \" "+input+"\" .");
   out.println("</body");
   out.println("</html");

}


protected void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException {

    String field=request.getParameter("field");
    PrintWriter out=response.getWriter();

    out.println("<html>");
    out.println("<body>");
    out.println("You entered \" "+field+"\" into the text box.");
    out.println("</body>");
    out.println("</html>");

}

}

index.html 的index.html

<html>
<head>
    <title>Example Web Application</title>
</head>

<body>
    <p>This is a static document with a form in it.</p>
    <form method="POST" action="servlet">
        <input name="field" type="text" />
        <input type="submit" value="Submit" />
    </form>
</body>
</html>

hello.xml 的hello.xml

<<?xml version="1.0"  encoding="ISO-8859-1"?>
<<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">



<<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/hello</Set>
<Set name="extractWAR">false</Set>
<Set name="copyWebDir">false</Set>
<Set name="defaultsDescriptor"><Property name="jetty.home" default="."/>/etc/webdefault.xml</Set>
<Call name="setAttribute">
<Arg>org.eclipse.jetty.websocket.jsr356</Arg>
<Arg type="Boolean">true</Arg>
</Call>

<Get name="securityHandler">
<Set name="loginService">
  <New class="org.eclipse.jetty.security.HashLoginService">
    <Set name="name">Test Realm</Set>
    <Set name="config"><SystemProperty name="jetty.base" default="."/>/etc/realm.properties</Set>
  </New>
</Set>
<Set name="authenticator">
  <New class="org.eclipse.jetty.security.authentication.FormAuthenticator">
    <Set name="alwaysSaveUri">true</Set>
  </New>
</Set>
<Set name="checkWelcomeFiles">true</Set>
</Get>

</Configure>

In your web.xml , you specified the url-pattern as /servlet/* , meaning that the prefix you need to use is servlet/ , not HelloServlet/ : web.xml ,您将url-pattern指定为/servlet/* ,这意味着您需要使用的前缀是servlet/ ,而不是HelloServlet/

http://localhost:8080/servlet/servlet

(And while it can be instructional to write a servlet by hand, for real-world projects it's better to go with a system like Spring that already has all of the plumbing taken care of for you.) (虽然可以手工编写一个servlet具有指导意义,但对于实际项目,最好使用像Spring这样的系统,该系统已经为您提供了所有帮助。)

I have managed to find a solution after all and my problem is fixed. 毕竟,我已经设法找到了解决方案,并且我的问题已解决。 I just started the server from Intellij Idea and everything works perfectly. 我刚刚从Intellij Idea启动了服务器,并且一切正常。

Thank you all for your help. 谢谢大家的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM