Web服务器项目中,如何区分静态与动态请求处理方法?

2026-05-24 11:141阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计802个文字,预计阅读时间需要4分钟。

Web服务器项目中,如何区分静态与动态请求处理方法?

注意:完整项目下载+处理了核心功能后,我们会发现有些请求并非都是静态的,那么我们需要实现动态请求的处理。以下是我们请求的解决方案,我们只需在Ht中实现即可。

注:完整项目下载

在处理了核心任务之后,我们会发现有些请求并不是都是静态的,那么我们就需要进行实现处理动态请求的要求,如下面代码是我们请求的解决方式,我们只需在HttpRequestImpl实现类中,将如下代码实现具体的判断过程

//判断当前请求的否是静态资源 public boolean isStaticResource(){ return true; } //判断当前请求的否是动态资源 public boolean isDynamicResource(){ return true; }

1、实现isStaticResource()方法:

// 判断当前请求的否是静态资源 public boolean isStaticResource() { // 存在??文件??静态?? System.out.println("进入isStaticResource()方法"); if (requestPath != null) { String parent = ConfigUtils.getConfigValue("rootPath"); File file = new File(parent, requestPath); return file.exists() && file.isFile(); } return false; }

2、实现isDynamicResource():

// 判断当前请求的否是动态资源 public boolean isDynamicResource() { // 存在??文件??动态?? System.out.println("进入isDynamicResource()方法"); String path = ServletMappingUtils.getMappingValue(requestPath); /* * //使用路径判断,当时我不知道还有一个叫做isContains(key)的方法,如果知道的话 就可以使用了,请参见测试类 * if(isContainers(requestPath())) { return *; } */ System.out.println("ServletMapping中根据requestPath获取的映射:" + path); if (path != null) { return true; } return false; }

在动态方法中,我们String path = ServletMappingUtils.getMappingValue(requestPath);来获取请求的资源的路径,如果存在值的话就返回结果,其实现如下所示,对于servlet_mapping.properties文件,还是复制到项目下即可:

Web服务器项目中,如何区分静态与动态请求处理方法?

package com.sample.utils; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ServletMappingUtils { private static Properties p; static { InputStream in=null; p=new Properties(); try { //读了xx.properties文件 in=ServletMappingUtils.class.getResourceAsStream("servlet_mapping.properties"); //放置到p中,即放properties文件中的key,value p.load(in); } catch (IOException e) { e.printStackTrace(); } finally { if(in!=null) try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } public static String getMappingValue(String mapping) { return p.getProperty(mapping); } public static boolean isContainsKey(String key) { return p.containsKey(key); } public static void main(String[] args) {//输出测试 // Properties p=new Properties(); // p.setProperty("rootPath","ddd"); // System.out.println(p.get("rootPath")); System.out.println(getMappingValue("/HelloWorld")); System.out.println(isContainsKey("/Login")); } }

3、实现对静态请求和动态请求的封装

在处理完基本的功能性要求之后,我们实现对核心任务取得封装,在封装时,我们仍然采用类实现接口的方式,首先我们需要明确该确定一个怎么样的接口,其代码如下:

package com.sample.127.0.0.1:10002/HelloWorld?id=1212&name=suguniang信息回车时能够看到如下所示界面,但是此时的状态是在项目文件夹webapps中根本不存在HelloWorld页面,但是我们能够正常访问到页面信息,而此时返回的信息正是我们的Servlet实现类中的内容:


而当我们在URL中输入不存在的请求资源时,则会弹出404界面

到此这篇关于浅谈web服务器项目中静态请求和动态请求处理的文章就介绍到这了,更多相关web服务器中静态请求和动态请求内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计802个文字,预计阅读时间需要4分钟。

Web服务器项目中,如何区分静态与动态请求处理方法?

注意:完整项目下载+处理了核心功能后,我们会发现有些请求并非都是静态的,那么我们需要实现动态请求的处理。以下是我们请求的解决方案,我们只需在Ht中实现即可。

注:完整项目下载

在处理了核心任务之后,我们会发现有些请求并不是都是静态的,那么我们就需要进行实现处理动态请求的要求,如下面代码是我们请求的解决方式,我们只需在HttpRequestImpl实现类中,将如下代码实现具体的判断过程

//判断当前请求的否是静态资源 public boolean isStaticResource(){ return true; } //判断当前请求的否是动态资源 public boolean isDynamicResource(){ return true; }

1、实现isStaticResource()方法:

// 判断当前请求的否是静态资源 public boolean isStaticResource() { // 存在??文件??静态?? System.out.println("进入isStaticResource()方法"); if (requestPath != null) { String parent = ConfigUtils.getConfigValue("rootPath"); File file = new File(parent, requestPath); return file.exists() && file.isFile(); } return false; }

2、实现isDynamicResource():

// 判断当前请求的否是动态资源 public boolean isDynamicResource() { // 存在??文件??动态?? System.out.println("进入isDynamicResource()方法"); String path = ServletMappingUtils.getMappingValue(requestPath); /* * //使用路径判断,当时我不知道还有一个叫做isContains(key)的方法,如果知道的话 就可以使用了,请参见测试类 * if(isContainers(requestPath())) { return *; } */ System.out.println("ServletMapping中根据requestPath获取的映射:" + path); if (path != null) { return true; } return false; }

在动态方法中,我们String path = ServletMappingUtils.getMappingValue(requestPath);来获取请求的资源的路径,如果存在值的话就返回结果,其实现如下所示,对于servlet_mapping.properties文件,还是复制到项目下即可:

Web服务器项目中,如何区分静态与动态请求处理方法?

package com.sample.utils; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ServletMappingUtils { private static Properties p; static { InputStream in=null; p=new Properties(); try { //读了xx.properties文件 in=ServletMappingUtils.class.getResourceAsStream("servlet_mapping.properties"); //放置到p中,即放properties文件中的key,value p.load(in); } catch (IOException e) { e.printStackTrace(); } finally { if(in!=null) try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } public static String getMappingValue(String mapping) { return p.getProperty(mapping); } public static boolean isContainsKey(String key) { return p.containsKey(key); } public static void main(String[] args) {//输出测试 // Properties p=new Properties(); // p.setProperty("rootPath","ddd"); // System.out.println(p.get("rootPath")); System.out.println(getMappingValue("/HelloWorld")); System.out.println(isContainsKey("/Login")); } }

3、实现对静态请求和动态请求的封装

在处理完基本的功能性要求之后,我们实现对核心任务取得封装,在封装时,我们仍然采用类实现接口的方式,首先我们需要明确该确定一个怎么样的接口,其代码如下:

package com.sample.127.0.0.1:10002/HelloWorld?id=1212&name=suguniang信息回车时能够看到如下所示界面,但是此时的状态是在项目文件夹webapps中根本不存在HelloWorld页面,但是我们能够正常访问到页面信息,而此时返回的信息正是我们的Servlet实现类中的内容:


而当我们在URL中输入不存在的请求资源时,则会弹出404界面

到此这篇关于浅谈web服务器项目中静态请求和动态请求处理的文章就介绍到这了,更多相关web服务器中静态请求和动态请求内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!