[Java EE] Manual configuration of the servlet

Source: Internet
Author: User

A Servlet

Sun provides a dynamic Web Resources development Technology, essentially a Java applet, you can add sevlet to the Servlet in the container

*servlet Container -- the environment that can run the Servlet is called the sevlet container. --Tomcat

*web Container - - The environment that can run a Web application is called a Web container --Tomcat

Write a class to implement the Servlet interface defined by Sun Company

Configure the written class to Tomcat

Defines a class Myservlet inherits the genericservlet class, implementing its abstract method service ()

public void Service (ServletRequest req, servletresponse Res) throws Servletexception, Java.io.IOException

Pass in a servletresponse object that calls the servletresponse object's getwriter (). Write () method, Parameters: Text

Import the related package java.io Javax.servlet

Command-line compilation Javac Myservlet.java An error is reported andtheservlet package cannot be found because this is a package under Tomcat

command line setting environment variable,set Classpath=%classpath%;tomcat the location of the servlet package under Lib

Command-line compilation javac-d. Myservlet.java, with package compilation

Configure the compiled package into a Web application under Tomcat

Create a new web-inf directory under the Web app directory

Create a new directory under the web-inf directory classes, directory lib, file Web. xml

Put the class in the classes directory

Configure the Web. XML file, configuration <web-app> node,

Configuring servlets <servlet>

Configure the Servlet name <servlet-name>

Full path name of the configuration class <servlet-class>

Configuration mapping <serlet-mapping>

Configure virtual paths <url-pattern>

Myservlet.java

 Package com.tsh; import java.io.*; import javax.servlet.*;  Public class extends genericservlet{    publicvoidthrows  servletexception, java.io.ioexception{        Res.getwriter (). Write ("Taoshihan");}    }

Xml

<?XML version= "1.0" encoding= "Iso-8859-1"?><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"><servlet>        <Servlet-name>Myservlet</Servlet-name>        <Servlet-class>Com.tsh.MyServlet</Servlet-class>    </servlet>    <servlet-mapping>        <Servlet-name>Myservlet</Servlet-name>        <Url-pattern>/index</Url-pattern>    </servlet-mapping></Web-app>

[Java EE] Manual configuration of the servlet

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.