Black Horse day03 servlet Quick Start

Source: Internet
Author: User

First, the Servlet
1.sun provides a dynamic web resource development technology. Essentially, it's a Java applet. You can add servlets to the servlet container to run.
*servlet container-an environment that can run a servlet is called a servlet container. ---Tomcat
*web Container--the environment that can run a Web application is called the Web container---Tomcat

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

Configure a well-written class to the Web application's XML in tomcat (configure the external access path)

Next, we write a servlet ourselves and put it under Tomcat to run.

1. Write a class Firstservlet, in order to facilitate our direct inheritance of Genericservlet abstract class, directly implement the service method.

Package Com.itheima;import javax.servlet.*;import Java.io.*;class Firstservlet extends Genericservlet {public void Service (ServletRequest req, servletresponse Res) throws Servletexception, ioexception{Res.getwriter (). Write ("Hello World! ");}}
2. Compile into A. class file with a package. We need the servlet jar package.

E:\code>set classpath=%classpath%; E:\apache-tomcat-6.0.14\lib\servlet-api.jar; E:\code>javac Firstservlet.javafirstservlet.java:6: Error: Unable to find Symbol                response.getwriter (). Write ("Hello world!");                ^  symbol:   variable response  Position: Class FirstServlet1 error E:\code>javac firstservlet.javae:\code>javac-d.  Firstservlet.javae:\code>

At the end of the run, there is a Com-->itheima-->firstservlet file. Put this folder under Tomcat's WebApp to run.

Then build web-inf/classes lib Web. xml

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "><servlet>        <servlet-name>firstservlet</ servlet-name>        <servlet-class>com.itheima.FirstServlet</servlet-class>    </servlet>    <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/servlet/ firstservlet</url-pattern> </servlet-mapping>  <welcome-file-list>    <welcome-file >index.jsp</welcome-file>  </welcome-file-list></web-app>

Running results can be seen in the browser Hello world!



Black Horse day03 servlet Quick Start

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.