Java 如何编写 hello world servlet 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18821227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:21:54  来源:igfitidea点击:

how to write hello world servlet Example

javaservlets

提问by user2782773

javaclass

package com.example;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class
public class Helloworld extends HttpServlet {
    private String message;

    public void init() throws ServletException {
        // Do required initialization
        message = "Hello World";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // Set response content type
        response.setContentType("text/html");
        // Actual logic goes here.
        PrintWriter out = response.getWriter();
        out.println("<h1>" + message + "</h1>");
    }

    public void destroy() {
        // do nothing.
    }
}

web.xml

网页.xml

<servlet>
        <servlet-name>HelloForm</servlet-name>
        <servlet-class>HelloForm</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloForm</servlet-name>
        <url-pattern>/HelloForm</url-pattern>
    </servlet-mapping>

Give is code But i run the Project There is no Output comes 404 Error is comes in web Page . we need create Jsp Page also for servlet? I am really new in Servlet Please help how to write hello world is Servlet .

给出代码但我运行项目没有输出出现 404 错误出现在网页中。我们还需要为 servlet 创建 Jsp 页面吗?我真的是 Servlet 的新手 请帮助如何编写 hello world is Servlet 。

回答by Pradeep Simha

You have created servlet class like this:

您已经创建了这样的 servlet 类:

public class Helloworld extends HttpServlet

But in web.xml you have mapping like this:

但是在 web.xml 你有这样的映射:

<servlet-class>HelloForm</servlet-class>

You need to have same name, so you're getting 404 error. Change either your servlet name to HelloFormor change <servlet-class>to HelloWorldin web.xml

您需要具有相同的名称,因此您会收到 404 错误。将您的 servlet 名称更改为HelloForm或更改<servlet-class>HelloWorldweb.xml

回答by Sandip Wankhede

you haven't specified the package of the servlet class write like this com.example.Helloworld

你没有指定servlet类的包像这样写com.example.Helloworld

回答by Yubaraj

Use following :

使用以下:

<servlet>
    <servlet-name>HelloForm</servlet-name>
    <servlet-class>com.example.Helloworld</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>HelloForm</servlet-name>
    <url-pattern>/HelloForm</url-pattern>
</servlet-mapping>

and type your url: like localhost:8080/projectName/HelloFormIt may work. And I think you are beginner so go This link. Here is complete tutorial...aboutt this

并输入您的网址:就像localhost:8080/projectName/HelloForm它可能会起作用。我认为你是初学者所以去这个链接。这是完整的教程......关于这个

回答by Himanshu Bhardwaj

Your class resides in com.example

您的课程位于com.example

So servlet-class should,

所以servlet类应该,

<servlet-class>com.example.Helloworld</servlet-class>

回答by SpringLearner

Following way will work.

以下方式将起作用。

Create a folder(your poject name,example project) in webapps Inside proect folder create another folder,name it as WEB-INF. Inside WEB-INF past the web.inf file. Create another folder classes inside project folder and keep the .class files. now modify your web.xml as Himanshu Bhardwaj has suggested. restart the server.Then run

在 webapps 中创建一个文件夹(您的项目名称,示例项目) 在 proect 文件夹内创建另一个文件夹,将其命名为 WEB-INF。在 WEB-INF 中通过 web.inf 文件。在项目文件夹中创建另一个文件夹类并保留 .class 文件。现在按照 Himanshu Bhardwaj 的建议修改您的 web.xml。重新启动服务器。然后运行

回答by David Pham

Now you can switch to servlet 3.0. It's really simple.

现在您可以切换到 servlet 3.0。这真的很简单。

@WebServlet("/example")
public class AnnotationServlet extends HttpServlet{

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter prinOut = response.getWriter();
        prinOut.write("Hello, This is the first servlet 3 annotation example");
    }

}

From Servlet 3.0 Annotation Example in Java

来自Java 中的 Servlet 3.0 注释示例

And here is full Java Servlet Tutorial

这是完整的Java Servlet 教程