javaweb 的应用我们需要参考javaee api
查找servlet接口
javax.servlet
Interface Servlet
- All Known Subinterfaces:
- HttpJspPage, JspPage
- All Known Implementing Classes:
- FacesServlet, GenericServlet, HttpServlet
-
public interface Servlet
Implemented by: FacesServlet, GenericServlet, JspPage
定义所有 servlet 都必须实现的方法。
servlet 是运行在 Web 服务器中的小型 Java 程序。servlet 通常通过 HTTP(超文本传输协议)接收和响应来自 Web 客户端的请求。
要实现此接口,可以编写一个扩展 javax.servlet.GenericServlet
的一般 servlet,或者编写一个扩展 javax.servlet.http.HttpServlet
的 HTTP servlet。
此接口定义了初始化 servlet 的方法、为请求提供服务的方法和从服务器移除 servlet 的方法。这些方法称为生命周期方法,它们是按以下顺序调用的:
- 构造 servlet,然后使用
init
方法将其初始化。 - 处理来自客户端的对
service
方法的所有调用。 - 从服务中取出 servlet,然后使用
destroy
方法销毁它,最后进行垃圾回收并终止它。
除了生命周期方法之外,此接口还提供了 getServletConfig
方法和 getServletInfo
方法,servlet 可使用前一种方法获得任何启动信息,而后一种方法允许 servlet 返回有关其自身的基本信息,比如作者、版本和版权。
See also
javax.servlet.GenericServlet, javax.servlet.http.HttpServlet
Defines methods that all servlets must implement.
A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.
To implement this interface, you can write a generic servlet that extends javax.servlet.GenericServlet
or an HTTP servlet that extends javax.servlet.http.HttpServlet
.
This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from the server. These are known as life-cycle methods and are called in the following sequence:
- The servlet is constructed, then initialized with the
init
method. - Any calls from clients to the
service
method are handled. - The servlet is taken out of service, then destroyed with the
destroy
method, then garbage collected and finalized.
In addition to the life-cycle methods, this interface provides the getServletConfig
method, which the servlet can use to get any startup information, and the getServletInfo
method, which allows the servlet to return basic information about itself, such as author, version, and copyright.
由此我们看出 要想实现动态的网页必须集成servlet接口 或者 继承javax.servlet.GenericServlet
or an HTTP servlet that extends javax.servlet.http.HttpServlet
.
我们创建一个 servlet的demo
先添加一个 servlet类 集成接口Servlet
package cn.jiemoxiaodi.servlet; import java.io.IOException; import java.io.OutputStream; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class AServlet implements Servlet{ public void destroy() { // TODO Auto-generated method stub System.out.println("destory"); } public void init(ServletConfig arg0) throws ServletException { // TODO Auto-generated method stub System.out.println("init"); } public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { // TODO Auto-generated method stub OutputStream ops= arg1.getOutputStream(); ops.write("hellow".getBytes()); System.out.println("hello--service"); } public ServletConfig getServletConfig() { // TODO Auto-generated method stub return null; } public String getServletInfo() { // TODO Auto-generated method stub return null; } }
我们创建完类型后我们要告诉我们的服务器,有这个动态网页资源啦 在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"> <display-name></display-name> <!-- 配置servlet到项目中 --> <servlet> <!-- 可以随便填只需要注意不要和其他servlet的名字重复即可(建议使用简单类名) --> <servlet-name>AServlet</servlet-name> <!-- 配置servlet的完整类名 --> <servlet-class>cn.jiemoxiaodi.servlet.AServlet</servlet-class> </servlet> <!-- 配置上面这个servlet使用那个路径能被访问 --> <servlet-mapping> <!-- 此处不能随便写了,必须与上面的servlet对应,表示在为那个servlet配置路径 --> <servlet-name>AServlet</servlet-name> <!-- 配置访问这个servlet的路径 (相对路径) /==>http://localhost:8080/day10_servlet/ http://localhost:8080/day10_servlet/AServlet --> <url-pattern>/AServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
到此就可以了。
http://localhost:8080/项目名字/AServlet