JavaWeb复习-001

B/S结构的系统通信原理

Web系统的访问过程 第一步:打开浏览器 第二步:地址栏 第三步:输入一个合法的网址 第四步:回车 第五步:浏览器显示内容 一个Web系统的通信步骤 第一步:输入URL 第二步:域名解析器进行域名解析 第三步:浏览器在网络中搜索解析出的IP地址,直到找到该主机 第四步:确定浏览器需要的是index.html 第五步:找到index.html文件 第六步:浏览器接收来自服务器的代码 第七步:执行前端三大件的代码

servlet规范是一个什么规范

遵循servlet规范的webapp就可以放在不同的Web服务器中运行 Servlet规范包括: 规范了哪些接口 规范了哪些类 规范了一个Web应用中应该有哪些配置文件 规范了一个Web应用中配置文件的名字 规范了一个Web应用配置文件的存放路径 规范了一个Web应用配置文件的内容 ... idea创建的一个JavaWeb项目,使用的是Maven模板,为的是,方便添加依赖,和视频从0不一样 2023 idea默认不显示 add framework support,直接通过project structure添加即可 生成web文件夹,里面有WEB-INF,再里面有web.xml,新版本不生成jsp文件 创建包,实际上是创建文件夹把他标记为Sources Root,变成蓝色和src一样,在src的目录下右键可以创建package 其项目结构如下 新版本的Servlet是jakarta版本的,注意区分 生成5个方法,其中Service编写业务代码

java 复制代码
package com.demo.servlet;  
  
import jakarta.servlet.*;  
  
import java.io.IOException;  
import java.io.PrintWriter;  
import java.sql.*;  
  
public class StudentServlet 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 {  
        // 设置响应的内容类型  
        servletResponse.setContentType("text/html;charset=utf-8");  
        PrintWriter out = servletResponse.getWriter();  
        // 连接数据库  
        Connection conn = null;  
        PreparedStatement ps = null;  
        ResultSet rs = null;  
        try {  
            // 注册驱动  
            Class.forName("com.mysql.cj.jdbc.Driver");  
            // 获取连接  
            String url = "jdbc:mysql://localhost:3306/easyjob";  
            String user = "root";  
            String password = "apple1314";  
            conn = DriverManager.getConnection(url, user, password);  
            // 获取预编译数据库操作对象  
            String sql = "select question_id, title from exam_question";  
            ps = conn.prepareStatement(sql);  
            // 执行SQL  
            rs = ps.executeQuery();  
            // 处理结果集  
            while (rs.next()) {  
                String question_id = rs.getString("question_id");  
                String title = rs.getString("title");  
                out.print(question_id + "," + title + "<br>");  
            }  
        } catch (ClassNotFoundException | SQLException e) {  
            throw new RuntimeException(e);  
        } finally {  
            // 释放资源  
            if (rs != null) {  
                try {  
                    rs.close();  
                } catch (SQLException e) {  
                    throw new RuntimeException(e);  
                }  
            }  
            if (ps != null) {  
                try {  
                    ps.close();  
                } catch (SQLException e) {  
                    throw new RuntimeException(e);  
                }  
            }  
            if (conn != null) {  
                try {  
                    conn.close();  
                } catch (SQLException e) {  
                    throw new RuntimeException(e);  
                }  
            }  
        }  
    }  
  
    @Override  
    public String getServletInfo() {  
        return null;  
    }  
  
    @Override  
    public void destroy() {  
  
    }  
}

web.xml中注册StudentServlet

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>
        <servlet-name>studentServlet</servlet-name>
        <servlet-class>com.demo.servlet.StudentServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>studentServlet</servlet-name>
        <url-pattern>/servlet/student</url-pattern>
    </servlet-mapping>
</web-app>

创建一个HTML文件提供一个可访问Servlet的超链接

xml 复制代码
<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title>Title</title>  
</head>  
<body>  
    <a href="/servlet/student">student li</a>  
</body>  
</html>

引入依赖需要的jar包,pom.xml配置如下 用于是手动添加的Web模块记得添加依赖,还是Project Structure idea关联Tomcat,webApp部署到Tomcat 如果没有Artifact,就通过Project Structure进行手动创建,在手动添加的web模块位置 添加war这个artifact 调整上下文 启动 点击超链接,实现刚刚写的Servlet查询数据库的功能 以下是我的数据库的内容 完成,流程跑通

相关推荐
Apex Predator3 分钟前
windows安装maven环境
java·maven
Bug退退退12311 分钟前
RabbitMQ 工作模式
java·分布式·rabbitmq
小莫分享1 小时前
github 镜像节点
java
链上Sniper2 小时前
智能合约状态快照技术:实现 EVM 状态的快速同步与回滚
java·大数据·linux·运维·web3·区块链·智能合约
缘来是庄2 小时前
设计模式之建造者模式
java·设计模式·建造者模式
小湘西2 小时前
Apache HttpClient 的请求模型和 I/O 类型
java·http·apache
沃夫上校3 小时前
Feign调Post接口异常:Incomplete output stream
java·后端·微服务
LeeGe3 小时前
SpringAOP中@within和@annotation以及 @within和@target的区别
后端
一个平平无奇的Java小学生3 小时前
Spring Cloud Alibaba 微服务从入门到生产部署完整指南
后端
一个平平无奇的Java小学生3 小时前
Spring Cloud Alibaba 微服务实战指南
后端