首页 > 基础资料 博客日记

[javaweb]javaweb中servlet的使用详解

2023-07-30 18:00:43基础资料围观328

本篇文章分享[javaweb]javaweb中servlet的使用详解,对你有帮助的话记得收藏一下,看Java资料网收获更多编程知识

servlet

sun公司提供两种实现类:

1.实现servlet接口

2.继承Httpservlet

  • sun公司用来开发动态web的一门技术
  • sun在这些API中提供了一个接口叫servlet,如果你想开发一个servlet程序,只需要完成两个小步骤。
    • 编写一个类,实现servlet类接口
    • 把开发好的java类部署到web服务器中

把实现了servlet接口的程序叫做servlet

创建一个helloservlet

导入maven依赖:

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>

  <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>

  </dependency>

  <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.2-b01</version>
  </dependency>
</dependencies>

敲一个servlet

package com.javaweb;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author panglili
 * @create 2022-07-19-8:18
 */
public class HelloServlet extends HttpServlet {
    //由于get和post只是请求的方式不一样,因此只需要实现一个,在另一个里面调用
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("******doget");
        PrintWriter writer = resp.getWriter();
        writer.print("hello,servlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

注册在web上

<servlet>
  <servlet-name>hello</servlet-name>
  <servlet-class>com.javaweb.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>hello</servlet-name>
  <url-pattern>/add</url-pattern>
</servlet-mapping>

6.servletContext

web容器在启动的时候,会为每个web程序都创建一个servletcontext对象,代表当前web应用。全局唯一!

  • 可以实现各个servlet之间的通信,统一的管理所有servlet的数据(一般不用此方法,增加浏览器负担)。
  • servletcontext:调用servlet实现数据共享

测试一下:

(1)创建一个servlet使用context设置属性

package com.javaweb.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author panglili
 * @create 2022-07-19-8:18
 */
public class HelloServlet extends HttpServlet {
    //由于get和post只是请求的方式不一样,因此只需要实现一个,在另一个里面调用
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        ServletContext context = this.getServletContext();
        String username="ssss";
        context.setAttribute("username",username);
        System.out.println("******doget");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

(2)创建另一个servlet使用context获取属性值

package com.javaweb.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author panglili
 * @create 2022-07-19-16:28
 */
public class getServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();

        String username =(String) context.getAttribute("username");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        resp.getWriter().print(username);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

(3)然后配置servlet在web中就可以在网页访问了

<servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.javaweb.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>getc</servlet-name>
    <servlet-class>com.javaweb.servlet.getServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>getc</servlet-name>
    <url-pattern>/getc</url-pattern>
</servlet-mapping>

(4)在网页请求getc可以拿到hello的属性值

  • servletcontext获取web初始化参数

(1)web初始化数据

<context-param>
    <param-name>url</param-name>
    <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>

(2)servlet中获取

package com.javaweb.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author panglili
 * @create 2022-07-19-17:02
 */
public class ServletDemo extends HttpServlet {
    //由于get和post只是请求的方式不一样,因此只需要实现一个,在另一个里面调用
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        String url = context.getInitParameter("url");
        resp.getWriter().print(url);

        System.out.println("******doget");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

(3)网页数据

  • servletcontext:实现页面请求转发:

(1)写请求转发的servlet

此请求被转发到getIni

package com.javaweb.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author panglili
 * @create 2022-07-19-17:02
 */
public class ServletDemo2 extends HttpServlet {
    //由于get和post只是请求的方式不一样,因此只需要实现一个,在另一个里面调用
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        context.getRequestDispatcher("/getIni").forward(req,resp);

        System.out.println("******doget");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

(2)web配置

请求demo2

<servlet>
    <servlet-name>demo2</servlet-name>
    <servlet-class>com.javaweb.servlet.ServletDemo2</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>demo2</servlet-name>
    <url-pattern>/demo2</url-pattern>
</servlet-mapping>

(3)网页实现

返回的是getIni数据


文章来源:https://www.cnblogs.com/lumanmanqixiuyuanxi/p/16512657.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐

标签云