how to write hello world servlet Example

前端 未结 6 705
一向
一向 2021-01-13 14:29

javaclass

package com.example;

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

// Extend HttpServlet class
public class Helloworld e         


        
相关标签:
6条回答
  • 2021-01-13 15:11

    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/HelloForm It may work. And I think you are beginner so go This link . Here is complete tutorial...aboutt this

    0 讨论(0)
  • 2021-01-13 15:14

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

    @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

    And here is full Java Servlet Tutorial

    0 讨论(0)
  • 2021-01-13 15:16

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

    0 讨论(0)
  • 2021-01-13 15:19

    Your class resides in com.example

    So servlet-class should,

    <servlet-class>com.example.Helloworld</servlet-class>
    
    0 讨论(0)
  • 2021-01-13 15:26

    You have created servlet class like this:

    public class Helloworld extends HttpServlet
    

    But in web.xml you have mapping like this:

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

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

    0 讨论(0)
  • 2021-01-13 15:32

    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

    0 讨论(0)
提交回复
热议问题