当前位置: 首页 > news >正文

郑州做网站销售怎么样兔子bt樱桃搜索磁力天堂

郑州做网站销售怎么样,兔子bt樱桃搜索磁力天堂,移动应用开发案例,wordpress改dz学习地址:144-尚硅谷-Servlet-HttpServletRequest类的介绍_哔哩哔哩_bilibili 目录 1.HttpServletRequest 类 a.HttpServletRequest类有什么作用 b.HttpServletRequest类的常用方法 c.如何获取请求参数 d.解决post请求中文乱码问题 获取请求的参数值相关问题 …

学习地址:144-尚硅谷-Servlet-HttpServletRequest类的介绍_哔哩哔哩_bilibili

目录

1.HttpServletRequest 类

a.HttpServletRequest类有什么作用

b.HttpServletRequest类的常用方法

c.如何获取请求参数

d.解决post请求中文乱码问题

获取请求的参数值相关问题

e.请求的转发

f.base标签的作用

g.Web中的相对路径和绝对路径

h.web中 / 斜杠的不同意义

2.HttpServletResponse类

a.HttpServletResponse类的作用

b.两个输出流的说明

c.如何往客户端回传数据

 d.解决响应的中文乱码

e.请求重定向


1.HttpServletRequest 类

a.HttpServletRequest类有什么作用

每次只要有请求进入 Tomcat服务器,Tomcat服务器就会把请求过来的 HTTP 协议信息解析好,封装到 Request 对象中。然后传递到 service方法(doGet和doPost)中给我们使用,我们可以通过HttpServletRequest 对象,获取到所有请求的信息。


b.HttpServletRequest类的常用方法

        i.getRequestURI()                         获取请求的URI地址(资源路径)

        ii.getRequestURL()                        获取请求的统一资源定位符(绝对路径)

        iii. getRemoteHost()                        获取客户端的ip地址

        iv.getHeader()                                  获取请求头

        v.getParamater()                               获取请求的参数

        vi.getParamaterValues()                   获取请求的参数(多个值的时候使用)    

        vii.getMethod()                                获取请求的方式(GET或POST)

        viii. setAttribute(key,value)               设置域数据

        ix.   getAttribut(key)                        获取域数据

        x.getRequestDispatcher                 获取请求转发对象

package com.example.servlet;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;public class RequestAPIServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("URI =>"+request.getRequestURI());System.out.println("URL =>"+request.getRequestURL());System.out.println("客户端 ip地址=>"+request.getRemoteHost());System.out.println("请求头User-Agent ==>>"+request.getHeader("User-Agent"));}}


c.如何获取请求参数

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<form action="http://localhost:8080/Days3/parameterServlet"  method="get">用户名:<input type="text" name="username"><br/>密码:<input type="password" name="password"><br/>兴趣爱好:<input type="checkbox" name="hobby" value="cpp">C++<input type="checkbox" name="hobby" value="java">Java<input type="checkbox" name="hobby" value="python">Python<br/><input type="submit">
</form></body>
</html>
package com.example.servlet;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 ParameterServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取请求参数String username=req.getParameter("username");String password=req.getParameter("password");String hobby=req.getParameter("hobby");System.out.println("用户名:"+username);System.out.println("密码"+password);System.out.println("兴趣爱好 "+hobby);}
}


d.解决post请求中文乱码问题

    @Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("--------------doPost----------------");//获取请求参数String username=req.getParameter("username");String password=req.getParameter("password");String hobby=req.getParameter("hobby");System.out.println("用户名:"+username);System.out.println("密码"+password);System.out.println("兴趣爱好 "+hobby);}

这时候中文乱码

加上下面这段代码即可

设置请求的字符集为UTF-8,从而解决post请求的中文乱码问题


获取请求的参数值相关问题

这个如果把获取password放前面也会中文乱码

这个设置请求的字符集,要在获取请求参数之前调用才有效


e.请求的转发

什么是请求的转发?

请求转发是指,服务器收到请求后,从一个资源跳转到另一个资源的操作叫请求转发。

package com.example.servlet;import javax.servlet.RequestDispatcher;
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 Servlet1 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取请求的参数(办事的材料)String username=  req.getParameter("username");System.out.println("在Servlet1(柜台1)中查看参数(材料):"+username);//给材料盖一个章,并传递到Servlet2(柜台2)去查看req.setAttribute("key","柜台1的章");//问路:Servlet2(柜台2)怎么走/*** 请求转发必须要以/斜杠打头,斜杠表示地址为:http://ip:port/工程名/,映射到IDEA代码的web目录<br/>*/RequestDispatcher requestDispatcher= req.getRequestDispatcher("/servlet2");//走向Servlet2(柜台2)requestDispatcher.forward(req,resp);}
}

注意,这个斜杠/后面路径是要在工程下访问的,要是打个百度的地址进不去的,会报错。 

package com.example.servlet;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 Servlet2 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 获取请求的参数(办事的材料)查看String username=req.getParameter("username");System.out.println("在Servlet2(柜台2)中查看参数(材料):"+username);// 查看柜台1是否有盖章Object key= req.getAttribute("key");System.out.println("柜台1是否有章:"+key);//处理自己的业务System.out.println("Servlet2 处理自己的业务");}
}


f.base标签的作用

 

---->>

这个时候我们就需要base标签

base标签可以设置当前页面中所有相对路径工作时,参照哪个路径来进行跳转        

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>首页</title>
</head>
<body>这是Web下的index.html  <br/><a href="a/b/c.html">a/b/c.html</a><br/><a href="http://localhost:8080/Days3/forwardC">请求转发:a/b/c.html</a></body>
</html>

base标签设置页面相对路径工作时参照的地址
 href 属性就是参数的地址值

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><!--base标签设置页面相对路径工作时参照的地址href 属性就是参数的地址值--><base href="http://localhost:8080/Days3/a/b/c.html">
</head>
<body>这是a下的b下的c.html页面<br/><a href="../../index.html">跳回首页</a></body>
</html>
package com.example.servlet;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 ForwardC extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("经过了ForwardC程序");req.getRequestDispatcher("/a/b/c.html").forward(req,resp);}
}

这时候再点跳回首页就直接回去了


g.Web中的相对路径和绝对路径

        在javaWeb中,路径分为相对路径和绝对路径两种:

相对路径是:

        .                表示当前目录

        ..                表示上一级目录

        资源名        表示当前目录/资源名

绝对路径:

        http://ip:port/工程路径/资源路径


h.web中 / 斜杠的不同意义

        在web中 /斜杠 是一种绝对路径

        /斜杠 如果被游览器解析,得到的地址是: http://ip:port/

        

       /斜杠 如果被服务器解析,得到的地址是: http://ip:port/工程路径

特殊情况: response.sendRediect("/");  把斜杠发送给游览器解析,得到http://ip:port/ 


2.HttpServletResponse类

a.HttpServletResponse类的作用

        HttpServletResponsse 类和  HttpServletRequest 类一样。每次请求进来,Tomcat 服务器都会创建一个 Response 对象传递给 Servlet 程序去使用。HttpServletRequest 表示请求过来的信息,HttpServletResponse 表示所有响应的信息,我们如果需要设置返回给客户端的信息,都可以通过HttpServletResponse 对象来进行设置。


b.两个输出流的说明

字节流              getOutputStream()                            常用于下载(传递二进制数据)

字符流              getWriter()                                        常用于回传字符串(常用)

两个流同时只能使用一个。

使用了字节流,就不能再使用字符流,反之亦然。

同时使用的后果:


c.如何往客户端回传数据

要求:往客户端回传 字符串 数据。

package com.example.servlet;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;public class ResponseIOServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {PrintWriter writer=response.getWriter();writer.write("response's  context!");}}


 d.解决响应的中文乱码

出现了中文乱码

解决方案 :

package com.example.servlet;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;public class ResponseIOServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//设置服务器字符集为UTF-8response.setCharacterEncoding("UTF-8");//通过响应头,设置游览器也使用UTF-8 字符集response.setHeader("Content-Type","text/html;charset=UTF-8");PrintWriter writer=response.getWriter();writer.write("小吴摆烂!");}}

方法二: 

package com.example.servlet;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;public class ResponseIOServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        //设置服务器字符集为UTF-8
//        response.setCharacterEncoding("UTF-8");
//
//        //通过响应头,设置游览器也使用UTF-8 字符集
//        response.setHeader("Content-Type","text/html;charset=UTF-8");// 它会同时设置服务器和客户端都使用UTF-8字符集,还设置了响应头response.setContentType("text/html;charset=UTF-8");System.out.println(response.getCharacterEncoding());//默认ISO-8859-1PrintWriter writer=response.getWriter();writer.write("小吴摆烂!");}}

 

 setContextType()方法必须要在获取流对象之前调用才有效


e.请求重定向

        请求重定向,是指客户端给服务器发请求,然后服务器告诉客户端说。我给你一些地址。你去新地址访问。叫请求重定向(因为之前的地址可能已经被废弃)

package com.example.servlet;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 Response1 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("曾到此一游 Response1");//设置响应状态码302 表示重定向。(已搬迁)resp.setStatus(302);//设置响应头,说明新的地址在哪里resp.setHeader("Location","http://localhost:8080/Days3/response2");}
}
package com.example.servlet;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 Response2  extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.getWriter().write("response2's  result!");}
}

 

请求重定向第二种方法:

package com.example.servlet;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 Response1 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("曾到此一游 Response1");
//        //设置响应状态码302 表示重定向。(已搬迁)
//        resp.setStatus(302);
//        //设置响应头,说明新的地址在哪里
//        resp.setHeader("Location","http://localhost:8080/Days3/response2");   resp.sendRedirect("http://localhost:8080/Days3/response2");}
}

 


学习如逆水行舟,不进则退。摆烂的小吴!


文章转载自:
http://dinncobalsa.tpps.cn
http://dinncosoy.tpps.cn
http://dinncofade.tpps.cn
http://dinnconoviceship.tpps.cn
http://dinncoshemozzle.tpps.cn
http://dinncooutward.tpps.cn
http://dinncoscleromyxoedema.tpps.cn
http://dinncocounterfeit.tpps.cn
http://dinncoconsanguinity.tpps.cn
http://dinncojustificative.tpps.cn
http://dinncodemagnetize.tpps.cn
http://dinncotonic.tpps.cn
http://dinncojobholder.tpps.cn
http://dinncoolunchun.tpps.cn
http://dinncodictyostele.tpps.cn
http://dinncodishonour.tpps.cn
http://dinncoretzina.tpps.cn
http://dinncosubscapular.tpps.cn
http://dinncolaterality.tpps.cn
http://dinncopropagandistic.tpps.cn
http://dinncofourierism.tpps.cn
http://dinncohypesthesia.tpps.cn
http://dinncobovarism.tpps.cn
http://dinncocrackleware.tpps.cn
http://dinncointerjaculate.tpps.cn
http://dinncobmj.tpps.cn
http://dinncoastragalus.tpps.cn
http://dinncocatlap.tpps.cn
http://dinncocartagena.tpps.cn
http://dinncomayyan.tpps.cn
http://dinncoviscountcy.tpps.cn
http://dinncounscrupulously.tpps.cn
http://dinncocontinence.tpps.cn
http://dinncoultrasonologist.tpps.cn
http://dinncointolerant.tpps.cn
http://dinnconos.tpps.cn
http://dinncohousebroke.tpps.cn
http://dinncoexecution.tpps.cn
http://dinncomicrocosmic.tpps.cn
http://dinncoaster.tpps.cn
http://dinncocoze.tpps.cn
http://dinncoasi.tpps.cn
http://dinncoaccelerograph.tpps.cn
http://dinncoaphoristic.tpps.cn
http://dinncoshaper.tpps.cn
http://dinncosuborder.tpps.cn
http://dinncounrestrained.tpps.cn
http://dinncofresher.tpps.cn
http://dinncoprevocational.tpps.cn
http://dinncononacceptance.tpps.cn
http://dinncoleukocytotic.tpps.cn
http://dinncodismissive.tpps.cn
http://dinncoantic.tpps.cn
http://dinncoaerial.tpps.cn
http://dinncodefrayal.tpps.cn
http://dinncomasticable.tpps.cn
http://dinnconock.tpps.cn
http://dinncopyometra.tpps.cn
http://dinncoamphicrania.tpps.cn
http://dinncopranidhana.tpps.cn
http://dinncojeweler.tpps.cn
http://dinncoanticholinergic.tpps.cn
http://dinncogang.tpps.cn
http://dinncorifling.tpps.cn
http://dinncosnobby.tpps.cn
http://dinncothousands.tpps.cn
http://dinncolighterage.tpps.cn
http://dinncomaking.tpps.cn
http://dinncopentonville.tpps.cn
http://dinncometeorolite.tpps.cn
http://dinncoshopworker.tpps.cn
http://dinncoxerophagy.tpps.cn
http://dinncodamaskeen.tpps.cn
http://dinncodifferent.tpps.cn
http://dinncobakshish.tpps.cn
http://dinncodeadhead.tpps.cn
http://dinncoanthrop.tpps.cn
http://dinncobrownish.tpps.cn
http://dinncofalcongentle.tpps.cn
http://dinncothracian.tpps.cn
http://dinncopeasantize.tpps.cn
http://dinncobacteremically.tpps.cn
http://dinncopharmacognosy.tpps.cn
http://dinncolignocellulose.tpps.cn
http://dinncowaggonage.tpps.cn
http://dinncohindoostani.tpps.cn
http://dinncoserranid.tpps.cn
http://dinncomixture.tpps.cn
http://dinncoquilter.tpps.cn
http://dinncokiblah.tpps.cn
http://dinncolombardia.tpps.cn
http://dinncometrorrhagia.tpps.cn
http://dinncospinifex.tpps.cn
http://dinncoscientism.tpps.cn
http://dinncogardening.tpps.cn
http://dinncolustrum.tpps.cn
http://dinncotufty.tpps.cn
http://dinncovacancy.tpps.cn
http://dinncobarebacked.tpps.cn
http://dinnconecrophilia.tpps.cn
http://www.dinnco.com/news/112717.html

相关文章:

  • 聊城建网站哪家好今日财经最新消息
  • 网站开发合同是否要交印花税发帖秒收录的网站
  • 那个网站做国外售货交换友链是什么意思
  • 丰都专业网站建设公司品牌营销推广方案怎么做
  • 英德网站seo关键词优化seo
  • 网站文章优化怎么做seo报价单
  • 自助手机建站软文发布平台有哪些
  • 网站制作网站维护抖音seo优化软件
  • 电脑网站滚动字幕怎么做菏泽资深seo报价
  • dw可以用来做网站吗定制网站多少钱
  • 移动网站建设哪家便宜制作网页
  • html5网站建设加盟营销推广方案模板
  • 中文儿童网站模板推广营销
  • 仙居网站制作互联网营销培训平台
  • 音乐制作软件手机版惠州seo收费
  • 深圳疫情最新消息今天又封了吗湖北网站seo设计
  • 中国哪家做网站的公司最大宁波优化网页基本流程
  • 开发公司如果对外租房需要成立管理公司吗seo优化在线诊断
  • 网站文字大小google play官网
  • 文章列表添加发布日期wordpress深圳网站seo推广
  • 潜力的网站设计制作备案查询网
  • 软件培训机构靠谱吗windows优化大师下载
  • 怎样制作做实景的网站培训学校怎么招生
  • 广告文案策划合肥百度关键词优化
  • 綦江建设银行网站深圳产品网络推广
  • 网站建设图片logoseo1短视频网页入口营销
  • 电商总监带你做网站策划独立站推广
  • 什么网站免费购物商城自己做网站制作流程
  • 网站突然没收录了seo网站推广批发
  • 衡水淘宝的网站建设网站开发