Servlet创建(二)

一、通过MyEclipse创建servlet

1. 创建一个webProject,名为servletTest

2. src下新建HelloServlet.java文件

package com.it.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet{

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.getWriter().write("Hello Servlet");//将信息显示在页面上
	}
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

3. WEB-INF下配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1"
  metadata-complete="true">

	<servlet>
		<servlet-name>HS</servlet-name>
		<servlet-class>com.it.servlet.HelloServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>HS</servlet-name>
		<url-pattern>/hs</url-pattern>
	</servlet-mapping>
  </web-app>

4.WebRoot下配置1.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="/day7_1/hs" method="get">
		<input type="submit" value="submit"/>
	</form>
</body>
</html>

二、Servlet体系

三、创建一个servlet步骤:

1. 创建一个类,去继承HttpServlet(实现Servlet接口或继承GenericServlet)

2. 重写方法 doGet doPost(接口中需要将所有方法重写,但是service方法时处理请求的)

3. 在web.xml文件中配置

四、web.xml配置Servlet访问虚拟路径

五、GET方式请求提交映射关系

六、Servlet的接口实现关系

猜你喜欢

转载自blog.csdn.net/Ada_yangyang/article/details/81565456