wood burning stoves 2.0*
The moose likes JSP and the fly likes I need help with this situation regarding form post parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "I need help with this situation regarding form post parameters" Watch "I need help with this situation regarding form post parameters" New topic
Author

I need help with this situation regarding form post parameters

Boresm Borest
Greenhorn

Joined: Feb 22, 2012
Posts: 8
Hi
I have a form.jsp file which has the following :


The form post data are sent to LoginServlet as usual but I want to be able to catch the form data in the form.jsp file itself exactly as php does. I am surprised way php has this feature where jsp no!! Because I want to use the form data to process some tasks as well as sending the user to LoginServlet.

Regards
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Processing should never be done in a JSP. JSPs are for creating views, not processing; that's what servlets are for.

You also should never use Java code in a modern JSP. That's an outdated practice that has been discredited for 10 years now.

If you are coming from PHP and learning how to write Java web apps, you need to realize that you are not using tinker toys any longer and that many "PHP ways" of doing things are not going to be considered best practices.

I recommend that all newcomers to JSP and Java web apps read the following articles:
  • The Secret Life of JSPs
  • The Front Man


  • The form should be posted to the servlet, and any processing should happen there. What is it that you are feeling should be done by the JSP?


    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Nam Ha Minh
    Ranch Hand

    Joined: Oct 31, 2011
    Posts: 484

    Boresm Borest wrote:

    The form post data are sent to LoginServlet as usual but I want to be able to catch the form data in the form.jsp file itself exactly as php does. I am surprised way php has this feature where jsp no!! Because I want to use the form data to process some tasks as well as sending the user to LoginServlet.

    Regards


    Well, it is very simple in JSP. Just use EL (expression language):



    Replace parameterName by the actual name of the parameter you want to capture.


    PM Me If You Want to get Urgent Help on Java Programming
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60057
        
      65

    Nam Ha Minh wrote:

    Not correct. Post parameters are never placed in request scope.

    And, as has already been pointed out, posting to a JSP is a poor practice that should not be encouraged.
    Nam Ha Minh
    Ranch Hand

    Joined: Oct 31, 2011
    Posts: 484

    Bear Bibeault wrote:
    Nam Ha Minh wrote:

    Not correct. Post parameters are never placed in request scope.

    And, as has already been pointed out, posting to a JSP is a poor practice that should not be encouraged.


    Sorry, my mistake. It should be ${param.parameterName}
    Technically, a JSP is eventually compiled to a servlet, so what's the difference?
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60057
        
      65

    That's an implementation detail. JSPs are intended to create the HTML views, and other use is a violation of good practices and a misuse of the technology.

    In any case, the proper use of JSP has been discussed at length elsewhere. Please search for previous discussions if you want more information on best practices.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60057
        
      65

    Punit Jain, please do not hijack other people's posts with your own questions. Your post was moved to a new topic.
    Boresm Borest
    Greenhorn

    Joined: Feb 22, 2012
    Posts: 8
    Hi again; and please read carefully this nota.

    After some research I have found that what I was plaining to do isn't possible that is all neither with jsp or php. The form is located to login.jsp and this form has action to ServLogin servlet so this mean that when the user arrive to form.jsp, the post doesn't exists yet so there is nothing to catch! when the user submit the button the form.jsp is no longer proceeded at all because the data of the request are sent to the servlet LoginServlet, ${param.parameterName} didn't do anything because I am talking about form.jsp file and not the servlet. If I put the ${param.parameterName} in the form.jsp. What happen is that the form.jsp is proceeded when the user arrives to that page so right now there is no post request so nothing to catch, when the user click the form submit button we are no longer in form.jsp but LoginServ servlet which handle the post request so ${param.parameterName} in the login.jsp file is useless.

    An alternative idea that come to my mind is using the jsp forward, so here we start :

    1 - In the directory which contain login.jsp, i create a void file called fwd.jsp
    2 - I put this code into the contain of fwd.jsp :

    3 - In the form.jsp file I change the following :

    to

    Now logically, what I am planing to do is clear: This process is equivalent to the first situation except here we pass the post request to a third file before sending the user to the LoginServ servlet. So I have some questions regarding the slash in the forward code so is it right like that :

    or like that :


    Edit: What it matter for you regarding locations of those files is that both form.jsp and fwd.jsp are located in the seem directory and that form.jsp uses action="LoginServ" where LoginServ servlet is located in a separate location probably.

    Regards
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60057
        
      65

    You seem to be making this much more complicated than it needs to be and jumping through needless hoops.

    Why do you need this so-called "3rd file" in the first place? You haven't really answered the question of what you are trying to accomplish. Chances are you're either doing something that's needless, or trying to do something in a manner that's contrary to best practices and can be accomplished in a much more simple manner.
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18135
        
        8

    Boresm Borest wrote:Hi
    I have a form.jsp file which has the following :


    The form post data are sent to LoginServlet as usual but I want to be able to catch the form data in the form.jsp file itself


    But that doesn't make any sense at all. The data is sent from that JSP to a LoginServlet, as it should be. Or more precisely, that JSP generates HTML which is sent to the browser, and later the browser sends the data to LoginServlet. There is no reason to expect that that JSP is going to receive the parameters that will be sent from the browser to the servlet five minutes later. (And PHP doesn't do that either.)

    And there is no way for you to make that work, nor should there be any way. It's the responsibility of the LoginServlet to deal with the login information, it's nothing to do with that JSP. Nor should it be anything to do with any other JSP. So whatever you are trying to do, you are on the wrong path.

    So, how about if you tell us what you are trying to do? And don't tell us that you want this JSP to do that thing. Tell us why you want a JSP to get information from a login page.
    Boresm Borest
    Greenhorn

    Joined: Feb 22, 2012
    Posts: 8
    Okay I managed for a solution; action=form.jsp instead of action=LoginServlet. For the purpose of that is to process some checks before sending the user to the servlet that is all the story. It is the servlet which will process the post request using doGet and doPost procedures so I keep respecting the rules, thoses checks didn't looks into the post data itself but does something else.

    I want to forward the post request of my form to the servlet LoginServlet exactly as if I make action="LoginServlet" in my form. My form is in form.jsp and this is my approach :

    Content of form.jsp


    So the slash in the <jsp:forward page="/LoginServlet"/> is it mandatory or useless ?
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60057
        
      65

    Boresm Borest wrote:Okay I managed for a solution; action=form.jsp instead of action=LoginServlet.

    NO. No. No. Never send a form to a JSP!

    For the purpose of that is to process some checks before sending the user to the servlet that is all the story

    As has already been pointed out, a JSP for "performing checks" is the worst possible choice. What "checks" need to be done prior to getting to the servlet, which is early what should be performing any checks.?

    It is the servlet which will process the post request using doGet and doPost procedures so I keep respecting the rules

    I don't know what you mean by this, could you clean it up. But so far, you shown very little following of "the rules", is by rules you mean best practice. Please read this article.

    thoses checks didn't looks into the post data itself but does something else.

    Such as? No matter what it is, JSP is the wrong place to do it!

    Boresm Borest
    Greenhorn

    Joined: Feb 22, 2012
    Posts: 8
    I think you are neglecting a bit the jsp technology, if I follow your rules than no single dynamic process should be done with jsp just servlet's if so why Oracle has invented jsp ? just html suffice no ?
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18135
        
        8

    You are ignoring two important features of language design:

    (1) Language designers add new features as time passes.

    (2) Language designers rarely remove old features.

    So here's what happened: Sun (not Oracle, they acquired Sun only a couple of years ago) designed JSP. In the beginning it featured scriptlets and various other primitive features. After a few years it became obvious that the architecture of JSP was rather poor. So various new features like JSTL and tag libraries were added, to replace the old features which led people to design cruddy systems which were impossible to debug and hard to maintain.

    That was in 2002. All of the features which you are stubbornly defending have been obsolete since then. And why are they still in JSP? It's because of feature (2). If you remove an old feature then you break all of the programs which were already written to use that feature. They may be cruddy programs but breaking them would be irresponsible.

    I assume it is no longer 2002 where you live? Then don't behave as if it is. Programming has moved on since then, and ignoring ten years of progress is just a bad attitude.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60057
        
      65

    Boresm Borest wrote:I think you are neglecting a bit the jsp technology

    Yes. And quite deliberately. As Paul so eloquently pointed out (well done, Paul) parts of JSP have been discredited for 10 years now. A whole decade. Do you not think it's time to catch with modern approaches?

    Did you read the article I linked to? You have a bit of catching up to do.

    html just html suffice no ?

    No, as HTML has no ability for client-side templating. That's something that is JSP's primary purpose for being. Read this article for more information on exactly what JSP is and how it can be used.
    Boresm Borest
    Greenhorn

    Joined: Feb 22, 2012
    Posts: 8
    Hey, I was so idiot that is all the story. Well, when browsing some tomcat sevlet and jsp in the doc I have seen some jsp files with a form, and when click submit and what I have tiped is wrong it goes later to a servlet and didn't return back to that jsp but the seem form appear again, with the seem source code in both.

    Login.jsp :

    servlogin :

    So that simply mean that I can put the form into the servlet as well as into the jsp. Have a single question now. When I press submit button of http://myserver.com/homelogin/login.jsp I want to http://myserver.com/homelogin/servlogin does this mean that now I am no longer in the login.jsp file but I am in the servlogin servlet do you ?
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60057
        
      65

    Boresm Borest wrote:So that simply mean that I can put the form into the servlet as well as into the jsp.

    No. Why would you repeat it? What would be the purpose of putting the form into the servlet? The servlet should process the input coming from the form, not be the form! If, after the processing is complete, you need to show the form again, simply redirect back to the page controller for the form's JSP (you have been reading the articles right? so you you know what a page controller is? and the PRG pattern?)

    Have a single question now. When I press submit button of http://myserver.com/homelogin/login.jsp I want to http://myserver.com/homelogin/servlogin does this mean that now I am no longer in the login.jsp file but I am in the servlogin servlet do you ?

    When the submit button of a form is clicked, the form is submitted to the action specified by the form. This is indeed a completely different request from the request that ended up displaying the form in the browser. And as for "I am no longer in the login.jsp file"... if you have been reading the articles I've pointed you to (read them if you have not), you'll know that you are no longer "in the JSP" as soon as the response is sent to the browser.
    Boresm Borest
    Greenhorn

    Joined: Feb 22, 2012
    Posts: 8
    I understand what you mean, what I have said before is that some examples shows that when I submit the form and if the submission fail than the form should return again to retry, but the link that change from the first which is http://localhost/../homelogin/login.jsp to http://localhost/../homelogin/servlogin so what this mean ? this mean that people who program that have made the form in the servlet because if it wasn't in the servlet than it is http://localhost/../homelogin/login.jsp that should appear again.
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18135
        
        8

    Boresm Borest wrote:but the link that change from the first which is http://localhost/../homelogin/login.jsp to http://localhost/../homelogin/servlogin so what this mean ?


    This "link" you're talking about is what you see in the address bar of the browser? What you see there is the URL that the request was sent to.

    this mean that people who program that have made the form in the servlet because if it wasn't in the servlet than it is http://localhost/../homelogin/login.jsp that should appear again.


    No, that's completely incorrect. The browser sends a request to the "servlogin" servlet. In a well-run system this servlet would authenticate the user ID and password parameters, using a database or whatever other process is in the design. If it decides to reject those credentials, it may well forward the request to a JSP whose job is to prepare HTML containing user ID and password parameters (and a message saying the first try failed). And then this HTML will appear in the browser. But the browser does not know that the forwarding took place in the server, so of course what you see in the browser's address bar does not reflect that.

    And that is how it should be. The URLs in your system's HTML should always point to servlets, never to JSPs. Once a servlet has done the business processing on its request parameters, it will forward to a JSP to generate the HTML for the response. The client should not know anything about that and should not ever see a URL which points directly at a JSP.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60057
        
      65

    Boresm Borest wrote: this mean that people who program that have made the form in the servlet because if it wasn't in the servlet than it is http://localhost/../homelogin/login.jsp that should appear again.

    Wrong. It most likely means that the servlet forwards from the servlet to the form's JSP. This works but is not a great way to do it. It means that a refresh will cause the servlet action to repeat. That's why the article that you likely haven't read yet introduces the PRG pattern that prevents this from occurring.

    Bear in mind that just because you see an example of some code, it doesn't mean that it's good code. The web is full of examples of how to do things badly. That's one of the reasons that CodeRanch is here. The experienced developers who volunteer here emphasize doing things the right way, using best practices.

    That's why you'll see us harp time and time again on things like using scriptlets in JSPs, and other bad practices. CodeRanch isn't the place to come and learn how to do things sloppily and quickly; it's the place to come and learn how to do things well.
    Boresm Borest
    Greenhorn

    Joined: Feb 22, 2012
    Posts: 8
    Okay, thank you both for great help lol. I will use the servlet to catch the post data. So now only one question remain. Suppose I am in the directory where the servlogin is located "the servlogin.java". As it is known that even if I made the changes to that servlet it didn't reflect instantly because this servlet need to recompile each time its source code has been changed, so without getting hassle server stop/start which command to compile the servlet and make the server proceed the new changes ? I suppose the servlet api is in :

    /usr/share/tomcat6/lib/servlet-api.jar for linux system
    C:\servlet-api.jar for Windows system

    Because if I run javac -cp /usr/share/tomcat6/lib/servlet-api.jar servlogin.java for example it actually compile but didn't take the new changes to account, it is so frustrating to do stop/start server to take the new changes. If there is an option in the server to tell it to automatically look for servlet source code change and if there is a change automatically compile and run now changes it would be so helpfull exactly like what jsp technology do, because with jsp the server automatically look into changes on jsp file and if there is change than it automatically process the new changes with no hassle.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60057
        
      65

    You don't need to stop and restart the server, just the web app. And yes, the web app must be restarted when a servlet, or any other java class, is updated.

    Tomcat has a manager app that makes this easy. It also has an option to auto-reload an application when a class changes. Use only for development.

    If you are using another container, you'll need to look up its capabilities.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: I need help with this situation regarding form post parameters
     
    Similar Threads
    HTTP POST maximum data size?
    Fileupload using Servlet
    query regarding present code
    Accessing the full path of a file uploaded via an HTML form
    How to store image in JSP from BLOB captured in Oracle