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

直接上图,不废话!!!

第一:首先在Eclipse的包资源管理器中,单机鼠标右键,在弹出的快捷键菜单中选择“新建”/Servlet命令,在弹出的对话框中输入新建的Servlet所在的包和类名,然后单击下一步,

 

我选择默认,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("<HEAD><TITLE>Servlet实例</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" id="WebApp_ID" version="3.0">
 3   <display-name>web02</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   <servlet>
13       <!-- 声明Servlet对象 -->
14       <servlet-name>FirstServlet</servlet-name>
15       <!-- 上面一句指定Servlet对象的名称 -->
16       <servlet-class>com.ningmeng.FirstServlet</servlet-class>
17       <!-- 上面一句指定Servlet对象的完整位置,包含包名和类名 -->
18   </servlet>
19   <servlet-mapping>
20       <!-- 映射Servlet -->
21       <servlet-name>FirstServlet</servlet-name>
22       <!--<servlet-name>与上面<Servlet>标签的<servlet-name>元素相对应,不可以随便起名  -->
23       <url-pattern>/FirsetServlet</url-pattern>
24       <!-- 上面一句话用于映射访问URL -->
25   </servlet-mapping>
26 </web-app>

运行效果如下图所示

 

posted on 2016-10-13 20:12  别先生  阅读(50494)  评论(1编辑  收藏  举报