Servlet开发配置

本文主要简单实践一下servlet开发相关开发

1、Servlet的创建

两种方法:

  • 创建普通的java类,继承自HttpServlet类,在通过手动配置web.xml文件注册Servlet对象,比较复杂
  • 直接通过IDE集成开发工具进行创建

下面采用IDE的方法创建,具体步骤如下:

(1)创建一个动态的web项目,然后在包资源管理器中新建项目的名字上右键新建Servlet

20151215164942

(2)单击下一步,进入Servlet部署描述信息的页面,采用默认的配置

(3)继续下一步,进入选择修饰符、实现接口和生成的方法界面,采用默认设置,完成Servlet的创建

创建完Servlet后,打开相应的页面:

package com.wuyudong.www;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet 实现类  FirstServlet
 */
@WebServlet("/FirstServlet")
public class FirstServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     * 构造方法
     */
    public FirstServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //业务处理
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //业务处理
    }
 
}

2、Servlet配置的相关元素

1、声明Servlet对象

在web.xml中,通过 <servlet>标签声明一个servlet对象

<servlet-name>元素指定Servlet的名称

<servlet-class>元素用于指定Servlet对象的完整位置,包含Servlet对象的包名和类名

  <servlet>
    <servlet-name>SimpleServlet</servlet-name>
    <servlet-class>com.lyq.SimpleServlet</servlet-class>
  </servlet>

2、映射Sevlet

映射访问Servlet的url,使用<servlet-mapping>标签进行配置,分别包含 <servlet-name>和<url-pattern>。

配置如下:

  <servlet-mapping>
    <servlet-name>SimpleSevlet</servlet-name>
    <url-pattern>/SimpleSevlet</url-pattern>
  </servlet-mapping>

实践一下:

新建MyServlet.java

package com.wuyudong;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class MyServlet
 */
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 3563565034243126713L;
 
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("GBK");
        PrintWriter out = response.getWriter();
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>Servlet实例</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    Servlet实例: ");
        out.print(this.getClass());
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }
}

web.xml

    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>com.wuyudong.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/servlet/MyServlet</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>servlet/MyServlet</welcome-file>
    </welcome-file-list>

 

posted @ 2016-05-15 22:00  wuyudong  阅读(389)  评论(0编辑  收藏  举报
Top_arrow