01 Servlet

1 web.xml

  ...
      <servlet>
      	<servlet-name>hello</servlet-name>
      	<servlet-class>web.Hello</servlet-class>
      </servlet>
      <servlet-mapping>
      	<servlet-name>hello</servlet-name>
      	<url-pattern>/hello</url-pattern>
      </servlet-mapping>
  ...

2 servlet,需要servlet-api.jar,可以在tomcat\lib下找到

package web;

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

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 处理request
		String username = request.getParameter("username");
		// 处理response
		response.setContentType("text/html");
		PrintWriter pw = response.getWriter();
		pw.println(username);
		pw.close();
	}
}

3 deploy,启动tomcat,以

http://localhost:8080/{appName}/hello

或者

http://localhost:8080/{appName}/hello?username=xxx

访问web.Hello

你可能感兴趣的:(01 Servlet)