Home Java javaTutorial Graphical tutorial on developing servlets with myeclipse in Java

Graphical tutorial on developing servlets with myeclipse in Java

Jul 26, 2017 pm 03:04 PM
java myeclipse servlet

MyEclipse is a powerful enterprise-level integrated development environment developed on the basis of eclipse with its own plug-ins. It is mainly used for the development of Java, Java EE and mobile applications. The following article mainly introduces you to the relevant information about developing servlets with myeclipse. Friends in need can refer to it.

In web.xml, you can configure multiple external access paths for the same Servlet. If the information server is configured in web.xml, it will be automatically loaded and deployed. If the program code is modified in the Servlet, , it will need to be redeployed every time.

First, after using MyEclipse to create a Servlet, the created Servlet will be mapped to the web.xml file, as shown in the following figure:

 

After this mapping, the configuration information of this Servlet is automatically generated in the web.xml file:

##  

Of course, we can continue this Servlet in the web.xml file Add an external access path so that this Servlet can be accessed from different paths.

Now we can add a new external access path to this Servlet when the server is turned on:  

The red box part is newly added. At this time, the server will automatically prompt that it has been redeployed in the Console window. Yes, as long as you modify it in web.xml, you do not need to close it. The server can be re-updated and loaded by the server for deployment:

 

Then let’s try a new access address:

 

Okay access! !

Detail 2: When configuring the external access path for the Servlet in web. # One format is

The entire content can only have "*.extension"

, such as *.html, and if it is written like this< ;url-pattern>abc/*.html is wrong. Especially /*.html is also wrong. There cannot be "/", so be sure to pay attention. Example:

 In the browser, you can enter at will in the wildcard position:

 

This creates a pseudo-static Phenomenon, we think we are accessing a static page, but it is actually a dynamic web resource.

Another format is to start with a forward slash "/", add a custom path, and end with "/*", such as /abc/*. At this time, even the extension is arbitrary. Example:

 

You can enter it in the wildcard position in the address bar of the browser, and the extension is not required:

 

 

Detail 3: If the mapping paths of multiple Servlet resources exported by Detail 2 use wildcards, conflicts may occur. So which Servlet resource will the server respond with at this time? Let's take a look at the following question and we will understand:

 

Detail 4: During the entire life cycle of the Servlet, the Servlet's init() method will only be called once. For each access request of the Servlet, the Servlet will call the service() method once. And for each request access, the Servlet will create a new HttpServletRequest request object and a new HttpServletResponse response object. Of course, after each request access is completed, these request objects and response objects will be destroyed immediately, waiting for the next request and then re-created, which can reduce the pressure on the server (in non-concurrent situations).

Detail 5: After the Servlet mentioned before is deployed on the server, the server will only create an instance object of the Servlet when the Servlet is accessed for the first time. If in web.xml in a certain tag is configured under the ; tag, the Servlet will create an instance object after the server starts and execute the Servlet's initialization init() method. The content of is a positive integer value. The smaller the value, it means that the Servlet object will be created first and the initialization method will be executed after the server is started.

Detail 6: If the mapping path of a certain Servlet is only a forward slash "/", then this Servlet is called the default Servlet of the current web application. For URLs that cannot find a matching tag in the web. Access request.

For example, I now create two Servlets, one of which has a customized external mapping path, and the other is set as the default Servlet:

 

If you want To access SecondServlet, you must type in the browser address bar according to its corresponding path:

 

. For the default Servlet, just enter casually after the web application name:

 

(If you do not lose, the existing homepage index.jsp will be displayed. Why, please see details six below)

  Detail 6 (Important) : If we do not configure the default Servlet in the Servlet we developed, but directly access a static web resource under our web project (if any), then we will be able to browse See this web resource in the server, Remember: requesting any web resource uses a certain Servlet to respond to the returned data, so since we have not configured a default Servlet, why Can I access this resource? In other words, which Servlet responds to us with the resources we need? The answer is the default Servlet of Tomcat server! ! ! The default Servlet of Tomcat server sets the mapping path in the web.xml file in Tomcat's [conf] directory:

 

As can be seen from Tomcat's web.xml file , its default Servlet creates an instance object when the server starts, and it is the earliest created, and the external path has been set to the default format. And this default Servlet will manage each custom-created Servlet project. If we do not create a default Servlet in our own web project, then when we want to access a resource in our own web project, Tomcat's default Servlet will help us convert the resource. The resource is encapsulated into a response object and sent back to the client.

For example, I create a 1.html page under my web project:

 

And I go to the browser to access this page:

  

For this URL, the Tomcat server first matches whether my Servlet has a corresponding mapping path. If not, and if I do not set a default Servlet in my web project, then Tomcat's The default Servlet searches for HTTP requested resources under my web application, finds and encapsulates them into response objects and returns them to the client. If Tomcat's default Servlet cannot find the required resource, it will still respond to the client, but the client will see some prompts that the resource cannot be found, such as a 404 prompt. In addition, according to this process, when we develop our own Servlet, it is recommended not to map one of our own Servlets to the default Servlet.

Next, let’s discuss a homepage issue. In addition to accessing a specific resource in the web application (finally typing a file name on the browser address), an HTTP request is also sent after typing the web application name. At this time, what we see is also a page, usually called is the "home page", and this home page is also responded to by the default Servlet.

For example, when I use MyEclipse to create a web application, it will create an "index.jsp" file for me by default:

 

And I am in the web application No Servlet is created in the Servlet, or even if the Servlet is created, it is mapped to a specific external URL. Then I can still see this index.jsp when I enter the host address + web application name in the browser:

 

As mentioned before, this page must be sent back to the client by the default Servlet of the Tomcat server. So why does the Tomcat server send back this resource instead of other resources? The answer is still in the web.xml file. Do you still remember the homepage settings defined in "Tomcat Detailed Usage Learning (3)"? Yes, that is this method. This method is managed by the default Servlet. Let's review the web settings in Tomcat The tag in the .xml file:

## 

If the browser’s input address only enters the web application name, then Tomcat’s default Servlet transmits this The index.html file under the web application, otherwise the index.htm file is sent, and otherwise the index.jsp is sent. If there are none of the three, it can only return 404.

If I comment out or delete this code, and then want to directly access the web application name, it will be the same as when I deleted the index.jsp file. Tomcat’s default Servlet cannot find the resource and returns a 404 prompt (in The server needs to be restarted in MyEclipse):

 

 

So if we want to make a beautiful homepage for our web application, we only need to Just develop it in .jsp, or create another index.html file for development. This file will be accessed first than index.jsp.

Detail 7: Because our development Servlet must inherit a certain implementation class of the Servlet interface, then when we write the doGet method or doPost method, we are likely to call some methods that will throw exceptions, then We can only catch and handle these exceptions, but cannot throw them. This is because the subclass cannot throw more exceptions than the parent class when overriding the parent class's methods.

Summarize

The above is the detailed content of Graphical tutorial on developing servlets with myeclipse in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles