current position:Home>Servlet create project

Servlet create project

2022-05-15 07:33:06Youcan.

Catalog

1. Create project

2. Introduce dependencies

3. Create directory

4. Write code

 5. packaged applications

6. The deployment process

7. Verification procedure

 8. install Smart Tomcat plug-in unit

9. Configuration plug-ins

10. Access error


1. Create project

send ⽤ IDEA establish ⼀ individual Maven term ⽬.
1. menu ---》 file ---》 New projects ---》 Maven

 

2. Select the location where the project will be stored

groupId: Indicates the name of the organization
artifactId: The term ⽬ name
version: Version number

2. Introduce dependencies

Maven term ⽬ After creation , Meeting ⾃ dynamic ⽣ become ⼀ individual pom.xml ⽂ Pieces of , We need to be in pom.xml Middle quotation ⼊ Servlet API Rely on the jar package .
1. The central warehouse  https://mvnrepository.com/    lookup servlet .

 

Can be in http://tomcat.apache.org/whichversion.html View the corresponding version information in .

 

 

take servlet Dependent information Add to <dependencies> </dependencies> Between .

see servlet Successfully added . 

3. Create directory

stay main It's a bag Right click to create java class .

these ⽬ Recording :
src Indicates where the source code is located ⽬ record
main/java Represents the root of the source code ⽬ record . Follow up creation .java ⽂ Just put it in this ⽬ Recording .
main/resources The term ⽬ Of ⼀ Some resources ⽂ Where the piece is located ⽬ record . 
test/java Represents the root of the test code ⽬ record . 

4. Write code

Right click java , stay java ⽬ Create in record ⼀ Classes HelloServlet
1. Inherit HttpServlet
2. Set up URL mapping , Need to make ⽤ @WebServlet Annotation Association ⼀ individual HTTP The path of
3. rewrite doGet / doPost /...... Method
When all three conditions are met ⾜ after , Tomcat You can find this class , And enter at the right time ⾏ transfer ⽤.
@WebServlet("/hello")   // Set up url mapping    http://ip:port/hello
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // Set the return type and encoding format 
        resp.setContentType("text/html; charset=UTF-8");
        // Returned data 
        resp.getWriter().println("<h1> Hello .servlet.</h1>");
    }
}

 5. packaged applications

 
send ⽤ maven Into the ⾏ pack . open maven window ⼝ (⼀ Like in IDEA On the right you can see Maven window ⼝, If you can't see it , Can pass menu -> View -> Tool Window -> Maven open ) And then unfold Lifecycle , double-click package You can enter ⾏ pack .

 

If you can see SUCCESS Words like this , It means that the package is successful .

If the code / To configure / Environmental problems , May prompt BUILD FAILED, It can be solved according to the error information prompted .

After successful packing , You can see in the target ⽬ Record , ⽣ a ⼀ individual jar package .

 

 

war Bao He jar The difference between packages
jar The bag is ordinary java The result of program packaging . ⾥⾯ Will contain ⼀ some .class ⽂ Pieces of .
war Bag is java web The program , ⾥⾯ In addition to including .class ⽂ In addition to , It also includes HTML, CSS, JavaScript, chart ⽚,
And others jar package . become involved war Package format can only be Tomcat distinguish .

6. The deployment process

hold war Package copy ⻉ To Tomcat Of webapps ⽬ Record . Note that it must be webapps Under the root path of .

Click on startbat, start-up Tomcat .

7. Verification procedure

 

At this point, access through the browser localhost:8080/first-servlet/hello 

 8. install Smart Tomcat plug-in unit

1.  menu -> ⽂ Pieces of -> Settings --  Plugins --  Marketplace -- Search for "tomcat" -- "Install".

2. After installation , restart IDEA

9. Configuration plug-ins

1. Click top right ⻆ Of "Add Configuration"
2. Choose the one on the left "Smart Tomcat"
3. stay Name this ⼀ Fill in ⼀ A name ( I could just write )

 

 

 

 

  Be careful : Deployment Directory Is the running path , Be sure to configure it to webapp Under the table of contents

        server Port It's the port number , If 8080 occupied , Modify port number .

 

 

  add to   @WebServlet("/hello") Medium hello

 

send ⽤ Smart Tomcat When deployed , We found that Tomcat Of webapps The inside is not tortured ⻉⼀ individual war package , I didn't see the decompressed content .
Smart Tomcat Quite so in Tomcat When starting, directly lead ⽤ Item ⽬ Medium webapp and target ⽬ record

 

 10. Access error

404   Express ⽤ The resource accessed by the user does not exist . ⼤ The probability is URL The path of is not written correctly .

 

 

 405 Indicates the corresponding HTTP request ⽅ The law has not been realized .

 500  yes Servlet The exception thrown in the code results in .

  appear " empty ⽩⻚⾯"

  appear "⽆ Can't access this ⽹ standing " ---  ⼀ Like Tomcat The startup failed

copyright notice
author[Youcan.],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/131/202205102137278924.html

Random recommended