2022/08/30 吉软 JavaWeb(2)

学习目录

1、Servlet

1.1、Servlet

1.2、HelloServlet

1.3、Servlet原理

1.4、Mapping问题

        1.一个Servlet可以指定一个映射路径

        2.一个servlet可以指定多个映射路径

        3.一个servlet可以指定通用映射路径

        4.默认请求路径

        5.指定一些后缀或者前缀等等…

        6.优先级问题

1.5、ServletContext

1、共享数据

2、获取初始化参数

3、请求转发


1、Servlet

1.1、Servlet

Servlet就是sun公司开发动态web的一门技术

Sun在这些APi中提供一个接口叫做:Servlet,如果你想开发一个Servlet程序,只需要完成两个小步骤:

  • 编写一个类,实现Serlet接口 把开发好java类部署到web服务器中。 
  • 把实现了Servlet接口的Java程序叫做,Servlet

1.2、HelloServlet

 1. 构建一个普通的Maven项目,等里面的sc目录,以后我们的学习就在这个项目里面建立Moudel;这个空的工程就题Maven主工程;

 2. 关于Maven父子工程的理解; 
         父项目中会有

<modules>
    <module>servlet-01</module>
</modules>

子项目会有

    <parent>
        <artifactId>javaweb-02-servlet</artifactId>
        <groupId>com.kuang</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

父项目中的java子项目可以直接使用

son extends father

3.Maven环境优化

  • 修改web.xml为最新的
  • 将maven的结构搭建完整

4.编写一个Servlet程序

  • 编写一个普通类
  • 实现Servlet接口,这里我们直接继承HttpServlet
     public class HelloServlet extends HttpServlet {
         
         //由于get或者post只是请求实现的不同的方式,可以相互调用,业务逻辑都一样;
         @Override
         protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
             //ServletOutputStream outputStream = resp.getOutputStream();
             PrintWriter writer = resp.getWriter(); //响应流
             writer.print("Hello,Serlvet");
         }
     
         @Override
         protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
             doGet(req, resp);
         }
     }
     

5.编写Servlet的映射

        为什么需要映射:我们写的是JAVA程序,但是要通过浏览器访问,而浏览器需要连接web服务器,所以我们需要再web服务中注册我们写的Servlet,还需给他一个浏览器能够访问的路径;

 <!--注册Servlet-->
      <servlet>
          <servlet-name>hello</servlet-name>
          <servlet-class>com.kuang.servlet.HelloServlet</servlet-class>
      </servlet>
      <!--Servlet的请求路径-->
      <servlet-mapping>
          <servlet-name>hello</servlet-name>
          <url-pattern>/hello</url-pattern>
      </servlet-mapping>
  

1.3、Servlet原理

        Servlet是由Web服务器调用,web服务器在收到浏览器请求之后,会:

 

1.4、Mapping问题

        1.一个Servlet可以指定一个映射路径

      <servlet-mapping>
          <servlet-name>hello</servlet-name>
          <url-pattern>/hello</url-pattern>
      </servlet-mapping>

        2.一个servlet可以指定多个映射路径

      <servlet-mapping>
          <servlet-name>hello</servlet-name>
          <url-pattern>/hello</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
          <servlet-name>hello</servlet-name>
          <url-pattern>/hello2</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
          <servlet-name>hello</servlet-name>
          <url-pattern>/hello3</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
          <servlet-name>hello</servlet-name>
          <url-pattern>/hello4</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
          <servlet-name>hello</servlet-name>
          <url-pattern>/hello5</url-pattern>
      </servlet-mapping>
  

        3.一个servlet可以指定通用映射路径

      <servlet-mapping>
          <servlet-name>hello</servlet-name>
          <url-pattern>/hello/*</url-pattern>
      </servlet-mapping>

        4.默认请求路径

       <!--默认请求路径-->
       <servlet-mapping>
           <servlet-name>hello</servlet-name>
           <url-pattern>/*</url-pattern>
       </servlet-mapping>

         5.指定一些后缀或者前缀等等…

  
  <!--可以自定义后缀实现请求映射
      注意点,*前面不能加项目映射的路径
      hello/sajdlkajda.qinjiang
      -->
  <servlet-mapping>
      <servlet-name>hello</servlet-name>
      <url-pattern>*.qinjiang</url-pattern>
  </servlet-mapping>

         6.优先级问题

        指定了固有的映射路径优先级最高,如果找不到就会走默认的处理请求;

  <!--404-->
  <servlet>
      <servlet-name>error</servlet-name>
      <servlet-class>com.kuang.servlet.ErrorServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>error</servlet-name>
      <url-pattern>/*</url-pattern>
  </servlet-mapping>
  

1.5、ServletContext

web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用;

1、共享数据

我在这个Servlet中保存的数据,可以在另外一个servlet中拿到;

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
        //this.getInitParameter()   初始化参数
        //this.getServletConfig()   Servlet配置
        //this.getServletContext()  Servlet上下文
        ServletContext context = this.getServletContext();

        String username = "秦疆"; //数据
        context.setAttribute("username",username); //将一个数据保存在了ServletContext中,名字为:username 。值 username

    }

}

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.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().print("名字"+username);

    }

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

    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>com.kuang.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.kuang.servlet.GetServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>getc</servlet-name>
        <url-pattern>/getc</url-pattern>
    </servlet-mapping>

2、获取初始化参数

    <!--配置一些web应用初始化参数-->
    <context-param>
        <param-name>url</param-name>
        <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
    </context-param>
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext context = this.getServletContext();
    String url = context.getInitParameter("url");
    resp.getWriter().print(url);
}

3、请求转发

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext context = this.getServletContext();
    System.out.println("进入了ServletDemo04");
    //RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp"); //转发的请求路径
    //requestDispatcher.forward(req,resp); //调用forward实现请求转发;
    context.getRequestDispatcher("/gp").forward(req,resp);
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This error message usually occurs when the JDBC driver for MySQL is not found or not properly configured in your Java application. To fix this issue, you need to make sure that you have the correct JDBC driver for MySQL installed and that the driver is included in your application's classpath. Here are some steps you can follow to resolve this issue: 1. Download the JDBC driver for MySQL from the official website: https://dev.mysql.com/downloads/connector/j/ 2. Extract the downloaded ZIP file and copy the JAR file to a folder in your project directory. 3. Add the JDBC driver JAR file to your project's classpath. You can do this by adding the following line to your project's build file (e.g. pom.xml for Maven): <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> 4. Make sure that you are using the correct JDBC URL for your MySQL database. The format should be like this: jdbc:mysql://localhost:3306/your_database_name Replace "your_database_name" with the actual name of your MySQL database. 5. Check that your MySQL database is running and accessible from your Java application. You can test this by using a MySQL client tool (e.g. MySQL Workbench) to connect to the database using the same credentials that your Java application is using. Once you have verified that the JDBC driver is installed and configured correctly, and that your MySQL database is running and accessible, you should be able to connect to the database from your Java application without any issues.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值