📜 ⬆️ ⬇️

Creating servlets for dummies. Walkthrough

image

When a novice developer encounters servlets, it is very difficult for him to understand how he works and what this work depends on. That's because all the examples and video tutorials are designed for people who understand the nature of servlets and what follows. So I decided to write a guide for creating the simplest servlets. Perhaps this article will help someone.

So.
')
Suppose you have already downloaded an example somewhere using maven and you managed to plug your Tomcat code (this usually begins with the knowledge of servlets) in any way (WAR-archive or directly from the development environment). You have an application structure in which the web.xml file is present. C it and you need to start creating pages.

First and most important: the machine does not see a direct connection between the piece of the address bar and the page of the same name in your project. localhost : 8080 / WAR_name / test and test.jsp are not the same thing. / test is the url label of the servlet. By which the machine finds the necessary Java file and it already points to test.jsp.

The path from reading the code by the machine to displaying the page in the browser looks like this:

webapp / WEB-INF / web.xml -> servlet
---> ru.user.project / web / ClassName -> request
---> page.jsp

Yes, nothing is clear yet, but we will return to this scheme. If you describe it with simple human words, it will look like this:

From the web.xml file through the servlet, the machine obtains the path to the Java class, which, in turn, directs the machine to the page you are looking for.

It was a lyrical digression, go to the code.

So, we have a project on Tomcat, the main page of which is opened by calling localhost : 8080 / WAR_name (if we deploy a WAR file).

Open web.xml. This file is scanned by Tomcat first. Here we set the beginning of the path. Here is the code of our web.xml:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Test</display-name> <servlet> <servlet-name>testServlet</servlet-name> <servlet-class>ru.user.project.web.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>testServlet</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping> </web-app> 

The servlet links the link from the address bar and the Java class. The Java class, in our case, opens a JSP page. The servlet consists of 2 components:

 <servlet> //    Java- <servlet-name>testServlet</servlet-name> <servlet-class>ru.user.project.web.TestServlet</servlet-class> </servlet> <servlet-mapping> //      ,   <servlet-name>testServlet</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping> 

We register <servlet-class>. A Java class is stored along this path, which will be processed when the address bar is called. Then we finish <servlet-mapping>. This is a piece of the address from the address bar tied to the servlet. When we add to our original / test line, magic begins. But so far we have not added the rest of the code. Writing java file We have it at ru.user.project.web (for this you need to create a web folder, if it does not).

 import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * Created by promoscow on 17.07.17. */ public class TestServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/test.jsp").forward(request, response); } } 

The class must be inherited from HttpServlet and override the doGet () method; In the overridden method, we write the name of the line to which the transition will be made (in our case, this is "/test.jsp".

Thus, when calling the address localhost : 8080 / WAR_name / test, Tomcat finds the required <url-pattern>, finds out that it belongs to testServlet, then sees that the testServlet belongs to the testServlet, executes it, and goes to the Java-file go to test.jsp. It remains to write test.jsp:

 <%-- Created by IntelliJ IDEA. User: promoscow Date: 17.07.17 Time: 23:22 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Test</title> </head> <body> Hey there! It's test servlet page! </body> </html> 

Now, when the user completes / test to the original address, the algorithm described at the beginning of the article will be executed (remember, I promised to return to it?) And the browser will show the contents of the test.jsp file. Also, you can, for example, write a link in the start file (for example, index.html):

 <a href="test">Test page</a> 

And the above described chain of events will occur, which will eventually lead to the test.jsp page.

I hope this article will help novice developers searching for common sense to write the first servlet, and later everything else will be added to this understanding (as is usually the case).

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


All Articles