0

please help solve the problem, there is a form, here is the code:

<form action="test" method="Post">
<input type="text" name="text" autofocus >
<input type="submit" value="">
</form>

I enter the name of the file into it, and then the name is passed to the servlet. Here is a piece of code a servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException{
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        String myName = request.getParameter("text");

I have a form to enter a file name like this: test.txt, and you can enter just a test, no txt? I would be very grateful for the answers, and if you give them a piece of code) I enter only files with the extension txt, without the need

Eric Scot
  • 197
  • 8
  • 1
    I have no idea what you're asking here. Your form does not relate to files in any way. It's just a text field and whatever the user enters should be available to the servlet. What exactly does not work, please describe the use-case step by step? – home Nov 12 '12 at 18:47
  • 1
    Furthermore, how does this question relate to your [previous question](http://stackoverflow.com/questions/13320521/servlet-does-not-open-txt)? – home Nov 12 '12 at 18:50
  • String myName = request.getParameter("text").replaceFirst("[.][^.]+$", ""); – Eric Scot Nov 13 '12 at 13:20
  • so I was trying to get rid of the extension – Eric Scot Nov 13 '12 at 13:29

1 Answers1

3

I didn't understand what do you want. Can you explain it better,please?

Do you want to call the servlet from the HTML? If it is so, then you must put the address on the

<form action="servlet_url" method="post">

where servlet_url its the servlet address. If the servlet its in your project with the html web page then its easy to find the URL, if it is not the case, you should deploy the application with the servlet and see the final url within the application server (tomcat, weblogic, jboss, etc).

You talk about append ".text" in the code, like this?

String myName = request.getParameter("text");
if (myName != null && myName.length() > 0) {
    myName += ".txt";
}

If you want to see if the ".txt" is already included in the name, you can you any String api to verify that like:

if(!myName.endsWith(".txt")) myName += ".txt";

or you can join the two approaches!

José Cruz

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
josedacruz
  • 64
  • 1
  • 7
  • I repeat once again that I enter test. txt, a servlet takes this only txt, and I need to make it not have to enter txt file extension razyasnyu servlet accepts the principle of what I had introduced such as: test.txt, then it looks for the file, and makes him some action, but the fact that he does not know if I enter the test, he understands only a test.txt,that's how I do without txt – Eric Scot Nov 12 '12 at 18:14
  • 1
    @EricScot what part of checking if the `String` ends with "txt" suffix do you have problem? – Luiggi Mendoza Nov 12 '12 at 18:55
  • Thank you, but I need to check whether the extension of the file I need to get rid of it – Eric Scot Nov 13 '12 at 11:56
  • String myName = request.getParameter("text").replaceFirst("[.][^.]+$", ""); – Eric Scot Nov 13 '12 at 13:19
  • so I was trying to get rid of the extension – Eric Scot Nov 13 '12 at 13:29