[JavaEE] Servlet manual configuration and javaeeservlet Configuration

Source: Internet
Author: User

[JavaEE] Servlet manual configuration and javaeeservlet Configuration

1. Servlet

A Dynamic web Resource Development technology provided by sun is essentially a java applet that can add Sevlet to the Servlet container.

* Servlet container -- the environment that can run Servlet is called Sevlet container -- tomcat

* Web Container -- the environment that can run web applications is called web Container -- tomcat

 

Write a class to implement the Servlet interface defined by sun

Configure the written class to tomcat

 

Define a class MyServlet to inherit the GenericServlet class and implement its abstract method service ()

Public void service (ServletRequest req, ServletResponse res) throws ServletException, java. io. IOException

A ServletResponse object is passed in, and the getWriter (). write () method of the ServletResponse object is called. Parameter: Text

Import related packages java. io javax. servlet

When compiling javac MyServlet. java through the command line, an error is reported. The servlet package cannot be found because this is the package under tomcat.

Set the environment variable in the command line, set classpath = % classpath %; position of the servlet package in lib under tomcat

Compile javac-d. MyServlet. java with a package

 

Configure the compiled package to the web application under tomcat

Create a WEB-INF directory under the web application directory

Create directory classes, directory lib, file web. xml under WEB-INF directory

Put the class in the classes directory

 

Configure the web. xml file and the <web-app> node,

Configure servlet <servlet>

Configure the Servlet name <servlet-name>

Configure the full path name of the class <servlet-class>

Configure the Sering <serlet-mapping>

Configure the virtual path <url-pattern>

MyServlet. java

package com.tsh;import java.io.*;import javax.servlet.*;public class MyServlet extends GenericServlet{    public void service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException{        res.getWriter().write("taoshihan");    }}

 

Web. xml

<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="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>

 

 

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.