How to write hello world servlet Example

Javascript

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

 <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 a code. But I am starting a project. No exit. 404 Error appears on the web page. do we need to create a jsp page also for servlet? I am really new to servlet. Help how to write hello world - this is Servlet.

+4
source share
6 answers

You created the servlet class as follows:

 public class Helloworld extends HttpServlet 

But in web.xml you have a mapping like this:

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

You should have one name, so you get a 404 error. Change either the name of the servlet to HelloForm , or change the <servlet-class> to HelloWorld in web.xml

+9
source

Your class is in com.example

So the servlet class should,

 <servlet-class>com.example.Helloworld</servlet-class> 
+4
source

Use the 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 enter your url: like localhost:8080/projectName/HelloForm It can work. And I think you're a newbie, so go this link . Here is the complete tutorial ... aboutt this

+2
source

Now you can switch to servlet 3.0. It is very 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 Example Servlet 3.0 Annotation in Java

And here is the full Java Servlet Tutorial

+1
source

you did not specify a servlet class package write so com.example.Helloworld

0
source

The subsequent path will work.

Create a folder (the name of your project, an example project) in webapps Inside the proect folder, create another folder, name it WEB-INF. Inside WEB-INF is the web.inf file. Create other folder classes inside the project folder and save the .class files. now change your web.xml as suggested by Himanshu Bhardwaj. restart the server. Then run

0
source

Source: https://habr.com/ru/post/1502342/


All Articles