java中的url编码?

suj*_*jit 0 java url jsp servlets urlencode

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

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>
Run Code Online (Sandbox Code Playgroud)

单击提交按钮后,显示的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);
Run Code Online (Sandbox Code Playgroud)

result.jsp中

displays data here

bla*_*mpe 6

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

在表单编码中,URL字符串将转换为可用于Internet的有效ASCII格式.来自URLEncoder.encode(String,String)文档:

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

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

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