1. 程式人生 > >使用MyEclipse建立一個Servlet步驟

使用MyEclipse建立一個Servlet步驟

場景:使用MyEclipse建立一個Servlet步驟

第一種方式使用嚮導建立
1.開啟IDE, 選擇 File->New->Web Project
2.填寫專案名稱,點選Finish


3.專案結構如圖


4.使用Servlet Wizard建立,選擇 File->New->others... 點選進入如下向導框


5.填寫專案


6.點選Finish完成即可


7.可以看到生成TestServletWizardDemo類繼承了HttpServlet

public class TestServletWizardDemo extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * 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("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * 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("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

}

8.同時web.xml中自動添加了配置

<servlet>
    <display-name> </display-name>
    <servlet-name>TestServletWizardDemo</servlet-name>
    <servlet-class>com.zbz.TestServletWizardDemo</servlet-class>
  </servlet>

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

9. 啟動服務,根據以上資訊可以組裝url在瀏覽器訪問   http://127.0.0.1:8080/TestDemo/servlet/TestServletWizardDemo      
 

第二種方式,不用嚮導建立
1.在上面建立專案中建立類TestServletDemo,手動新增實現HttpServlet

public class TestServletDemo extends HttpServlet{
	
	private static final long serialVersionUID = -5956706735123527929L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		/**中文亂碼*/
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		String str ="這是手動建立的servlet";
		out.println(str);
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}
}

2. 在web.xml中配置servlet

<!-- 手動配置servlet start -->
	<servlet>
		<display-name>
		</display-name>
		<servlet-name>testServlet</servlet-name>
		<servlet-class>com.zbz.TestServletDemo</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>testServlet</servlet-name>
		<url-pattern>/test</url-pattern>
	</servlet-mapping>
    <!-- 手動配置servlet end -->

3.啟動服務,根據以上資訊可以組裝url在瀏覽器訪問  http://127.0.0.1:8080/TestDemo/test

以上TKS.