How to run a simple servlet successfully

Source: Internet
Author: User

Well, it's a simple question.

For a variety of reasons, this time is not, direct steps and code.

1. Pre-preparation

JDK, Tomcat, EditPlus (Eclipse) installed successfully and set environment variables.

2, because JDK (javase) is unable to compile the servlet directly, it is necessary to copy the Tomcat installation directory \lib\servlet-api.jar to the Java installation directory \lib, and in order to ensure that there is no problem, You can add ";%catalina_home%\lib\servlet-api.jar" after the system variable Classpath (provided the catalina_home is set up well.) )

3. Create a file structure

I created a new my folder under the WebApps directory of Tomcat and added the SRC folder (put all Java files including servlet), Web-inf folder (Put all classes classes, jar packages, Static page and Web. xml), create a new two folder in the Web-inf folder: Classes and lib.

4, can now write code, in the SRC folder under the new Folder Servlet, in the Servlet folder to create a new Java file, named Helloworldservlet.java.

1  Packageservlet;2 3 //Packing, note this place, will be used when configuring Web. XML4 5 Importjavax.servlet.http.*;6 Importjavax.servlet.*; 7 ImportJava.io.*;8 9 //the above three import statements are used by Servlets, especially the first twoTen  One  Public classHelloworldservletextendshttpservlet{ A  -     protected voidDoget (HttpServletRequest Req,httpservletresponse resp)throwsioexception,servletexception{ -  thePrintWriter out = Resp.getwriter ();//print stream, auto flush, no exception thrown -Out.println ("); -         -         //function is output helloworld! field +  - Out.flush (); +  A         //Flush () empties the buffer's data at  - out.close (); -  -         //Close -  -     }  in } -         

Because I use EditPlus, so I manually compile it (to be honest, there is no two, more I do not know how to do better ... )

Run--CMD--CD D:\apache-tomcat-8.0.12\webapps\my\src\servlet (this path can be changed according to the actual situation)--javac Helloworldservlet.java

If the problem is garbled, please see my previous article, "Troubleshooting Java file Encoding and Windows7 system (Chinese version) default encoding conflicts caused by garbled conditions"

After a successful compilation, copy the generated class file to the Classes folder in the Web-inf folder. Attention Because the location of the Helloworldservlet.java is within the servlet folder, so do the same here, in the Classes folder also to create a new servlet folder, and put the Helloworldservlet.class file into it.

5, do not think this is finished. Configuring Web. XML is also a big deal. If this is not configured, 100% 404 (I'm not going to tell you that my egg hurts for a long time)

If you are using Eclipse or other tools, you can choose to create the default Web. XML when you build the project (there is very little content on the inside, only some welcome page settings). I was using the Tomcat in the example project to change, you can refer to:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 3 <Web-appxmlns= "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"
Metadata-complete= "true">4 5 <servlet> 6 <Servlet-name>Hello</Servlet-name> 7 <Servlet-class>Servlet. Helloworldservlet</Servlet-class> 8 </servlet> 9 <servlet-mapping> Ten <Servlet-name>Hello</Servlet-name> One <Url-pattern>/helloworldservlet</Url-pattern> A </servlet-mapping> - </Web-app> -

The bold content is for the settings of the servlet written above. We can do the same (to tell you the truth, I also do not understand, we self-Baidu bar) ...

6, finally, start Tomcat, input http://localhost:8080/my/HelloWorldServlet, done.

How to run a simple servlet successfully

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.