servlet学习2
1.servlet
1.servlet是一个:java接口,我们需要创建一个类实现这个接口,我们才能使用servlet:方法。再复写方法,方法里面的内容,我们可以自己定义
servlet:方法:
package com.example.demo16;
import javax.servlet.*;
import java.io.IOException;
public class HelloServlet2 implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
servletResponse.getWriter().println("helloWorld!");//在浏览器可以输出,通过xml配置,输入网址
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
配置:servlet
在webapp下面的:WEF-INF有:
web.xml
我们:需要配置:servlet文件
<?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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>demo2</servlet-name>
<servlet-class>com.example.demo16.HelloServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo2</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
然后:重新启动tomcat服务器:
输入网址:
http://localhost:8080/demo16_war/hello//
访问到servlet.,执行类:实现servlet的 servlet(){}函数
把:里面的
servletResponse.getWriter().println(“helloWorld!”);//
响应到网页上去
2.servlet实现关系:
httpServlet
创建一个类继承httpServlet,重写方法
package com.example.demo16;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HelloSerrvlet3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//super.doGet(req, resp);
resp.getWriter().println("helloWolrd!");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
再配置这个类的文件:
<?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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>demo2</servlet-name>
<servlet-class>com.example.demo16.HelloServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo2</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>demo3</servlet-name>
<servlet-class>com.example.demo16.HelloSerrvlet3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo3</servlet-name>
<url-pattern>/helloo</url-pattern>
</servlet-mapping>
</web-app>
然后:重新启动服务器:输入网址:
http://localhost:8080/demo16_war/helloo
就能得到响应:
补充:
HttpServlet继承了GenericServlet (可以用ctrl+B:看到httpServlet继承关系)
public abstract class HttpServlet extends GenericServlet {}
GenericServlet 实现了 Servlet接口
public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {
private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.LocalStrings");
private transient ServletConfig config;
public GenericServlet() {
}
public void destroy() {
}
所以:
HttpServlet继承了GenericServlet
GenericServlet 实现了 Servlet接口
说明:httpServlet也和Servlet有关系