前言:兄弟们看到这里可以跳过了,不是教程是笔记
文章目录
作为了解即可,开发不这样使用
第一个Servlet
Java-Servlet
第一个Get,复习的时候看看注释
package com.stackery.servlet;
import javax.servlet.*;
import java.io.IOException;
public class HelloServlet implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
/**
* Service方法专门用来处理请求和响应
* @param servletRequest
* @param servletResponse
* @throws ServletException
* @throws IOException
*/
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("Hello Servlet被访问了");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
XML配置
<!-- servlet标签给程序配置Servlet程序-->
<servlet>
<!-- servlet-name标签给servlet起一个别名-->
<servlet-name>HelloServlet</servlet-name>
<!-- servlet-class标签是servlet的全类名-->
<servlet-class>com.stackery.servlet.HelloServlet</servlet-class>
</servlet>
<!-- servlet-mapping标签给程序配置访问地址-->
<servlet-mapping>
<!-- servlet-name标签告诉服务器,当前配置地址给哪个Servlet程序使用 -->
<servlet-name>HelloServlet</servlet-name>
<!-- url-pattern 配置访问地址-->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
访问即可,调用service函数
解决中文乱码问题
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
>
Servlet生命周期
- 1、执行Servlet构造器方法
- 2、执行init初始化方法
- 3、执行service方法
- 4、执行destroy销毁方法
第一、 二步,是在第一次访问的时候创建Servlet程序会调用。
第四步在WEB工程停止的时候才会被调用
看是否为GET或者POST方法
package com.stackery.servlet;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class HelloServlet implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
/**
* Service方法专门用来处理请求和响应
* @param servletRequest
* @param servletResponse
* @throws ServletException
* @throws IOException
*/
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("Hello Servlet被访问了");
// 转换为子类,因为子类才有这个方法
HttpServletRequest httpServlet = (HttpServletRequest) servletRequest;
//获取是get请求还是Post请求
String method = httpServlet.getMethod();
System.out.println(method);
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
开发中用法
简单说明
开发中通常都是继承子类去实现Servlet
,实际开发都是继承HttpServlet
实现
- 1、编写一个类去继承HttpServlet 类
- 2、根据业务需要重写doGet或doPost方法
- 3、到web.xml中的配置Servlet程序的访问地址
简单示例
package com.stackery.servlet;
import javax.servlet.ServletConfig;
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 HelloServlet2 extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
System.out.println("init被调用了");
}
/**
* doGet()在get请求被调用
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("GET请求");;
ServletConfig servletConfig = getServletConfig();
// 1、可以获取 Servlet 程序的别名 servlet-name 的值
System.out.println(servletConfig.getServletName());
// 2、获取初始化参数 init-param
System.out.println("初始化参数username的值为"+servletConfig.getInitParameter("username"));
System.out.println("初始化参数password的值为"+servletConfig.getInitParameter("password"));
// 3、获取 ServletContext 对象
System.out.println(servletConfig.getServletContext());
}
/**
* doPost()在Post请求被调用
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("POST请求");;
}
}