×
=0) { let js = text.slice(pos1, pos2); + '<\/' + "script" + '>'; arrText.push(js); // next pos1 = pos2; continue; } } } break; } return arrText; } function OpenDialog(parentDiv, urlContent) { parentDiv = document.getElementById('modal-background'); // new !!!!!!! parentDiv.appendChild(document.getElementById('modal-template')); document.getElementById('modal-background').style.display = "block"; document.getElementById('modal-template').style.display = "flex"; // !!!!! document.getElementById('modal-body').innerHTML = ""; post_url(urlContent, "", function(text_from_server) { var element = document.getElementById('modal-body'); element.innerHTML = text_from_server; // add scripts var arrJSText = get_scripts(text_from_server); for (var i=0; i
dir.by
Search
Programming, development, testing
→
Java
→
Java web application (servlet, jsp page, basic functionality and no frameworks)
→
Create a web servlet (the servlet is located on the web server, returns the result on request) | Java
Looked at
1078
times
Create a web servlet (the servlet is located on the web server, returns the result on request) | Java
last updated: 10 April 2025
Here's my servlet
File
MainServlet.java
@WebServlet(
"/hello"
)
public
class
MainServlet
extends
HttpServlet
{
@Override
protected
void
doGet(HttpServletRequest req, HttpServletResponse resp)
throws
ServletException, IOException
{
resp.setContentType(
"text/html"
);
PrintWriter printWriter = resp.getWriter();
printWriter.write(
"Good morning!"
);
printWriter.close();
}
}
Explanation!
On the path
/hello
my servlet
MainServlet
will be executed.
The name
MainServlet
can be invented by any name and it does not affect anything.
Important is the path
@WebServlet("/hello")
And it is important what the servlet returns. My servlet will return the value of
"Good morning!"
as
"text/html"
That is, to make a servlet out of a regular class, it must be inherited from the class
HttpServlet
.
Above the class, specify the annotation
@WebServlet()
, in which we bind the servlet to a specific path
/hello
.
Start the project and open the link:
http://localhost:8080/MyServlet1/hello
We see that our servlet is working on the path
/hello
, i.e. it gives an answer
Good morning!
Download an example:
MyServlet1.zip ...
size: 120 kb
Note!
You must have
Java JDK
installed. If you don't have it, then
need to download and install
Java JDK
...
Note!
You must have
IntelliJ IDEA Ultimate
installed. If you don't have it, then
need to download and install
IntelliJ IDEA Ultimate
...
Note!
You must have
Tomcat Web Server
installed. If you don't have it, then
need to download and install
Tomcat Web Server
...
Step 1. Creating a new project
Select the project:
Maven Archtype
Choose the type:
webapp
The project was created:
jsp
A file is a web page.
Let's try to run the project:
Click on the green triangle
The
Edit configuration
window with the default configuration appeared.
At the bottom, we see an error
Error: Module is not selected
Step 2. To launch this web project well, you need to add a web server (add the Smart Tomcat plugin, this is a lightweight web server)
Click on
Edit Configurations
Click on
+
click on
Smart Tomcat
Note!
If there is no
Smart Tomcat
in the list, then
You need to install
Smart Tomcat plugin
in
Intellij Idea
...
A window appeared with the configuration for
Smart Tomcat
I don't change anything, click on
OK
Step 3. Launching the project
Click on the green triangle
The application starts and sees the command in
Console
:
C:\Users\echig\.jdks\openjdk-22.0.1\bin\java.exe "-Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 9.0"
-Dcatalina.base=C:\Users\echig\.SmartTomcat\MyServlet1\MyServlet1
-Djava.io.tmpdir=C:\Users\echig\.SmartTomcat\MyServlet1\MyServlet1\temp
-Djava.util.logging.config.file=C:\Users\echig\.SmartTomcat\MyServlet1\MyServlet1\conf\logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3\lib\idea_rt.jar=59545:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3\bin"
-Dfile.encoding=UTF-8
-Dsun.stdout.encoding=UTF-8
-Dsun.stderr.encoding=UTF-8
-classpath "C:\Program Files\Apache Software Foundation\Tomcat 9.0\bin\bootstrap.jar;C:\Program Files\Apache Software Foundation\Tomcat 9.0\bin\tomcat-juli.jar"
org.apache.catalina.startup.Bootstrap start
This means that the web server will be terminated
Tomcat
Click on the link:
http://localhost:8080/MyServlet1
We see that our web page has been launched on a web server:
Attention!
If we run a project and an error:
Error running 'Unnamed'
java.io.FileNotFoundException:
C:\Users\echig\.SmartTomcat\MyServlet1\ MyServiet1conf\server.xml (The system cannot find the file specified)
Decision:
You need to recreate the project from the beginning.
Step 4. Add the
javax.servlet-api
library to the pom.xml file
In the
pom.xml
file, connect the libraries.
Xml
File
pom.xml
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.example
</groupId>
<artifactId>
MyServlet1
</artifactId>
<packaging>
war
</packaging>
<version>
1.0-SNAPSHOT
</version>
<name>
MyServlet1 Maven Webapp
</name>
<url>
http://maven.apache.org
</url>
<dependencies>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
3.8.1
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
javax.servlet
</groupId>
<artifactId>
javax.servlet-api
</artifactId>
<version>
4.0.1
</version>
</dependency>
</dependencies>
<build>
<finalName>
MyServlet1
</finalName>
</build>
</project>
Attention!
After adding
<dependency> ... </dependency>
to the
pom.xml
file, this library will not be downloaded from the Internet.
To download this library from the Internet, you need to close the project and click open project
Step 5. Create a Java folder and make a servlet file MainServlet.java
Let's create a folder
Java
Inside the
Java
folder, create a new file
MainServlet.java
:
Java
File
MainServlet.java
@WebServlet(
"/hello"
)
public
class
MainServlet
extends
HttpServlet {
@Override
protected
void
doGet(HttpServletRequest req, HttpServletResponse resp)
throws
ServletException, IOException
{
resp.setContentType(
"text/html"
);
PrintWriter printWriter = resp.getWriter();
printWriter.write(
"Good morning!"
);
printWriter.close();
}
}
Explanation!
On the path
/hello
my servlet
MainServlet
will be executed.
The name
MainServlet
can be invented by any name and it does not affect anything.
Important is the path
@WebServlet("/hello")
And it is important what the servlet returns. My servlet will return the value of
"Good morning!"
as
"text/html"
Step 6. Launch the project and see how the servlet works
Click on the link:
http://localhost:8080/MyServlet1
add at the end
/hello
Just like that
http://localhost:8080/MyServlet1
/hello
We see that our
servlet
is running on a web server:
← Previous topic
Creating a new simple web application (jsp web page) | Java, Maven
Next topic →
Installation Tomcat web server (download and install for Windows)
Your feedback ... Comments ...
Your Name
Your comment
(www links can only be added by a logged-in user)
+ Picture
Объявления
Объявления
Download and install
•
Installation JDK (download and install the Java library for Windows)
•
Download and install IntelliJ IDEA to explore Java, Spring, Jakarta EE | Functionality and differences: Community | Ultimate
Learn the language Java (class, interface, properties, etc.)
•
Create a simple console app in IntelliJ IDEA to learn Java
•
What is interface in Java ?
•
Directly in the code (at runtime) we make an implementation for the interface | Java
Java web application (servlet, jsp page, basic functionality and no frameworks)
•
Creating a new simple web application (jsp web page) | Java, Maven
•
Create a web servlet (the servlet is located on the web server, returns the result on request) | Java
•
Installation Tomcat web server (download and install for Windows)
•
Check, test Tomcat web server (create a new my.hml file)
•
What is Maven
•
What is a dynamic web page jsp (Java Server Page) ?
•
How to install Smart Tomcat plugin in Intellij Idea
•
Error "The SDK is not specified for module ... | Project SDK is not defined ..." in IntelliJ IDEA | Java
Spring in Java (Spring Framework, Spring Data, Spring Boot, ...)
•
What is Spring? Why use Spring in Java?
•
What is Spring Bean? What is Spring Container? IoC (inversion of control) | Creating a simple Spring project and configuration Spring Container in an XML file | Editor IntelliJ Idea and language Java
•
What is Dependency Injection in Spring? Creating a simple Spring project with Dependency Injection in the constructor with the attributes @Component, @Autowired | Editor IntelliJ Idea and language Java
•
Creating a new Spring web application (Controller responds to request) | Java, Spring Web, Spring Boot, Maven
•
Creating a new Spring WebSocket application (Java WebSocket sends a message to the JavaScript WebSocket) | Java, Spring WebSocket, Spring Boot, Maven
•
Create Azure Web App with the type Java (free). That is, create an empty web server
•
Put the Java Spring web application in Azure
•
Connect to the application Azure Web App (Java) via FTP | use File Explorer
•
Error "The SDK is not specified for module ... | Project SDK is not defined ..." in IntelliJ IDEA | Java, Spring Boot
•
How to install Spring WebSocket plugin in Intellij Idea
Jakarta EE / Java EE in Java (Web Applications, Web Services)
•
What is Jakarta EE (Java EE)? | In which editor (program) is it convenient to write Jakarta EE (Java EE) code?
•
Creating a simple web project in IntelliJ Idea Ultimate | Jakarta EE (Java EE)
Struts in Java (extends API Java Servlet using MVC)
•
What is Struts | Java
•
Interviews on Java
Ваши вопросы присылайте по почте:
info@dir.by