Servlet开发技术,创建,以及Servlet的配置,web.xml的配置

我选择默认,Next

技术分享

依旧默认,Next

技术分享

注意事项,在创建web的工程的时候需要注意的是如下图,选择上web.xml这个按钮,因为Servlet需要配置一下web.xml这个文件,web.xml在web-inf这个文件下面

技术分享

技术分享

技术分享

上图第二个箭头前面有个方括号,勾选上就可以自动创建好web.xml这个文件了。

技术分享
 1 package com.ningmeng;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 /**
13  * Servlet implementation class FirstServlet
14  */
15 @WebServlet("/FirstServlet")
16 public class FirstServlet extends HttpServlet {
17     private static final long serialVersionUID = 1L;
18        
19     /**
20      * @see HttpServlet#HttpServlet()
21      */
22     public FirstServlet() {
23         super();
24         // TODO Auto-generated constructor stub
25     }
26 
27     /**
28      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
29      */
30     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
31         // TODO Auto-generated method stub
32         response.setContentType("text/html");
33         response.setCharacterEncoding("GBK");
34         PrintWriter out=response.getWriter();
35         out.print("HTML");
36         out.println("HEADTITLEServlet实例/TITLE/HEAD");
37         out.println("BODY");
38         out.println("servlet实例");
39         out.println(this.getClass());
40         out.println("/BODY");
41         out.println("/HTML");
42         out.flush();
43         out.close();
44         response.getWriter().append("Served at: ").append(request.getContextPath());
45     }
46 
47     /**
48      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
49      */
50     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
51         // TODO Auto-generated method stub
52         doGet(request, response);
53     }
54 
55 }
技术分享

技术分享
 1 ?xml version="1.0" encoding="UTF-8"?
 2 web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"
 3   display-nameweb02/display-name
 4   welcome-file-list
 5     welcome-fileindex.html/welcome-file
 6     welcome-fileindex.htm/welcome-file
 7     welcome-fileindex.jsp/welcome-file
 8     welcome-filedefault.html/welcome-file
 9     welcome-filedefault.htm/welcome-file
10     welcome-filedefault.jsp/welcome-file
11   /welcome-file-list
12   servlet
13       !-- 声明Servlet对象 --
14       servlet-nameFirstServlet/servlet-name
15       !-- 上面一句指定Servlet对象的名称 --
16       servlet-classcom.ningmeng.FirstServlet/servlet-class
17       !-- 上面一句指定Servlet对象的完整位置,包含包名和类名 --
18   /servlet
19   servlet-mapping
20       !-- 映射Servlet --
21       servlet-nameFirstServlet/servlet-name
22       !--servlet-name与上面Servlet标签的servlet-name元素相对应,不可以随便起名  --
23       url-pattern/FirsetServlet/url-pattern
24       !-- 上面一句话用于映射访问URL --
25   /servlet-mapping
26 /web-app
技术分享

运行效果如下图所示

技术分享

Servlet开发技术,创建,以及Servlet的配置,web.xml的配置

原文地址:http://www.cnblogs.com/zhujiabin/p/7294154.html


最新回复(0)
/jishuess1qxjYvHTS1UO_2BEehpA3RFfyVm6eI_2Fbpx7Kw_3D_3D4719063
8 简首页