1. 第一个Servlet程序

package com.allanlxf.serv.basic;
import javax.servlet.*;
import java.io.*;
public class TimeServlet implements Servlet
{
    private ServletConfig config;
    
    public TimeServlet()
    {
        System.out.println("TimeServlet()");
    }
    
    public void init(ServletConfig config) throws ServletException
    {
        this.config = config;
        System.out.println("init(" + config + ")");
    }
    
    public void service(ServletRequest request, ServletResponse response)
                    throws ServletException, IOException
    {
        System.out.println(this);
        System.out.println("service(" + request + "," + response + ")");
        getServletConfig();
        PrintWriter out = response.getWriter();
        out.println("<h1>");
        out.println(new java.util.Date());
        out.println("</h1>");
        out.close();
    }
    
    public void destroy()
    {
        System.out.println("destroy");
    }
    
    public String getServletInfo()
    {
        return "time servlet by allan version 1.0";
    }
    
    public ServletConfig getServletConfig()
    {
        return config;
    }  
}


2. 编写及布署Servlet类

(1)编写一个类继承自HttpServlet

package com.allanlxf.serv.basic;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class HttpTimeServlet extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        out.println("<html>");
        out.println("<head>");
        out.println("<title>currentTime</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h3 align=\"center\">Current Time is</h3>");
        out.println("<hr>");
        out.println(new java.util.Date());
        out.println("</body>");
        out.println("</html>");
        out.close();
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException
    {
        doGet(request, response);
    }
}


(2)在Tomcat中布署一个myFirst-app web应用

---Tomcat

---webapps

---myFirst-app

---WEB-INF

---classes

---web.xml

---shoppingCart

(3)配置Web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
    
<servlet> 
      <servlet-name>ActionServlet</servlet-name>
      <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>  
     
   
  <servlet>
    <servlet-name>time</servlet-name>
    <servlet-class>com.allanlxf.serv.basic.TimeServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet>
    <servlet-name>HttpTimeServlet</servlet-name>
    <servlet-class>com.allanlxf.serv.basic.HttpTimeServlet</servlet-class>
  </servlet>
 <servlet-mapping> 
      <servlet-name>ActionServlet</servlet-name>
      <url-pattern>*.do</url-pattern>
    </servlet-mapping>
  
  <servlet-mapping>
    <servlet-name>time</servlet-name>
    <url-pattern>/basic/time</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>HttpTimeServlet</servlet-name>
    <url-pattern>/basic/http/time</url-pattern>
  </servlet-mapping>
<listener>
     <listener-class>com.chu.shoppingCart.web.shoppingCartListner</listener-class>
    </listener>
</web-app>


3. 常用类介绍

(1)Filter类

public class CharacterEncodingFilter implements Filter
{
    private FilterConfig config;
    
    public void init(FilterConfig config) throws ServletException
    {        
        this.config = config;
    }
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    {
        String encoding = config.getInitParameter("encoding");
        if(encoding == null)
        {
            encoding = "gbk";
        }
        
        request.setCharacterEncoding(encoding);
        response.setCharacterEncoding(encoding);
        
        chain.doFilter(request, response);
    }
    public void destroy()
    {
    }
}






Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐