码迷,mamicode.com
首页 > 其他好文 > 详细

servlet实现新闻控制

时间:2015-04-20 11:33:09      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

1、概述

技术分享

技术分享

技术分享

2、Servlet API

 技术分享

技术分享

3、Servlet生命周期

技术分享

 4、部署运行Servlet

 技术分享

样例:

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被销毁,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>

技术分享

<?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实现新闻控制

标签:

原文地址:http://my.oschina.net/u/2320342/blog/403682

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!