JavaWeb学习(一)创建简单的Servlet

一、新建工程

File——New——Project,新建工程ServletStudy0809。

JavaWeb学习(一)创建简单的Servlet_第1张图片

项目工程树:

JavaWeb学习(一)创建简单的Servlet_第2张图片

二、创建Servlet

(1)右键包——New——Servlet

JavaWeb学习(一)创建简单的Servlet_第3张图片

(2)Creat a New Servlet

JavaWeb学习(一)创建简单的Servlet_第4张图片

JavaWeb学习(一)创建简单的Servlet_第5张图片

三、Servlet创建好后得到servletDemo.java文件,同时web.html有新加入的内容。

package com.servlet.study;

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 servletDemo extends HttpServlet {

	/**
	 * 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(); } }

JavaWeb学习(一)创建简单的Servlet_第6张图片

四、在服务器上运行

JavaWeb学习(一)创建简单的Servlet_第7张图片

参考:http://www.cnblogs.com/xdp-gacl/p/3760336.html#!comments

你可能感兴趣的:(JavaWeb)