- JavaEE 5 API 在线文档(version_en):https://tool.oschina.net/apidocs/apidoc?api=javaEE5
- Servlet 技术规范文档(JavaEE 3)下载地址:https://download.csdn.net/download/Regino/12324213
import javax.servlet.*;
import java.io.IOException;
public class DemoServlet 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 {
System.out.println("Hello Servlet");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
<web-app 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_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>demoServletservlet-name>
<servlet-class>DemoServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>demoServletservlet-name>
<url-pattern>/demourl-pattern>
servlet-mapping>
web-app>
HTTP Status 500 – Internal Server Error
可能是版本不兼容导致的文件未找到错误。Error:java: 无效的源发行版: 12
的修复办法:
@WebServlet()
注解中添加 urlPatterns = /hello
,作为请求路径。import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
//name = "HelloServlet":servlet名称,相当于web.xml中的
//urlPatterns = "/hello":servlet的访问路径,相当于
@WebServlet(name = "DemoServlet", urlPatterns = "/demo")
public class DemoServlet 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 {
System.out.println("Hello Servlet 3.0");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
void destroy() 销毁servlet的方法
void init(ServletConfig config) 初始化servlet的方法
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
public class LifeCycleServlet extends HttpServlet {
@Override
public void init() throws ServletException {
super.init();
System.out.println("LifeCycleServlet 初始化。。。");
}
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("LifeCycleServlet 执行。。。");
}
@Override
public void destroy() {
super.destroy();
System.out.println("LifeCycleServlet 销毁。。。");
}
}
<web-app 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_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>LifeCycleServletservlet-name>
<servlet-class>LifeCycleServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>LifeCycleServletservlet-name>
<url-pattern>/lifeurl-pattern>
servlet-mapping>
web-app>
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
public class DemoGenericServlet extends GenericServlet {
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("DemoGenericServlet执行.......");
}
}
<web-app 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_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>demoGenericServletservlet-name>
<servlet-class>DemoGenericServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>demoGenericServletservlet-name>
<url-pattern>/genericurl-pattern>
servlet-mapping>
web-app>
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
public class DemoGenericServlet extends GenericServlet {
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("DemoGenericServlet执行.......");
}
}
<web-app 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_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>demoHttpServletservlet-name>
<servlet-class>DemoHttpServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>demoHttpServletservlet-name>
<url-pattern>/httpurl-pattern>
servlet-mapping>
web-app>
<web-app 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_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>demoHttpServletservlet-name>
<servlet-class>DemoHttpServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>demoHttpServletservlet-name>
<url-pattern>/httpurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>demoHttpServletservlet-name>
<url-pattern>/http2url-pattern>
servlet-mapping>
web-app>
/
开始/
开始,以 *
结束/
开始,以 *.xxx
结束 xxx
代表的是后缀名,例如: <servlet-mapping>
<servlet-name>jspservlet-name>
<url-pattern>*.jspurl-pattern>
<url-pattern>*.jspxurl-pattern>
servlet-mapping>
/
时,它表示一个默认(缺省)的 Servlet。默认的 Servlet 其作用是用于处理其它的 Servlet 处理不了的请求。例如: <servlet-mapping>
<servlet-name>defaultservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<web-app 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_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>demoHttpServletservlet-name>
<servlet-class>DemoHttpServletservlet-class>
<load-on-startup>2load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>demoHttpServletservlet-name>
<url-pattern>/httpurl-pattern>
servlet-mapping>
web-app>
- 对于以上方式,只有表单提交的方式才可能有 POST 请求,其它的都是 GET 请求。
- 这种区别是浏览器完成的功能,不需要在 doGet 和 doPost 方法中体现。
- get 和 post 都是明文,都不安全,用 HTTPS 才安全。
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 DemoServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get请求访问");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post请求访问");
}
}
<web-app 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_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>demoServletservlet-name>
<servlet-class>DemoServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>demoServletservlet-name>
<url-pattern>/demourl-pattern>
servlet-mapping>
web-app>
<%--
Created by IntelliJ IDEA.
User: 80626
Date: 2020/4/13
Time: 11:17
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$title>
head>
<body>
$END$
<h1>带协议的绝对路径h1>
<a href="http://localhost:8080/webappPractice/demo">DemoServleta>
<h1>不带协议的绝对路径h1>
<a href="/webappPractice/demo">DemoServleta>
<h1>相对路径h1>
<a href="demo">DemoServleta>
body>
html>
get请求访问
。DemoServlet
原文链接:https://qwert.blog.csdn.net/article/details/105481464