servlet实现新闻控制

1、概述

servlet实现新闻控制_第1张图片

servlet实现新闻控制_第2张图片

servlet实现新闻控制_第3张图片

2、Servlet API

 servlet实现新闻控制_第4张图片

servlet实现新闻控制_第5张图片

3、Servlet生命周期

servlet实现新闻控制_第6张图片

 4、部署运行Servlet

 servlet实现新闻控制_第7张图片

样例:

package com.ljb.servlet;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet3 extends HttpServlet{
 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  System.out.println("处理请求,doGet方法被调用。");
 }
 @Override
 public void destroy() {
  System.out.println("servlet被销毁,destroy方法被调用。");
 }
 @Override
 public void init(ServletConfig config) throws ServletException {
  System.out.println("servlet初始化,init方法被调用。");
 }
 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  System.out.println("处理请求,doPost方法被调用。");
 }
// @Override
// protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
//   throws ServletException, IOException {
//  // TODO Auto-generated method stub
//  super.service(arg0, arg1);
// }
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ServletDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
      <servlet-name>myServlet3</servlet-name>
      <servlet-class>com.ljb.servlet.MyServlet3</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>myServlet3</servlet-name>
      <url-pattern>/myServlet</url-pattern>
  </servlet-mapping>
</web-app>

servlet实现新闻控制_第8张图片

停止服务:

servlet被销毁,destroy方法被调用。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ServletDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
      <servlet-name>myServlet3</servlet-name>
      <servlet-class>com.ljb.servlet.MyServlet3</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>myServlet3</servlet-name>
      <url-pattern>/demo/*</url-pattern>
  </servlet-mapping>
</web-app>

servlet实现新闻控制_第9张图片

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ServletDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
      <servlet-name>myServlet3</servlet-name>
      <servlet-class>com.ljb.servlet.MyServlet3</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>myServlet3</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

servlet实现新闻控制_第10张图片

 servlet实现新闻控制_第11张图片

servlet实现新闻控制_第12张图片

 5、完成信息添加功能

servlet实现新闻控制_第13张图片

servlet实现新闻控制_第14张图片

 

你可能感兴趣的:(api,S​ervlet)