Servlet Response Resquest

Servlet Response Resquest,第1张

文章目录
  • Servlet 简介
    • HelloServlet
    • Maven 环境优化
  • Servlet实现原理
  • ServletContext
    • 共享数据
  • response验证码实现
  • 简单实现重定向
  • HttpServletRequest 获取参数,请求转发

Servlet 简介

Servlet 就是 Sun 公司开发动态 Web 的一门技术

Sun 在这些 API (Application Programming Interface,应用程序接口)中提供一个接口叫做:Servlet,如果你想开发一个Servlet程序,只需要完成两个小步骤:

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

HelloServlet

Serlvet 接口 Sun 公司有两个默认的实现类:HttpServlet,GenericServlet

构建一个普通的Maven项目,删掉里面的src目录,以后我们的学习就在这个项目里面建立Moudel;这个空的工程就是Maven主工程(建一个WebApp Maven项目,勾选模板);

关于Maven父子工程的理解:

父项目中会有:

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

子项目中会有:

<parent>
        <artifactId>javaweb-02-mavenartifactId>
        <groupId>com.mavengroupId>
        <version>1.0-SNAPSHOTversion>
parent>

父项目中的 Java 子项目可以直接使用类似于继承“is-a”

Maven 环境优化

修改 web.xml 为最新的


<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"
         metadata-complete="true">
  
web-app>

将 maven 的结构搭建完整
建立java和resource两个目录
创建一个class类

package com.maven.servlet;

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;

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //ServletOutputStream outputStream = resp.getOutputStream();
        PrintWriter writer = resp.getWriter();//响应流
        writer.println("Hello,Servlet");
    }

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

在web.xml中加入

 <servlet>
        <servlet-name>helloServletservlet-name>
        <servlet-class>com.maven.servlet.HelloServletservlet-class>
    servlet>
    <servlet-mapping>
        <servlet-name>helloServletservlet-name>
        <url-pattern>/hellourl-pattern>
    servlet-mapping>

实现原理

配置Tomacat,之前讲过了
对了,还有别忘记父类的pom.xml配置jar包

 <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>4.0.1version>
        dependency>

之后测试localhost:8080/s1/hello

Servlet实现原理


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

    <servlet-mapping>
        <servlet-name>helloservlet-name>
        <url-pattern>/hellourl-pattern>
    servlet-mapping>

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

    <servlet-mapping>
        <servlet-name>helloservlet-name>
        <url-pattern>/hellourl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>helloservlet-name>
        <url-pattern>/hello2url-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>helloservlet-name>
        <url-pattern>/hello3url-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>helloservlet-name>
        <url-pattern>/hello4url-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>helloservlet-name>
        <url-pattern>/hello5url-pattern>
    servlet-mapping>

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

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

4.默认请求路径

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

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


<servlet-mapping>
    <servlet-name>helloservlet-name>
    <url-pattern>*.qinjiangurl-pattern>
servlet-mapping>

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


    <servlet>
        <servlet-name>errorServletservlet-name>
        <servlet-class>com.maven.servlet.ErrorServletservlet-class>
    servlet>
    <servlet-mapping>
        <servlet-name>errorServletservlet-name>
        <url-pattern>/*url-pattern>
    servlet-mapping>
ServletContext

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

共享数据

在一个 servlet 中保存的数据,可以在另一个 servlet 中拿到;
设置值

package com.maven.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;

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 = "小白";  //数据
        //将一个数据保存在了ServletContext中,名字为:username 。值 username
        context.setAttribute("username",username);
    }

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

传值也叫获取值

package com.maven.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;

public class GetServletContext 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().println("名字"+username);
    }

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

web.xml配置


<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"
         metadata-complete="true">
  <servlet>
    <servlet-name>helloServletservlet-name>
    <servlet-class>com.maven.servlet.HelloServletservlet-class>
  servlet>
  <servlet-mapping>
    <servlet-name>helloServletservlet-name>
    <url-pattern>/hellourl-pattern>
  servlet-mapping>

  <servlet>
    <servlet-name>getServletContextservlet-name>
    <servlet-class>com.maven.servlet.GetServletContextservlet-class>
  servlet>
  <servlet-mapping>
    <servlet-name>getServletContextservlet-name>
    <url-pattern>/getCurl-pattern>
  servlet-mapping>

web-app>

先访问/getC
发现名字为null

在访问/hello
发现没有变化
这是因为这已经将值设置好了

再次访问/getC
发现输出“名字小白”是因为已经获取到了hello里的小白然后显示

response验证码实现
package com.maven.response;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

public class ImgServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //如何让浏览器3秒自动刷新一次
        resp.setHeader("refresh","3");
        //在内存中创建图片
        BufferedImage bufferedImage = new BufferedImage(80,20,BufferedImage.TYPE_INT_RGB);
        //得到图片
        Graphics2D g = (Graphics2D) bufferedImage.getGraphics();//笔
        //设置图片背景图片
        g.setColor(Color.white);
        g.fillRect(0,0,80,20);
        //给图片写数据
        g.setColor(Color.blue);
        g.setFont(new Font(null,Font.BOLD,20));
        g.drawString(makeNum(),0,20);
        //告诉浏览器,这个请求用图片的方式打开
        resp.setContentType("image/jpeg");
        //网站存在缓存,不让浏览器缓存
        resp.setDateHeader("expires",-1);
        resp.setHeader("Cache-Control","no-cache");
        resp.setHeader("Pragma","no-cache");
        //把图片写给浏览器
        ImageIO.write(bufferedImage,"jpg",resp.getOutputStream());

    }
    //生成随机数
    private String makeNum(){
        Random random = new Random();
        String num = random.nextInt(9999999)+"";
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 7-num.length() ; i++) {
            sb.append("0");
        }
        num = sb.toString() + num;
        return num;
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
  <servlet>
    <servlet-name>imgServletservlet-name>
    <servlet-class>com.maven.response.ImgServletservlet-class>
  servlet>
  <servlet-mapping>
    <servlet-name>imgServletservlet-name>
    <url-pattern>/imgurl-pattern>
  servlet-mapping>
简单实现重定向
package com.maven.response;

import com.maven.servlet.HelloServlet;

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

public class TestRedirect extends HelloServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理请求
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username+":"+password);
        //重定向时候一定要注意,路径问题,否则404;
        resp.sendRedirect("/s2/success.jsp");
    }

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

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!h2>
<%--这里提交的路径,需要寻找到项目的路径--%>
<%--${pageContext.request.contextPath}代表当前的项目--%>

<form action="${pageContext.request.contextPath}/login" method="get">
    用户名:<input type="text" name="username"> <br/>
    密码:<input type="password" name="password"> <br/>
    <input type="submit" name="提交">
form>

body>
html>

创建一个success.jsp

<%--
  Created by IntelliJ IDEA.
  User: 74485
  Date: 2022/5/4
  Time: 15:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Titletitle>
head>
<body>
<h1>成功h1>
body>
html>
HttpServletRequest 获取参数,请求转发

HttpServletRequest 代表客户端的请求,用户通过 HTTP 协议访问服务器,HTTP 请求中的所有信息会被封装到 HttpServletRequest ,通过这个HttpServletRequest 的方法,获得客户端的所有信息;

package com.maven.request;

import com.maven.servlet.HelloServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class TestRequest extends HelloServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String[] hobbies = req.getParameterValues("hobby");
        System.out.println("=======================");
        System.out.println(username);
        System.out.println(password);
        System.out.println(Arrays.toString(hobbies));
        System.out.println("=======================");
        req.getRequestDispatcher("/login.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!h2>
<%--这里提交的路径,需要寻找到项目的路径--%>
<%--${pageContext.request.contextPath}代表当前的项目--%>

<form action="${pageContext.request.contextPath}/login" method="get">
    用户名:<input type="text" name="username"> <br/>
    密码:<input type="password" name="password"> <br/>
    爱好:
    <input type="checkbox" name="hobby" value="女孩">女孩
    <input type="checkbox" name="hobby" value="代码">代码
    <input type="checkbox" name="hobby" value="唱歌">唱歌
    <input type="checkbox" name="hobby" value="跳舞">跳舞<br/>
    <input type="submit" name="提交">
form>

body>
html>
<%--
  Created by IntelliJ IDEA.
  User: 74485
  Date: 2022/5/4
  Time: 16:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Titletitle>
head>
<body>
<h1>成功h1>
body>
html>

面试题:重定向与转发的区别?

相同点:
页面都会实现跳转

不同点:
请求转发的时候,URL 不会发生变化
重定向时候,URL 地址栏会发生变化;

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/800044.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存