jsp内置对象====resposne对象

response对象的主要作用是对客户端的请求进行回应,

response对象的几个常用方法:

public void add(Cookie cookie);                          向客户端增加Cookie;

public void setHeader("String name ","String value");    设置头信息

public void sendRedirect(String location) throws IOException();  页面跳转,重定向


1设置头信息:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://www.mldn.cn/jstl/core"%>
<html>
    <head></head>
    <body>
<%!    //定义全局变量,注意前面的!号,如果没有!号 ,则定义的是局部变量
            int count = 0 ;
%>
<%
            response.setHeader("refresh","2");//页面2秒一刷新
%>
        <h1>您已经访问了<%=count++%>次!</h1>
    </body>
</html>

2页面的跳转:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
    <head></head>
    <body>
    <h1>3秒后跳转到hello.htm页面!如果没有跳转请按<a href="hello.htm">这里</a>!</h1>
<%
            response.setHeader("refresh","3;url=hello.htm");//3秒后跳转到hello.htm页面
%>
    </body>
</html>

<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
    <head></head>
    <META HTTP-EQUIV="refresh"  CONTENT="3;URL=hello.htm">
    <body>
        <h1>3秒后跳转到hello.htm页面!如果没有跳转请按<a href="hello.htm">这里</a>!</h1>
    </body>
</html>

3.response页面跳转

在response对象中提供了一个叫做sendRedirect()的方法,完成页面的跳转

例子:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
    <head></head>
    <body>    
<%
            response.sendRedirect("hello.htm");
%>
    </body>
</html>

此时页面已经跳转成功,并且地址栏改变了。这种跳转肯定属于客户端跳转,客户端跳转是无法传递 request属性范围内容的。

问题:那么<jsp:forward>和response.sendRedirect()这两种跳转的区别是什么呢?    setHeader也算一种跳转!

===如果是<jsp:forward>跳转的话,有如下特点:

           .服务器端跳转,跳转之后地址栏不改变,可以传递request的属性。

            .而且是属于无条件的跳转,跳转之前的语句会执行,跳转之后的就不会执行了,如果在jsp中使用jdbc的话,一定要在 跳转之前进行数据库的关闭,否则永远就关闭不了了。

===如果是response.sendRedirect()跳转:

         .属于客户端跳转,跳转之后地址栏发生改变,不可以传递request的属性

          .是在所有的语句都执行完后,才进行的跳转!


<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
    <head></head>
    <body>
    <%  System.out.println("===========forward跳转之前============") ;  %>
    <jsp:forward page="hello.htm"/>    
    <%  System.out.println("===========forward跳转之后============") ;  %>
    </body>
</html



<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
    <head></head>
    <body>
    <%  System.out.println("===========forward跳转之前============") ;  %>
    <%response.sendRedirect("hello.htm");%>
    <%  System.out.println("===========forward跳转之后============") ;  %>
    </body>
</html>

4.操作cookie:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
    <head></head>
    <body>
<%//本程序表示在客户端上设置两个Cookie对象
    Cookie  c1 = new Cookie("liuhui1","刘辉1");
    Cookie  c2 = new Cookie("liuhui2","刘辉2");
    response.addCookie(c1);
    response.addCookie(c2);
%>
    </body>
</html>

本程序表示在客户端上设置两个cookie,既然可以设置了,也可以用request对象来取出(必须用request对象,因为客户端在向服务器端发送请求的时候,会发送一个cookie头信息,所有的请求内容都封装为一个request对象当中);

<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
    <head></head>
    <body>
<%
        Cookie c[] = request.getCookies();//获取cookie
        for(int x=0;x<c.length;x++){
%>    
            <h1><%=c[x].getName()%>-------><%=c[x].getValue()%></h1>
<%
        }    
%>
    </body>
</html>

由于cookie没有设置保存的时间,所以默认是在一个浏览器上保存的, 如果此浏览器关闭,则Cookie消失,如果想要cookie真正保存在客户端上一段时间,则要用Cookie类提供的的方法:setMaxAge()方法。

response对象主要保存的就是服务器端对客户端的回应。cookie本身是存在安全隐患的,如果要存放客户端信息的时候用cookie就行了。



import os from typing import List import jieba from dotenv import load_dotenv, find_dotenv from llama_index.core import ( StorageContext, QueryBundle, VectorStoreIndex, ) from llama_index.llms.ollama import Ollama from llama_index.retrievers.bm25 import BM25Retriever from llama_index.core.query_engine import RetrieverQueryEngine from llama_index.core.postprocessor import SentenceTransformerRerank from llama_index.core.node_parser import SentenceSplitter from pipeline import MarkdownHeaderTextSplitter class AdvancedRAG: def __init__(self, file_path): # 加载环境变量 _ = load_dotenv(find_dotenv()) # 加载文档路径 self.file_path = file_path # 初始化全局变量 self.nodes = [] self.retriever = None self.reranker = None self.query_engine = None # 创建节点 self.nodes = self.create_nodes() # 启动系统 self.bootstrap() # 初始化嵌入模型(从本地路径加载) self.embed_model = HuggingFaceEmbedding(model_name="本地模型路径", tokenizer_name="本地模型路径") def create_nodes(self): """ 接收pipline的节点数据 :return: 返回节点列表 """ try: # 创建节点 splitter_with_limit = MarkdownHeaderTextSplitter(self.file_path, is_separator_regex=True) chunks_with_limit = splitter_with_limit.split_text() nodes = splitter_with_limit.chunk_to_TextNode(chunks_with_limit) # 验证节点 if not nodes: print("警告:未创建任何节点") return [] if not isinstance(nodes, list): print("警告:节点不是列表类型") return [] print(f"成功创建 {len(nodes)} 个节点") for node in nodes: print(f"节点标题: {node.node_id}") print(f"节点内容: {node.text}") print("="*20) return nodes except Exception as e: print(f"创建节点时出错: {str(e)}") return [] # 返回空列表而不是 None def bootstrap(self): """初始化 RAG 系统""" # 初始化本地 LLM (Ollama) llm = Ollama( model="qwen3-1.7B", # 使用本地中文模型,例如 qwen:7b base_url="http://localhost:11434", # 本地 Ollama 服务地址 request_timeout=240, temperature=0, system_prompt=( "你是一位维修性工程领域的专家,你的工作是回答相关问题。" "所有问题都与维修相关。" "保持回答的专业性和基于事实 - 不要编造信息。" "如果没有检索到请回答‘未查到相关信息’" ), additional_kwargs={ "num_ctx": 8192, # 设置模型上下文大小 "num_gpu": 1, # 使用GPU加速 }, ) # 创建节点索引 index = VectorStoreIndex(self.nodes) # 创建 BM25 检索器(优化中文) self.retriever = BM25Retriever.from_defaults( similarity_top_k=10, index=index, tokenizer=self.chinese_tokenizer # 使用中文分词器 ) # 6. 创建中文优化重新排序器 # self.reranker = SentenceTransformerRerank( # top_n=5, # 保留更多结果供LLM参考 # model="BAAI/bge-reranker-large-zh" # 中文优化模型 # ) # 7. 创建查询引擎 self.query_engine = RetrieverQueryEngine.from_args( retriever=self.retriever, llm=llm, # node_postprocessors=[self.reranker] ) # 自定义分词函数,使用jieba def jieba_tokenizer(text: str) -> list: return list(jieba.cut(text)) def query(self, query_str: str): """执行查询并返回结果""" # 创建查询包 query_bundle = QueryBundle(query_str=query_str) # 第一步:检索相关节点 nodes = self.retriever.retrieve(query_bundle) # 第二步:重新排序节点 # reranked_nodes = self.reranker.postprocess_nodes( # nodes, # query_bundle=query_bundle # ) # 打印调试信息 print(f"初始检索: {len(nodes)} 个节点") # print(f"重新排序后: {len(reranked_nodes)} 个节点") # 使用查询引擎获取最终响应 response = self.query_engine.query(query_str) return response if __name__ == "__main__": resposne = AdvancedRAG(r"D:\minerU\output\维修手册\auto\维修手册.md").query("冷却系统维修统计表1-5月份维修次数") print(resposne)请根据这段代码进行改编,将bm25检索替换为混合检索,同时设置全局嵌入模型通过setting方法设置
最新发布
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值