小编典典

Java中的URL编码?

jsp

我想知道什么是 url编码 。我有2个jsp页面和1个servlet。当我运行应用程序时,显示的网址是:

http://localhost:8080/myproject/index.jsp

哪里

index.jsp:

<form action="Myservlet" method="post">
    <input type="text" name="mytext" id="mytext"/>
    <input type="submit" value="submit"/>
</form>

单击提交按钮后,显示的URL为:

http://localhost:8080/myproject/Myservlet

URL编码是什么意思? 如何编码网址?

从开始index.jspMyservlet然后result.jsp

Myservet#doPost //我需要在这里进行URL编码吗?如果是,怎么办?

  fetching data from db.......
  ....................
  String nextJSP = "/result.jsp";
  RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
  dispatcher.forward(request,response);

result.jsp

displays data here


阅读 1084

收藏
2020-06-10

共1个答案

小编典典

编码有两种类型:HTML表单编码URL重写

在格式编码中,URL字符串被转换为可用于Internet的有效ASCII格式。从URLEncoder.encode(String,String)文档:

使用特定的编码方案将字符串转换为application / x-www-form-
urlencoded格式。此方法使用提供的编码方案来获取不安全字符的字节。

第二种是URL重写。如果客户端浏览器不支持(或禁用)cookie或会话跟踪,则URL字符串将使用会话ID进行编码。从HttpServletResponse.encodeURL(String)文档中:

通过在其中包含会话ID对指定的URL进行编码,或者,如果不需要编码,则返回不变的URL。此方法的实现包括确定会话ID是否需要在URL中编码的逻辑。例如,如果浏览器支持cookie,或者关闭了会话跟踪,则不需要URL编码。

2020-06-10