快速创建一个servlet并且在web.xml配置和使用它

一.什么是Servlet

举个例子,生活中往往会问:“先有鸡还是先有蛋”

这个问题恐怕至今也没有人能够回答,可是如果有人问你"是先有JSP还是先有Servlet?"


我可以告诉大家,是先有Servlet。JSP的前身就是Servlet。

Servlet是在服务器上运行的小程序,一个Servlet就是一个Java类,并且可以通过“请求-响应”的编程模型来访问这个驻留在服务器内存中的Servlet程序。

二.Tomcat容器等级


Tomcat容器->container容器->Engine引擎容器->HOST主机容器->Servlet容器->Context容器

一个Context对应一个WEB工程

三.编写第一个Servlet

3.1创建servlet


新建一个动态web工程


为项目命名


点击下一步勾选web.xml配置文件,如果直接点击[Finish]在目录中查看不到web.xml


创建完成后的目录结构,在WebContent下的WEB-INF中可以看到有web.xml配置文件


新建一个Java类


3.2继承HttpServlet


在Browse中选择继承HttpServlet类


生成的类代码框架


右键选择Source添加资源,选择重写方法


选择添加doGet()、doPost()方法


自动生成的doGet()、doPost()重写方法跟

3.3重写doGet()或者doPost()

public class HelloServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("处理Get请求。。。。。");
		PrintWriter out = response.getWriter();
		out.println("Hello Servlet-Get");
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("处理Post请求。。。。。");
		PrintWriter out = response.getWriter();
		out.println("Hello Servlet-Post");
	}
}

3.4在web.xml中注册Servlet


在web.xml选项中选择Source


初始化的代码配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>20180510</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>HelloServlet</servlet-name>
  	<servlet-class>servlet.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>HelloServlet</servlet-name>
  	<url-pattern>/servlet/HelloServlet</url-pattern>
  </servlet-mapping>
</web-app>

四.使用Servlet

新建一个index.jsp启动servlet

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
	<h1>第一个Servlet小例子</h1>
	<hr>
	<a href="servlet/HelloServlet">Get方式请求HellowServlet</a>
	
	
	<form action="servlet/HelloServlet" method="post">
		<input type="submit" value="Post方式请求HelloServlet">
	</form>
</body>
</html>

五.运行结果


初始化启动


Post方式


Get方式


  • 40
    点赞
  • 163
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值