Servlet学习笔记

本文详细介绍了JavaServlet中的映射路径配置、ServletContext的使用、如何获取和设置参数、以及重定向与请求转发的区别。包括了servlet的注解配置、ServletConfig和ServletContext的作用,以及实际操作示例和原理分析。
摘要由CSDN通过智能技术生成

package Servlet02;

import java.io.IOException;

import java.io.PrintWriter;

import jakarta.servlet.ServletException;

import jakarta.servlet.annotation.WebServlet;

import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpServletResponse;

@WebServlet(“/demo01”)

public class ServlertDemo01 extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

resp.setContentType(“text/html;charset=UTF-8”);

PrintWriter out=resp.getWriter();

out.print(“hello 注解的方式配置servlet”);

在这里插入图片描述

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

// TODO Auto-generated method stub

super.doPost(req, resp);

}

}

结果图

在这里插入图片描述

1.5 Mapping问题


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

hello

/hello

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

hello

/hello

hello

/hello1

hello

/hello2

hello

/hello3

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

hello

/hello/*

  1. 默认请求路径

hello

/*

  1. 指定一些后缀或前缀等等…

hello

*.do

  1. 优先级问题

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

1.6 ServletContext


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

1.6.1 共享数据

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

保存数据的servlet

package com.yan.study;

import jakarta.servlet.ServletContext;

import jakarta.servlet.ServletException;

import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.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 uname=“小言”;

context.setAttribute(“username”,uname);//将一个数据保存在ServletContext中,名字为:username,值为uname

}

}

获取数据的servlet(GetServlet)

package com.yan.study;

import jakarta.servlet.ServletContext;

import jakarta.servlet.ServletException;

import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

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);

}

}

配置servlet的XML

Archetype Created Web Application

hello

com.yan.study.HelloServlet

hello

/hello

get

com.yan.study.GetServlet

get

/get

测试结果图

在这里插入图片描述

1.6.2 获取初始化参数

配置XML

Archetype Created Web Application

url

jdbc:mysql://localhost:3306/mybatis

gp

study.ServletDemo03

gp

/gp

ServletDemo03

package study;

import jakarta.servlet.ServletContext;

import jakarta.servlet.ServletException;

import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

public class ServletDemo03 extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

ServletContext context=this.getServletContext();

String url=context.getInitParameter(“url”);

resp.getWriter().print(url);

}

}

测试结果

在这里插入图片描述

1.6.3 请求转发

  • 写一个ServletDemo04转发到ServletDemo03里面

ServletDemo04

package study;

import jakarta.servlet.RequestDispatcher;

import jakarta.servlet.ServletContext;

import jakarta.servlet.ServletException;

import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

public class ServletDemo04 extends HttpServlet {

@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实现请求转发

}

}

配置xml

Archetype Created Web Application

url

jdbc:mysql://localhost:3306/mybatis

gp

study.ServletDemo03

gp

/gp

demo04

study.ServletDemo04

demo04

/demo04

测试结果

在这里插入图片描述

1.7 HttpServletResponse


web服务器接收到客户端的http请求,针对这个请求,分别创建一个代表请求的HttpServletRequest对象,代表响应一个HttpServletResponse;

  • 如果要获取客户端请求过来的参数:找HttpServletRequest

  • 如果要给客户端响应一些信息:找HttpServletResponse

重定向 [resp.sendRedirect();]

  • 在访问RedirectServlet时,重定向到GetServlet

RedirectServlet

package com.yan.study;

import jakarta.servlet.ServletException;

import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

public class RedirectServlet extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

resp.sendRedirect(“/servlet01_war/get”);

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

}

}

配置XML

red

com.yan.study.RedirectServlet

red

/red

结果图展示

在这里插入图片描述

重定向和转发的区别?

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

不同点:

请求转发的时候,url不会发生变化

重定向的时候,url地址会发生变化

1.8 HttpServletRequest


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

1.8.1 获取参数,请求转发

  • RequestDemo01一个servlet,从form.jsp的表单中获取信息,重定向到success.jsp中

form.jsp

<%–<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>–%>

<%@ page contentType=“text/html;charset=UTF-8” language=“java” isELIgnored=“false”%>

Title

<%–${pageContext.request.contextPath}代表当前项目–%>

用户名:

密码:

success.jsp

<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>

Title

success

ResquestDemo01

package com.yan.study;

import jakarta.servlet.ServletException;

import jakarta.servlet.annotation.WebServlet;

import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet(urlPatterns = “/demo01”)

public class RequestDemo01 extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

String uname=req.getParameter(“username”);

String psw=req.getParameter(“password”);

System.out.println(uname+“;”+psw);

resp.sendRedirect(“/test01_war/success.jsp”);

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

super.doPost(req, resp);

}

}

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
OException {

String uname=req.getParameter(“username”);

String psw=req.getParameter(“password”);

System.out.println(uname+“;”+psw);

resp.sendRedirect(“/test01_war/success.jsp”);

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

super.doPost(req, resp);

}

}

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-sFJqn2pU-1714952592629)]

[外链图片转存中…(img-QkYJ8Yvt-1714952592630)]

[外链图片转存中…(img-Qj4v02Xt-1714952592630)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 7
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值