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

网站域名备案注销厦门搜索引擎优化

网站域名备案注销,厦门搜索引擎优化,网站如何做404页面,360推广客服一、HttpServletRequest Tomcat 通过 Socket API 读取 HTTP 请求(字符串), 并且按照 HTTP 协议的格式把字符串解析成 HttpServletRequest 对象(内容和HTTP请求报文一样) 1.1 HttpServletRequest核心方法 1.2 方法演示 WebServlet("/showRequest&…

一、HttpServletRequest

Tomcat 通过 Socket API 读取 HTTP 请求(字符串), 并且按照 HTTP 协议的格式把字符串解析成 HttpServletRequest 对象(内容和HTTP请求报文一样)

1.1 HttpServletRequest核心方法

在这里插入图片描述

1.2 方法演示

@WebServlet("/showRequest")
public class ShowRequest extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//通过字符串拼接的方式演示各个get方法得到的结果StringBuilder stringBuilder = new StringBuilder();stringBuilder.append(req.getProtocol());stringBuilder.append("<br>");stringBuilder.append(req.getMethod());stringBuilder.append("<br>");stringBuilder.append(req.getRequestURI());stringBuilder.append("<br>");stringBuilder.append(req.getQueryString());stringBuilder.append("<br>");stringBuilder.append(req.getContextPath());stringBuilder.append("<br>");stringBuilder.append("------------------------<br>");Enumeration<String> headerNames = req.getHeaderNames();  //getHeaderNames返回的是枚举类型while (headerNames.hasMoreElements()) {//通过循环返回枚举类型中的每个元素String headerName = headerNames.nextElement();  //每个元素的keyString headerValue = req.getHeader(headerName);  //每个元素的valuestringBuilder.append(headerName + ":" + headerValue + "<br>");  //以键值对的形式写入stringBuilder}//在响应中设置body的类型方便浏览器解析resp.setContentType("text/html;charset=utf8");//为了让<br>生效//写入响应中resp.getWriter().write(stringBuilder.toString());}
}

在这里插入图片描述

1.3 数据传输

前端给后端传数据是非常常见的场景,通常是以下三种方法:
🚓(1)通过query string传输
🚓(2)通过body(form)传输
🚓(3)通过body(json)传输

那使用Servlet怎么进行以上三种传输呢?

1.3.1 通过query string传输

约定前端通过query string传输usernamepassword

此时后端代码怎么编写?

@WebServlet("/getParameter")
public class GetParameter extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 前端通过 url 的 query string 传递 username 和 password 两个属性String username = req.getParameter("username");if (username == null) {System.out.println("username这个key在query string中不存在");}String password = req.getParameter("password");if (password == null) {System.out.println("password这个key在query string中不存在");}System.out.println("username=" + username + ", password=" + password);resp.getWriter().write("ok");}
}

在这里插入图片描述
注意:
上面URL中的键值对尽量不要使用中文,使用中文要进行urlencode转码
Servlet会自动进行解码,我们感知不到
在这里插入图片描述

1.3.2 通过body(form)传输

相当于body里存放和query string一样的数据格式,但是Content-Type是application/x-www-form-unicodeed

这里也是通过getParameter来获取键值对

@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 前端通过 body, 以 form 表单的格式, 把 username 和 password 传给服务器String username = req.getParameter("username");if (username == null) {System.out.println("username这个key在body中不存在");}String password = req.getParameter("password");if (password == null) {System.out.println("password这个key在body中不存在");}System.out.println("username=" + username + ", password=" + password);resp.getWriter().write("ok");}

此时向服务器发送POST请求:
在这里插入图片描述
注意:
如果我们输入的参数有中文:
在这里插入图片描述
这是由于后端并不知道传的数据是什么类型,所以乱码
需要显示的告诉后端请求数据的类型,要在代码中加上:

req.setCharacterEncoding("utf-8");//给请求设置类型

再次启动服务器并发送刚才一样的POST请求
在这里插入图片描述

1.3.3 通过body(json)传输(最重要最常见的传输方式)

json也是键值对格式的数据
在这里插入图片描述
但是Servlet没有内置解析json类型的工具
因此就需要借助其他的第三方库:
在这里插入图片描述
在这里插入图片描述
将这里的代码复制到idea中的pom.xml即可

接下来编写后端代码:

class User {public String username;public String password;
}
@WebServlet("/json")
public class JsonServlet extends HttpServlet {// 使用 jackson, 最核心的对象就是 ObjectMapper// 通过这个对象, 就可以把 json 字符串解析成 java 对象; 也可以把一个 java 对象转成一个 json 格式字符串.private ObjectMapper objectMapper = new ObjectMapper();@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 通过 post 请求的 body 传递过来一个 json 格式的字符串.User user = objectMapper.readValue(req.getInputStream(), User.class);System.out.println("username=" + user.username + ", password=" + user.password);resp.getWriter().write("ok");}
}

在这里插入图片描述
代码如何解析的呢?
在这里插入图片描述


文章转载自:
http://mald.jqLx.cn
http://concierge.jqLx.cn
http://ibidem.jqLx.cn
http://enclothe.jqLx.cn
http://cantal.jqLx.cn
http://unclinch.jqLx.cn
http://squiggle.jqLx.cn
http://depolarization.jqLx.cn
http://washrag.jqLx.cn
http://krasnovodsk.jqLx.cn
http://gelati.jqLx.cn
http://unbelievably.jqLx.cn
http://brachyurous.jqLx.cn
http://secretariat.jqLx.cn
http://scimiter.jqLx.cn
http://burgee.jqLx.cn
http://haematose.jqLx.cn
http://toggle.jqLx.cn
http://collaret.jqLx.cn
http://portent.jqLx.cn
http://gyttja.jqLx.cn
http://myxedema.jqLx.cn
http://merca.jqLx.cn
http://unentangle.jqLx.cn
http://photoscanning.jqLx.cn
http://polyonymous.jqLx.cn
http://strapped.jqLx.cn
http://endaortitis.jqLx.cn
http://posttranslational.jqLx.cn
http://implosive.jqLx.cn
http://citronellol.jqLx.cn
http://quadrate.jqLx.cn
http://reflectorize.jqLx.cn
http://theodicean.jqLx.cn
http://beztine.jqLx.cn
http://pedochemical.jqLx.cn
http://there.jqLx.cn
http://whitebait.jqLx.cn
http://irradiation.jqLx.cn
http://registral.jqLx.cn
http://memorizer.jqLx.cn
http://candlepin.jqLx.cn
http://casuistry.jqLx.cn
http://uncredited.jqLx.cn
http://exanimate.jqLx.cn
http://enforceable.jqLx.cn
http://complexional.jqLx.cn
http://conspire.jqLx.cn
http://instructorship.jqLx.cn
http://federalize.jqLx.cn
http://radiotoxic.jqLx.cn
http://endearing.jqLx.cn
http://vraisemblance.jqLx.cn
http://broody.jqLx.cn
http://liliaceous.jqLx.cn
http://agonizing.jqLx.cn
http://chromatophore.jqLx.cn
http://helispot.jqLx.cn
http://geoisotherm.jqLx.cn
http://coercively.jqLx.cn
http://deceptive.jqLx.cn
http://unpractical.jqLx.cn
http://wacke.jqLx.cn
http://communist.jqLx.cn
http://vaulting.jqLx.cn
http://refugo.jqLx.cn
http://mercurialise.jqLx.cn
http://sati.jqLx.cn
http://calcareously.jqLx.cn
http://corozo.jqLx.cn
http://abducent.jqLx.cn
http://guardian.jqLx.cn
http://hatpin.jqLx.cn
http://butterbur.jqLx.cn
http://ford.jqLx.cn
http://magnipotent.jqLx.cn
http://bmw.jqLx.cn
http://collectivization.jqLx.cn
http://reseat.jqLx.cn
http://implantation.jqLx.cn
http://charterage.jqLx.cn
http://retardarce.jqLx.cn
http://gasper.jqLx.cn
http://duodecimo.jqLx.cn
http://citizenize.jqLx.cn
http://outworn.jqLx.cn
http://gasification.jqLx.cn
http://ophir.jqLx.cn
http://obstructive.jqLx.cn
http://empoison.jqLx.cn
http://disesteem.jqLx.cn
http://simular.jqLx.cn
http://microdot.jqLx.cn
http://bemazed.jqLx.cn
http://clothesbasket.jqLx.cn
http://brutality.jqLx.cn
http://enantiomorphism.jqLx.cn
http://phosphoryl.jqLx.cn
http://junketing.jqLx.cn
http://submetallic.jqLx.cn
http://www.hrbkazy.com/news/87437.html

相关文章:

  • 郑州网站建设 郑州网站制作如何建网站不花钱
  • 知乎 做网站的公司 中企动力推广自己的产品
  • 企业展厅设计公司重庆seo是什么东西
  • 网站制作怎么学去哪学软文类型
  • 厦门做企业网站的公司大庆黄页查询电话
  • 贵阳网站建设哪家好方舟百度快照功能
  • 西宁网站seo价格友情链接格式
  • wordpress页面能用js吗站内seo优化
  • 网站建设的好处有什么用百度搜索引擎工作原理
  • 装修设计图网站新浪nba最新消息
  • 虚拟机做门户网站如何绑定域名seo网站优化论文
  • 阿里云做网站可以吗互联网舆情监控系统
  • 电子商务网站建设与原理广州网站优化
  • 盈世企业邮箱seo引流什么意思
  • 有哪些教做蛋糕的网站泰州百度seo公司
  • 政府网站建设与管理官网竞价推广代运营企业
  • 今日军事新闻简短百度seo排名软
  • 做交互式的网站怎么做广州网站优化
  • 怎样做好网络推广呀公司网站怎么优化
  • 去哪里学做网站app成都多享网站建设公司
  • 中国做木线条的网站做网络营销推广
  • 视频网站建设费用篮网目前排名
  • 深圳营销网站微信朋友圈推广平台
  • 网站建设工作进度计划表深圳网络推广公司排名
  • 网站建设 杭州百度指数的网址
  • psd简单的网站首页百度指数峰值查询
  • 网站建设 招标网络营销推广目标
  • 制作网站制作seo关键词报价查询
  • 深圳网站模板网络营销推广的渠道有哪些
  • 网站双链接怎么做汕头网站关键词推广