8-19HTTP协议+servlet

接下来几天我们将根据老师的讲解做出一个小型的例子,来深入了解JAVAWEB的相关内容,最后的完整项目会上传。

Request+Response
  1. 请求行
  2. 请求头部
  3. 空行
  4. 请求数据
Servlet
  1. DNS域名解析
  2. 建立连接(TCP/IP三次握手)
    在这里插入图片描述
    在这里插入图片描述
  3. demo
package com.pactrea.servlet;

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

public class IndexServlet extends HttpServlet {
    @Override
    public void init(){

    }
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response){
        try {
            response.sendRedirect("jsp/index.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response){
        doGet(request,response);
    }
    @Override
    public void destroy(){

    }

}

JavaWebDemo
在这里插入图片描述

package com.pactrea.servlet;

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 IndexServlet extends HttpServlet {
    @Override
    public void init() {

    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {

        request.setAttribute("name", "张三");

        // 重定向 不带参数
//        try {
//            response.sendRedirect("jsp/index.jsp");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        // 转发 带着信息转发
        try {
            request.getRequestDispatcher("jsp/index.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        doGet(request, response);
    }

    @Override
    public void destroy() {

    }

}

<?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">
    <welcome-file-list>
        <welcome-file>/jsp/index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>haha</servlet-name>
        <servlet-class>com.pactrea.servlet.IndexServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>haha</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>
</web-app>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/8/19
  Time: 13:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>jsp</title>
</head>
<body>
jsp包的index
<%=request.getAttribute("name")%>
${name}
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值