-1

I'm downloading a file without an extension when I go to a servlet

This is code of doGet method (these are just test lines, don't take them seriously):

try {
        PrintWriter pw = response.getWriter();
        pw.write("test");
        pw.println(request.getParameter("a"));
        DAOFactory m = DAOFactory.getDAOFactory(1);
        Connection conForTests = MySQLDAOFactory.getConnection();
        UserDao s = m.getUserDao();
        boolean check  = s.validateUser("test1","test1",conForTests);
        pw.write(String.valueOf(check));
        User user = s.findUser("test1",conForTests);
        int id = user.getUserId();
        pw.write(11);

    } catch (SQLException|IOException  sqlException) {
        System.out.println("asdsad");
        sqlException.printStackTrace();
    }

    System.out.println("asdsad");
}

And I checked all the lines removing them line by line and I have found out that at this line:

 pw.write(11);

And that's 11 was a user id so to not retrieve that id each time, I have just written 11. The servlet starts not showing a page, but downloading a file without an extension.

I checked that 11 number is staying for a Vertical Tab in ASCII table. Why is 11 code in ASCII table makes browser to not displaying but downloading file?

And that is content of this file:
file content

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
DozezQuest
  • 179
  • 7

1 Answers1

0

Why am I downloading a file with no extension using servlet?

Because you just opened a response stream and started writing into it. In lay terms, you are just sending some bytes back to the browser, but the browser doesn't know what does bytes are. Is it html? Is it plain text? Is it an image? Some other thing?

So before starting to write the response, you need to say what that response is by setting a content type. Replace this code of yours:

try {
    PrintWriter pw = response.getWriter();
    ....

with:

try {
    response.setContentType("text/html")
    PrintWriter pw = response.getWriter();
    ....

or whatever content type you prefer (a text/plain can also work for what you are doing).

See also:

Bogdan
  • 23,890
  • 3
  • 69
  • 61
  • I undestand that, but why then when i type some other strings there is not any problem with displaying it ?I mean, when i type content to printstream, other than 11th number , browser displaying it as html/text by default so i dont need to specify the content type, but when 11th number comes up it doesn`t want to display it and just download some file without an extension and in that file there are two brackets that represent that 11 number – DozezQuest Jun 04 '21 at 17:20
  • And could you please tell me about meta tag in html document i mean what is the purpose of setting content-type to html document if its always html, i mean we specify the content-type in response object so that browser will get Content-type header with that information but then in html file we write meta tag with http-equiv attribute and set content type to another type what is gonna happen?And okay, its only works if the file is html so that browser could undestand that meta tag – DozezQuest Jun 04 '21 at 17:58
  • But then what is purpose of all of that?I mean browser only could undestand that meta tag if the file is html so response header must be set to text/html but then in the file we set meta tag attribute value to another Content-type and what is the purpose of doing that? – DozezQuest Jun 04 '21 at 18:00
  • Those are all mechanisms to tell the browser how to interpret the response. Without them, the browser will make guesses, which sometimes can be wrong. For your first question, the vertical tab is a control character in ASCII so it isn't something you would expect in printable text. So the browser probably encounters it and makes a guess that the content is binary. The result should probably be the same for other control characters not just vertical tab. For your second question see this https://www.w3.org/International/questions/qa-html-encoding-declarations – Bogdan Jun 04 '21 at 18:40