Servlet开发 Servlet基础 Servlet API编程常用接口和类 Servlet开发

Servlet是运行在Web服务器端的Java应用程序,它是用Java语言编写的,具有Java语言的特点。并且Servlet对象主要封装了对Http请求的处理,并且他的运行需要Servlet容器的支持。

Servlet结构体系

Servlet开发
Servlet基础
Servlet API编程常用接口和类
Servlet开发

Servlet技术特点

因为Servlet是由Java语言编写的,不仅继承了Java语言的优点还对Web的相关应用进行了封装,同时对Servlet容器还提供了对应用的相对扩展。
主要技术优点:功能强大,可移植,性能高效,安全性能高,可扩展.

Servlet与JSP的区别

Servlet开发
Servlet基础
Servlet API编程常用接口和类
Servlet开发
Servlet开发
Servlet基础
Servlet API编程常用接口和类
Servlet开发

Servlet API编程常用接口和类

Servlet接口

方法 说明
public void init(SerletConfig config) Servlet实例化后,Servlet容器调用该方法来完成初始化工作
public void service(ServletRequest request,ServletResponse response) 用于处理客户端的请求
public void destory() 当Servlet 对象从Servlet 容器中移除时,容器调用该方法,以便释放资源
public ServletConfig getServletConfig() 用于获取Servlet 对象的配置信息,返回ServletConfig对象
public String getServletInfo() 返回有关Servlet 的信息,它是纯文本格式的字符串,如作者,版本

ServletConfig接口

Servlet开发
Servlet基础
Servlet API编程常用接口和类
Servlet开发

HttpServletRequest接口

Servlet开发
Servlet基础
Servlet API编程常用接口和类
Servlet开发

HttpServletResponse接口

Servlet开发
Servlet基础
Servlet API编程常用接口和类
Servlet开发

Servlet开发

import java.io.IOException;
import java.io.PrintWriter;

public class MyServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
    response.setContentType("text/html");
    response.setCharacterEncoding("GBK");
        PrintWriter out=response.getWriter();
        out.print("<HTML>");
        out.print("<HEAD><TITLE>Servlet实例</TITLE></HEAD>");
        out.print("<BODY>");
        out.print("Servlet实例<BR/>");
        out.print(this.getClass());
        out.print("</BODY");
        out.print("</HTML>");
        out.flush();
        out.close();


    }
}

Servlet开发
Servlet基础
Servlet API编程常用接口和类
Servlet开发
Servlet开发
Servlet基础
Servlet API编程常用接口和类
Servlet开发

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/servlet/MyServlet</url-pattern>
    </servlet-mapping>
</web-app>

Servlet开发
Servlet基础
Servlet API编程常用接口和类
Servlet开发