简体   繁体   English

如何在Servlet中加密网址?

[英]How to encrypt Url in Servlet?Ho

I want to encrypt my Url in servlet .Please check my code below 我想在servlet中加密我的网址,请检查下面的代码

TestServlet 测试Servlet

public class TestServlet extends HttpServlet {
    public void doGet(HttpServletRequest request,HttpServletResponse response)
        throws IOException {
      String url"Test2?name=bhanuprasd";
      response.sendRedirect(url);
    }
}

Test2 测试2

public class Test2 extends HttpServlet {
    public void doGet(HttpServletRequest request,HttpServletResponse response)
        throws IOException {
        System.out.println("calling my first servlet");
        String name = request.getParameter("name");
        response.getWriter().print("hello"+name);
    }
}

when my request redirect to my page in browser it's displayed name like this 当我的请求重定向到浏览器中的页面时,其显示名称如下

**http://MyServer/Test/Test2?name=bhanuprasd**

I don't want to show the name in browser How can I do this. 我不想在浏览器中显示名称。我该怎么做。 I try this way but not working 我以这种方式尝试但无法正常工作

String url=java.net.URLEncoder.encode("Test2?name='bhanuprasd'");
String url1 =response.encodeRedirectUrl("Test2?name='bhanuprasd'");

I think what you need here is POST instead of GET. 我认为您需要的是POST而不是GET。 Anyway you can encrypt your request params when adding them and decrypt before using them. 无论如何,您可以在添加请求参数时对其进行加密,并在使用它们之前进行解密。 I don't think there is a direct way of doing this. 我认为没有直接的方法可以做到这一点。

PS: I think you are confused about usage of url encoding. PS:我认为您对url编码的使用感到困惑。 http://www.w3schools.com/tags/ref_urlencode.asp http://www.w3schools.com/tags/ref_urlencode.asp

Your question implies you want to encrypt the parameters somehow, but then in the detail it doesn't seem that way. 您的问题暗示您想以某种方式加密参数,但是在细节上似乎并非如此。

If you don't want the parameters to be displayed, use a POST request as others have said. 如果您不希望显示参数,请使用其他人所说的POST请求。 A POST request sends parameters in the body of the request, rather than in the query string. POST请求在请求的正文中而不是查询字符串中发送参数。 The Servlet container can handle either, via doGet and doPost. Servlet容器可以通过doGet和doPost处理。

If you want to use a GET request, but don't want the parameters to appear in plaintext, you can use an encoding like Base64 and then URL encoding encode the parameters. 如果要使用GET请求,但不希望参数以纯文本形式出现,则可以使用Base64之类的编码,然后使用URL编码对参数进行编码。 Note that this merely obscures the text, and is not in any way a security measure. 请注意,这仅是模糊的文字,绝不是安全措施。

If you want to encrypt communication between your server and the client, use SSL . 如果要加密服务器和客户端之间的通信,请使用SSL

If you truly want to encrypt the parameters themselves, you'll likely need to do this yourself using public key encryption. 如果您确实想对参数本身进行加密,则可能需要使用公钥加密自己进行。 There is no out of the box solution for this that I'm aware of, probably because it's not something applications need to do very often. 我知道没有针对此问题的开箱即用的解决方案,可能是因为它不是应用程序经常需要做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM