JSP Getting Started

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #16
    Would you experts recommend me an article that discuss about creating effective javabeans or bugs of tomcat 5.5.x,

    Im not sure about google's suggested...... .

    I've encountered errors on getting the exact value from my bean........
    If im gonna restart the tomcat, the errors gone, but when im going to add additional jsp code that filters the input, then the errors came back again.......

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #17
      Originally posted by sukatoa
      Would you experts recommend me an article that discuss about creating effective javabeans or bugs of tomcat 5.5.x,

      Im not sure about google's suggested...... .

      I've encountered errors on getting the exact value from my bean........
      If im gonna restart the tomcat, the errors gone, but when im going to add additional jsp code that filters the input, then the errors came back again.......
      That problem specifically, i've used an arraylist......

      Base on my observation, when i used the class arraylist on my bean, a random interval will fail the updated value to be retrieved......

      Is this problem already exists?

      Comment

      • sukatoa
        Contributor
        • Nov 2007
        • 539

        #18
        Originally posted by sukatoa
        That problem specifically, i've used an arraylist......

        Base on my observation, when i used the class arraylist on my bean, a random interval will fail the updated value to be retrieved......

        Is this problem already exists?
        I got it,

        For me, there is a conflict when creating a bean that extends/contains an Object type ArrayList that will be used for storing some data that represents a String value.....

        I don't know what's the real reason, but when i try to convert an Object type list into String type list

        Code:
        ArrayList<String> ret = new ArrayList<String>();
        for(Object obj: ObjectList){
               ret.add((String)obj);
        }
        return ret;
        The conflict occurs, in any time interval, the conversion fails........
        Just for sure, i restarted the tomcat for the class files to be refreshed......

        Hence, changing the type of the List into String resolves the issue........

        Comment

        • sukatoa
          Contributor
          • Nov 2007
          • 539

          #19
          This is my first time to have IF/ELSE statements more than i've expected on a single file..........

          Is it natural to have more if/else statement on just a single JSP file?
          or can you recommend to me experts how to reduce such structure? ( URL )

          Looking forward on your replies,
          sukatoa

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #20
            Originally posted by sukatoa
            ...

            The conflict occurs, in any time interval, the conversion fails........
            ...
            What was the exact error message?

            Comment

            • chaarmann
              Recognized Expert Contributor
              • Nov 2007
              • 785

              #21
              Originally posted by sukatoa
              This is my first time to have IF/ELSE statements more than i've expected on a single file..........

              Is it natural to have more if/else statement on just a single JSP file?
              or can you recommend to me experts how to reduce such structure? ( URL )

              Looking forward on your replies,
              sukatoa
              You can reduce such a structure by making a decision table. And put the long expression in a function!
              Example (Wrong way):
              [HTML]<HTML>
              <FONT color=<% if(a==7) out. write("red") else {if (b==1) out.write("yell ow") else if (b==2) out.write("red" ) else out.write("gree n")}%> Hello World </FONT>
              </HTML>[/HTML]
              Example(Right way):
              [HTML]<%!
              private String getColor(int a, int b)
              {
              if (a==7 || b==2) return "red";
              if (b==1) return "yellow";
              return "green";
              }
              %>
              <HTML>
              <FONT color=<%= getColor(a,b) %> Hello World </FONT>
              </HTML>[/HTML]


              Just a sidenote:
              Every if-else can be reduced to a formula. Fore example:
              Code:
              z=0;
              if (x=7) z=3; else z=5;
              can be written as
              Code:
              z = (x=7) ? 3 : 5
              or as a formula:
              Code:
              z =  Math.abs(Math.signum(x-7)) * 2 + 3;
              The last expression evaluates to
              case x==7 --> 0*2+3 --> 3
              case x !=7 --> 1*2+3 --> 5

              Comment

              • sukatoa
                Contributor
                • Nov 2007
                • 539

                #22
                What was the exact error message?
                @ reply#16(mine), that error occured due to typo.......

                @reply#17
                Error fixed, yet the problem persist, only the Object type ArrayList when converted to String on JSP page, well i don't know the reason but not all values are set on the said bean........

                Before i've come-up the alternative way, my bean has an Object-type ArrayList that stores all the String representation value from my sign-up form.....

                In any time, all the values that i was trying to store on that bean from the validation.jsp fails, no error message issued...

                My first approach was it is a known/unknown bug, then i tried to rewrite the type of that ArrayList into String instead of Object since all the values are Strings........

                Here is my previous algo:
                scope=session.

                catch all values from the form through request.getPara meter() method
                store them in a temporary String type ArrayList.

                If there is an invalid input, blank or unmatched values, then erase the password, retypepassword, emailaddress,re typeemail and confirmation code values from that temporary String List and replace them with a value "invalid".
                else, persist.

                Convert the temp list into Object type List then upload to the bean.....

                Redirect into the signup page to fill-up correctly all the necessary fields.

                Initialize a temp String type arraylist and store all the values from the bean(Object type, assumed it was converted again down to String)

                Now, here is the problem.

                alternately, all values that was stored by the validation.jsp into bean is not exactly received by the signup form(JSP page)...... I've list down the behavior of the beans,

                first attempt:
                Lacking: username, creditcard number
                second attempt:
                Not exact: password & retypepassword
                third attempt:
                validation code
                fourth attempt:
                etc & etc.......

                And before i click the submit button, i've wrote all the values exactly what my naked eyes could see.....

                And, it appears that there is a problem on my inputs..(i've created an indicator that tells the guests about their wrong input/leaved blank)......

                I get stuck on this experiment(last day). I've exert all of my efforts on debugging, and it is proven that there should be no error, typo, mistakes on my code.....

                my last option was to use my common sense.
                Since all the values are being treated as String and absolutely a pure string data type, then i've changed the Object type ArrayList into String type ArrayList.

                Then it solves my problem......

                You may not believe on this kind of scenario, but it really happens on my part.......





                Originally posted by chaarmann
                You can reduce such a structure by making a decision table. And put the long expression in a function!
                Example (Wrong way):
                [HTML]<HTML>
                <FONT color=<% if(a==7) out. write("red") else {if (b==1) out.write("yell ow") else if (b==2) out.write("red" ) else out.write("gree n")}%> Hello World </FONT>
                </HTML>[/HTML]
                Example(Right way):
                [HTML]<%!
                private String getColor(int a, int b)
                {
                if (a==7 || b==2) return "red";
                if (b==1) return "yellow";
                return "green";
                }
                %>
                <HTML>
                <FONT color=<%= getColor(a,b) %> Hello World </FONT>
                </HTML>[/HTML]


                Just a sidenote:
                Every if-else can be reduced to a formula. Fore example:
                Code:
                z=0;
                if (x=7) z=3; else z=5;
                can be written as
                Code:
                z = (x=7) ? 3 : 5
                or as a formula:
                Code:
                z =  Math.abs(Math.signum(x-7)) * 2 + 3;
                The last expression evaluates to
                case x==7 --> 0*2+3 --> 3
                case x !=7 --> 1*2+3 --> 5
                What always come to my mind is that "z = (x=7)? 3: 5"
                But the methods are all created for validation only........

                All conversion, db access, constants, encoders/decoders are in a bean.....
                and all elseif statements are forcely implemented for validations.... ..
                (my last option)

                Like:

                Code:
                if this is, 
                    then do this, 
                else, 
                    do also this,
                Unless, if it is an assignment operation with two options to choose with respect to its (logically tested)value, then this "z = (x=7)? 3: 5" pattern should be used.........

                Thanks for the examples,
                i will try it.....

                JSP beginner,
                sukatoa

                Comment

                • chaarmann
                  Recognized Expert Contributor
                  • Nov 2007
                  • 785

                  #23
                  Originally posted by sukatoa

                  What always come to my mind is that "z = (x=7)? 3: 5"
                  But the methods are all created for validation only........

                  All conversion, db access, constants, encoders/decoders are in a bean.....
                  and all elseif statements are forcely implemented for validations.... ..
                  (my last option)

                  Like:

                  Code:
                  if this is, 
                      then do this, 
                  else, 
                      do also this,
                  Unless, if it is an assignment operation with two options to choose with respect to its (logically tested)value, then this "z = (x=7)? 3: 5" pattern should be used.........

                  Thanks for the examples,
                  i will try it.....

                  JSP beginner,
                  sukatoa
                  There is a mistying on my side, the corrected version is:
                  Code:
                  z = (x==7) ? 3: 5;
                  Please note the double-equal sign after "x", or else it will not work as intended.

                  My example with decision table and function should show you that instead of using:
                  Code:
                  if this is, 
                      then do this, 
                  else, 
                      do also this,
                  it is better to use
                  Code:
                  if this is, 
                      then do this and return, 
                  do also this,
                  This reduces complexity of your if-else nesting.
                  Please note that a "decision table" puts cases which are the same together, which also reduces your if-else-nesting.
                  Like in the example:
                  Code:
                  if this, then do that
                  if this, then do also that
                  is reduced to
                  Code:
                  if this or this then do that
                  .
                  This is just a simple part of this matter. There is a whole course at university only how to make decision tables and it can be a complex matter, but it is worth it. It makes your programs smaller (=faster) and easier to understand.

                  Comment

                  • sukatoa
                    Contributor
                    • Nov 2007
                    • 539

                    #24
                    Im just wondering.....

                    I have observed the behavior of my JavaServer Pages such that it will encounter an IllegalStateExc eption when this application not modified(not roaming around the site) for 20 to 30 mins(minimum).. .. (After logging-in)

                    Why?

                    Can this be avoided?
                    All of my beans are session scoped.....

                    Please let me know experts,
                    sukatoa

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #25
                      Originally posted by sukatoa
                      Im just wondering.....

                      I have observed the behavior of my JavaServer Pages such that it will encounter an IllegalStateExc eption when this application not modified(not roaming around the site) for 20 to 30 mins(minimum).. .. (After logging-in)

                      Why?

                      Can this be avoided?
                      All of my beans are session scoped.....

                      Please let me know experts,
                      sukatoa
                      On a Tomcat Servlet container a Session times out after 30 minutes (by default)
                      if I remember correctly. If you don't want that you should anticipate for it like
                      extending the timeout time for the Session object.

                      kind regards,

                      Jos

                      Comment

                      • sukatoa
                        Contributor
                        • Nov 2007
                        • 539

                        #26
                        thanks for your reply Jos,

                        But, how can i modify its default value?

                        Can you recommend me where to read that one?

                        regards,
                        sukatoa

                        Comment

                        • chaarmann
                          Recognized Expert Contributor
                          • Nov 2007
                          • 785

                          #27
                          Originally posted by sukatoa
                          thanks for your reply Jos,

                          But, how can i modify its default value?

                          Can you recommend me where to read that one?

                          regards,
                          sukatoa
                          If your page does not use session, you can use a jsp-directive. Just write at the top of your jsp-page:
                          Code:
                          <%@ page session="true" %>
                          If you want to use a session and want to modify its timeout (e.g. 4 hours=240 minutes), there are several ways.
                          1.)
                          write in your jsp-page:
                          Code:
                          <% session.setMaxInactiveInterval(240*60); %>
                          2.) write in your web.xml
                          Code:
                          <session-config>
                                 <session-timeout>240</session-timeout>
                          </session-config>
                          The file is there two times: in tomcat's "conf" folder (default value for all applications) or in your application's "WEB-INF" folder. (default value for all jsp-pages of your application)

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #28
                            Originally posted by sukatoa
                            thanks for your reply Jos,

                            But, how can i modify its default value?

                            Can you recommend me where to read that one?

                            regards,
                            sukatoa

                            The place to read would be the Servlet Specification

                            You can change it for the whole application or for a particular session only.
                            When changing for the whole application then you set the value in your application's web.xml with something like

                            Code:
                            <session-config>
                                <session-timeout>100</session-timeout>
                              </session-config>
                            that is 100 minutes

                            For a particular session then that would be
                            int seconds = ....//your required timeout
                            Code:
                             session.setMaxInactiveInterval(seconds);
                            Edit: Since when did chaar get a faster keyboard than mine?

                            Comment

                            • sukatoa
                              Contributor
                              • Nov 2007
                              • 539

                              #29
                              Originally posted by chaarmann
                              If your page does not use session, you can use a jsp-directive. Just write at the top of your jsp-page:
                              Code:
                              <%@ page session="true" %>
                              If you want to use a session and want to modify its timeout (e.g. 4 hours=240 minutes), there are several ways.
                              1.)
                              write in your jsp-page:
                              Code:
                              <% session.setMaxInactiveInterval(240*60); %>
                              2.) write in your web.xml
                              Code:
                              <session-config>
                                     <session-timeout>240</session-timeout>
                              </session-config>
                              The file is there two times: in tomcat's "conf" folder (default value for all applications) or in your application's "WEB-INF" folder. (default value for all jsp-pages of your application)
                              Thanks, that was interesting to read,
                              But, i have modified the web.xml in WEB-INF folder, and i couldn't see

                              Code:
                              <session-config>
                                     <session-timeout>240</session-timeout>
                              </session-config>
                              I expect that it was there, yet, it should be added.....

                              So, Here is the content of the web.xml

                              Code:
                              <?xml version="1.0" encoding="ISO-8859-1"?>
                              <!--
                                Copyright 2004 The Apache Software Foundation
                              
                                Licensed under the Apache License, Version 2.0 (the "License");
                                you may not use this file except in compliance with the License.
                                You may obtain a copy of the License at
                              
                                    http://www.apache.org/licenses/LICENSE-2.0
                              
                                Unless required by applicable law or agreed to in writing, software
                                distributed under the License is distributed on an "AS IS" BASIS,
                                WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
                                See the License for the specific language governing permissions and
                                limitations under the License.
                              -->
                              
                              <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
                                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
                                  version="2.4">
                              
                                <display-name>Welcome to Tomcat</display-name>
                                <description>
                                   Welcome to Tomcat
                                </description>
                              
                              <!-- JSPC servlet mappings start -->
                              
                                  <servlet>
                                      <servlet-name>org.apache.jsp.index_jsp</servlet-name>
                                      <servlet-class>org.apache.jsp.index_jsp</servlet-class>
                                  </servlet>
                              
                                  <servlet-mapping>
                                      <servlet-name>org.apache.jsp.index_jsp</servlet-name>
                                      <url-pattern>/index.jsp</url-pattern>
                                  </servlet-mapping>
                              
                              <!-- JSPC servlet mappings end -->
                              
                              </web-app>
                              Can you point me where to put your suggested xml code in the xml above?
                              This was my first time to modify such config file.....

                              P.S i just don't want to commit mistake specially on this type of file... because i believe that if there is something wrong with this, i can't trace whatever error/bug may exists later......

                              sukatoa

                              Comment

                              • chaarmann
                                Recognized Expert Contributor
                                • Nov 2007
                                • 785

                                #30
                                Originally posted by sukatoa
                                Can you point me where to put your suggested xml code in the xml above?
                                This was my first time to modify such config file.....

                                sukatoa
                                Inside your web-app element. (line 22)

                                Comment

                                Working...