Servlet technology is the java ee appeared before existed in the development of dynamic web pages, has been widely used, until now java ee project is also very important, but also the jsp developed on the basis of the servlet. Therefore, the master servlet too important.
Servlet server program is written in java, which is characterized by:
Ⅰ. He is a server-side (tomcat) called and executed
Ⅱ. He is using java language
Ⅲ. He is in accordance with the Servlet specification development
Ⅳ. Powerful, can do almost all the website features
Ⅴ. Jsp learning foundation
Servlet interface to achieve
This is a way to use the earliest, with more trouble, why do you want to learn servlet interfaces? Learn to understand it because it servlet life cycle is very good. Servlet interface implementation steps:
Ⅰ. Establishment of a web application MyServletWeb
Ⅱ.MyServletWeb established under WEB-INF / web.xml, you can copy items in ROOT
Ⅲ. Establish classes directory under MyServletWeb (Servlet our will in the project development), l establish lib directory
Ⅳ. Development MyFirstServlet.java, implement the interface servlet
Ⅴ. According to the Servlet specification, we also need to deploy Servlet, deployed in the web.xml file
NOTE: If you use javac to compile a java file, you need to take command parameters javac -d. MyFristServlet.java file
MyFristServlet file
package com.beekc.www; import javax.servlet.*; import javax.servlet.http.*; import java.io. *; import java.lang.*; public class MyFristServlet implements Servlet { // This function initializes a servlet, the servlet is loaded into the memory // it will only be called once public void init(ServletConfig config) throws ServletException { } // get the object ServletConfig public ServletConfig getServletConfig() { return null; } // This function is a function of the service, our business logic code that is written here // This function is called every time public void service(ServletRequest req,ServletResponse res) throws ServletException,IOException { res.getWriter().println("Hello world " + new java.util.Date()); } // This function is obtained servlet configuration information public String getServletInfo() { return null; } // destroy the servlet, cleared from memory // it will only be called once public void destroy() { } }
web.xml file
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <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_4_0.xsd" version="4.0" metadata-complete="true"> <! - The serlvet specification, to the need to deploy Servlet web.xml file, the deployment can be copied from the examples -> <servlet> <-! Servlet-name is in the future to access the servlet resource name, the name can define your own: you use the default name for the servlet -> <servlet-name>MyFristServlet</servlet-name> <-! Servlet-class to specify which package placed under Servlet -> <servlet-class>com.beekc.www.MyFristServlet</servlet-class> </servlet> <-! Servlet Mapping -> <servlet-mapping> <! - The servlet-name, and to name the same as the above servlet-name -> <servlet-name>MyFristServlet</servlet-name> <! - Servlet name to access some of the resources -> <url-pattern>/MyFristServlet</url-pattern> </servlet-mapping> </web-app>
Ⅵ. Test Results