Servlet基础(续集)
Servlet原理
Servlet是由Web服务器调用,Web服务器在收到浏览器请求之后,会:
Mapping问题
一个Servlet可以指定一个映射路径
hello
/hello
一个Servlet可以指定多个映射路径
hello
/hello
hello
/hello2
hello
/hello3
hello
/hello4
hello
/hello5
一个Servlet可以指定通用映射路径
hello
/hello/*
默认请求路径映射
hello
/*
自定义后缀实现请求映射,注意点,*前面不能加映射的路径
hello
*.zzc
errorservlet例子:
package com.kuang.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 ErrorServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
resp.setCharacterEncoding("UTF-8");
PrintWriter writer = resp.getWriter();
writer.print("404
");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
//在servlet请求文件中写上:
error
com.kuang.servlet.ErrorServlet
error
/*
优先级问题:指定了固有的映射路径优先级最高,如果找不到就会走默认的处理请求;
ServletContext
web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前web应用;
- 共享数据:我在这个Servlet中保存的数据,可以在另外一个servlet中拿到;
@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 = "zzc";//数据 context.setAttribute("username", username);//将一个数据保存在了ServletContext中,名字为:username、值:username System.out.println("Hello"); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //该步骤用于获取servlet01存放的context ServletContext context = this.getServletContext(); String username = (String) context.getAttribute("username"); //下面两个是用来识别中文汉字,以至于不会出现乱码 resp.setContentType("text/html"); resp.setCharacterEncoding("UTF-8"); //注意在获取context前,首先需要让servlet01放入数据,我们再访问servlet02的网络 resp.getWriter().println("名字"+username); } hello com.kuang.servlet.HelloServlet hello /hello getc com.kuang.servlet.GetServlet getc /getc测试访问结果:
- 获取初始化参数
url jdbc:mysql://localhost:3306/mybatis @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //该步骤用于获取servlet01存放的context ServletContext context = this.getServletContext(); String url = context.getInitParameter("url"); resp.getWriter().print(url); }- 请求转发
他路径是sd4,但是请求了/gp,所以就显示了gp的内容
sd4 com.kuang.servlet.ServletDome04 sd4 /sd4 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this.getServletContext(); RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");//转发的请求路径 //调用forward实现请求转发; requestDispatcher.forward(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }重定向概念:
- 读取资源文件
Properties
- 在java目录下新建properties
- 在resources目录下新建properties
发现:都被打包到同一路径下:classes,我们俗称这个路径为classpath
sd5 com.kuang.servlet.ServletDome05 sd5 /sd5 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //该步骤用于获取所需东西,然后把资源变成一个流 InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");//第一个/表示模块路径(当前web项目) Properties prop= new Properties(); //load()加载数据 prop.load(is); String user = prop.getProperty("username"); String pwd = prop.getProperty("password"); resp.getWriter().print(user+":"+pwd); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { }
- 读取资源文件
- 请求转发
- 获取初始化参数
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!










