0

I want to know that what is url encoding. I have 2 jsp pages and one servlet. When I run the application the url displayed is :

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

where

index.jsp :

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

after the submit button is clicked the URL displayed is :

http://localhost:8080/myproject/Myservlet

What is the meaning of URL encoding? How can I encode url?

From index.jsp goes to Myservlet then to result.jsp

Myservet#doPost // Do I need to do URL encoding here? If yes how ?

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

result.jsp

displays data here

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
sujit
  • 251
  • 5
  • 12
  • 23
  • please somebody give some answers to my question – sujit Apr 23 '12 at 07:46
  • Did you even try to google it? http://en.wikipedia.org/wiki/URL_Encoding – home Apr 23 '12 at 07:52
  • Possible duplicate: http://stackoverflow.com/questions/8713208/how-to-encode-a-url-with-the-special-character-percentage (See the 2nd response) – blackcompe Apr 23 '12 at 07:53
  • ya i googled but i did n't get answers to my question...there i found confusing topics and not coming near to my question..if you know then please help me little bit by posting your answer, how can i encode url? – sujit Apr 23 '12 at 07:55
  • Just search for 'java url encoding'... – home Apr 23 '12 at 07:58
  • @blackcompe can you please help me by writing Balus code in `scriplets`, i am using scriplets in jsp not jstl, i am getting confused in jstl and i am learning it – sujit Apr 23 '12 at 07:58
  • ya i am searching but truly speaking i am not getting any answer for my question – sujit Apr 23 '12 at 08:02
  • URL encoding is not the answer to any problem you have described here. – user207421 Apr 23 '12 at 08:14
  • @sujit: You should be able to make due with the information I've provided. – blackcompe Apr 23 '12 at 08:19
  • @EJP i saw about some vulnerable attacks on websites by url, so how to protect url? by encoding url or some other methods are there? – sujit Apr 23 '12 at 08:20
  • @blackcompe i got your answer but in my application i.e. `http://localhost:8080/myproject/index.jsp` should be formed to an other url like `http://localhost:8080/myproject/rrwr%f$$%ddsd`? i am unable to convert your answer in my web application – sujit Apr 23 '12 at 08:25
  • Solution to what? I suggest you reduce your question to something that can actually be answered. URLEncoding isn't a solution to website vulnerabilities either. You seem to have fixed on a 'solution' without even deciding what the problem is. – user207421 Apr 23 '12 at 08:47
  • @EJP ok i am telling that many times while visiting websites url you would have seen like this: `http://www.xyz.com/index.jsp?erwerc%frefrr_55%...`like this, what is this`?erwerc....` how this comes after index.jsp? – sujit Apr 23 '12 at 09:20
  • @sujit These are URL arguments and they are indeed URL-encoded. This is once again not something that requires a 'solution'. – user207421 Apr 23 '12 at 09:50
  • @EJP..i have asked same question i.e. how can i format url to be looking like the above one i.e. `http://localhost:8080/myproject/index.jsp?wwffwfrf%_%dffrfewr...` like this...i want this type of url encoding...is there any way how can i do it? Please EJP – sujit Apr 23 '12 at 10:08

3 Answers3

7

There are two types of encoding: HTML form encoding and URL re-writing.

In form encoding, the URL string is converted into a valid ASCII format that's Internet-ready. From the URLEncoder.encode(String, String) docs:

Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme. This method uses the supplied encoding scheme to obtain the bytes for unsafe characters.

The second kind is URL rewriting. The URL string is encoded with a session id in case the client browser doesn't support (or has disabled) cookies or session tracking. From the HttpServletResponse.encodeURL(String) docs:

Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged. The implementation of this method includes the logic to determine whether the session ID needs to be encoded in the URL. For example, if the browser supports cookies, or session tracking is turned off, URL encoding is unnecessary.

blackcompe
  • 3,180
  • 16
  • 27
2

I think you have misundersting here. Neither HTML Form Encoding nor URL Re-writing is for what you want to achieve.

If you want to achieve like .

For example: instead of typing http://localhost:8080/search.jsp?xxx the user would see http:/localhost:8080/search?xxx

You can create a servlet mapping like this:

<servlet-mapping>
   <servlet-name>MappingServlet</servlet-name>
   <url-pattern>path/*</url-pattern>
 </servlet-mapping>

The url-pattern must be edited to suit your needs. You need of course to create the servlet in order to map the url to the actual jsp. This technique is used by most of the MVC frameworks.

Read More on How to develop JSP/Servlets Web App using MVC pattern?

Community
  • 1
  • 1
Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
  • thanks Hardik for your answer..servlet is coming fine i.e. http://localhost:8080/search but for jsp how can i display like this: http://localhost:8080/search (not 8080/search.jsp)? can i do something in web.xml? – sujit Apr 23 '12 at 09:34
  • Just like we map servlets to url in web.xml, you have to map jsp page with a particular url pattern in web.xml. – Sorter Feb 02 '13 at 13:33
1

Use java.net.URLEncoder.encode(s, "UTF-8") where s is the string to encode.

This is required whenever we send text as path segments, query string arguments, etc...

Example: see the documentation

Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80