赞
踩
目录
创建项目:
创建类HelloServlet:
- package com.qcby.servlet0724;
-
- 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("HelloServlet被访问了");
- }
-
- @Override
- public String getServletInfo() {
- return null;
- }
-
- @Override
- public void destroy() {
-
- }
- }

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_4_0.xsd"
- version="4.0">
-
- <!--servlet标签:给tomcat配置servlet程序-->
- <servlet>
- <!--servlet-name标签:给servlet程序起一个别名(一般是类名)-->
- <servlet-name>HelloServlet</servlet-name>
- <!--servlet-class标签:servlet程序的全类名-->
- <servlet-class>com.qcby.servlet0724.HelloServlet</servlet-class>
- </servlet>
-
- <!--servlet-mapping标签:给servlet程序配置访问地址-->
- <servlet-mapping>
- <!--servlet-name标签:告诉服务器,我当前配置的地址给哪个servlet程序使用-->
- <servlet-name>HelloServlet</servlet-name>
- <!--url-pattern标签:配置访问地址
- / 斜杠在服务器解析的时候,表示地址为:http://ip:port/工程路径
- /hello 表示地址为:http://ip:port/工程路径/hello
- -->
- <url-pattern>/hello</url-pattern>
- </servlet-mapping>
- </web-app>

项目结构:
启动:
访问/hello时:
注意:
第一、二步是在第一次访问时创建servlet程序会调用
第三步每次访问都会调用
第四步在web工程停止时调用
- package com.qcby.servlet0724;
-
- import javax.servlet.*;
- import java.io.IOException;
-
- public class HelloServlet implements Servlet {
- public HelloServlet() {
- System.out.println("1 构造器方法");
- }
-
- @Override
- public void init(ServletConfig servletConfig) throws ServletException {
- System.out.println("2 init初始化");
- }
-
- @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("3 service---HelloServlet被访问了");
- }
-
- @Override
- public String getServletInfo() {
- return null;
- }
-
- @Override
- public void destroy() {
- System.out.println("4 destroy销毁方法");
- }
- }

在webapp下创建a.html:
get请求
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <form action="http://localhost:8080/servlet0724/hello" method="get">
- <input type="submit">
- </form>
- </body>
- </html>
启动:
点击提交:
post请求
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <form action="http://localhost:8080/servlet0724/hello" method="post">
- <input type="submit">
- </form>
- </body>
- </html>
启动:
添加获取请求:
完善:
完善:
一般在实际项目开发中,都是使用继承HttpServlet类的方式去实现Servlet程序
创建HelloServlet2类:
- package com.qcby.servlet0724;
-
- 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 {
-
- /**
- * 在get请求时调用
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- System.out.println("HelloServlet2的doGet方法");
- }
-
- /**
- * 在post请求时调用
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- System.out.println("HelloServlet2的doPost方法");
- }
- }

web.xml:
a.html:
启动:
(取消勾选注解)
启动:
ServletConfig类从类名上来看,就知道是Servlet程序的配置信息类
Servlet程序和ServletConfig对象都是由tomcat负责创建,我们负责使用
Servlet程序默认是第一次访问时创建,ServletConfig是每个Servlet程序创建时就创建一个对应的ServletConfig对象
每个ServletConfig对象对应自己的Servlet程序,互不影响
作用:
1.
访问输出为:
2.
启动输出为:
3.
注意:
重写init方法一定要调用父类的super.init(config)
存数据 | 取数据 | 删除数据 | |
---|---|---|---|
Map | put() | get() | remove() |
域对象 | setAttribute() | getAttribute() | removeAttribute() |
1.
创建ContextServlet类
配置:
启动输出为:
2.
启动输出为:
3.
启动输出为:
1-3完整代码:
- package com.qcby.servlet0724;
-
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.io.IOException;
-
- public class ContextServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //获取web.xml中配置的上下文参数context-param
- ServletContext context = getServletConfig().getServletContext();
- String username = context.getInitParameter("username");
- System.out.println("context-param参数username的值是:"+username);
- String password = context.getInitParameter("password");
- System.out.println("context-param参数password的值是:"+password);
- //获取当前的工程路径,格式:/工程路径
- System.out.println("当前工程路径为:"+context.getContextPath());
- //获取工程部署后在服务器硬盘上的绝对路径
- /**
- * / 斜杠被服务器解析地址为:http://ip:port/工程名/ 映射到IDEA代码的webapp目录
- */
- System.out.println("工程部署的路径是:"+context.getRealPath("/"));
- }
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
-
-
- }
- }

4.
创建ContextServlet1
配置:
启动输出为:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。