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

Graphical tutorial on developing servlets with myeclipse in Java

黄舟
Release: 2017-07-26 15:04:57
Original
2252 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template