javaEE 部署Servlet文件的步骤

2014-11-23 21:26:12 · 作者: · 浏览: 10

① 编写Servlet并编译成class文件,编译时,将servlet-api.jar文件(在/common/lib 目录下)加入到classpath中;

② 将编译好的class文件拷贝到应用的WEB-INF/classes/下(如/myapp/WEB-INF/classes/);

③ 在web.xml中进行配置 ,具体可参见下面的web.xml。

这里引用myeclipse默认生成的servlet实例:

servlet:

package com.unilay.test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Test extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Test() {
		super();
	}

	/**
	 * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println(" A Servlet"); out.println(" "); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" "); out.println(""); out.flush(); out.close(); } /** * The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println(" A Servlet"); out.println(" "); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" "); out.println(""); out.flush(); out.close(); } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }

web.xml:

  

  
  
    
    
     This is the description of my J2EE component
     
    
     This is the display name of my J2EE component
     
    
     Test
     
    
     com.unilay.test.Test
     
   

  
    
    
     Test
     
    
     /servlet/Test
     
   


  

然后在 浏览器中运行http://127.0.0.1:8080/Test/servlet/Test