eclipse+tomcat+httpServlet初学

1.在eclipse中配置tomcat,参考上一边文章,建立的web项目tomcatWeb目录结构

eclipse+tomcat+httpServlet初学_第1张图片

2.添加Servlet处理类

eclipse+tomcat+httpServlet初学_第2张图片

3. 实现doGet()与doPost()方法:

public class HttpServletDemo extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4230239235404655360L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		response.setContentType("text/html;charset=GB2312");
		PrintWriter printWriter = response.getWriter();
		// printWriter.print("hello, china");
		String title = "HelloServlet";
		String heading1 = "HelloServlet的doGet方法的输出:";
		printWriter = response.getWriter();
		printWriter.print("" + title + "");
		printWriter.print("");
		printWriter.print(heading1);
		printWriter.println("

:您好

"); printWriter.print(""); printWriter.write(""); printWriter.flush(); printWriter.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { doGet(request, response); } }
4. 在web.xml中配置servlet

 
    TomcatWeb
    tomcat.servlet.HttpServletDemo
  

  
    TomcatWeb
    /servlet/HttpServletDemo
  


5. 用配置好的tomcat容器运行该web项目

方式1:右击项目--run as - run on server - 选择配置好的tomcat容器

方式2:eclipse+tomcat+httpServlet初学_第3张图片右击运行


6. 在浏览器上输入:http://localhost:8080/TomcatWeb/servlet/HttpServletDemo,即可看到

eclipse+tomcat+httpServlet初学_第4张图片


转载于:https://www.cnblogs.com/marcotan/p/4256919.html

你可能感兴趣的:(eclipse+tomcat+httpServlet初学)