By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,275 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,275 IT Pros & Developers. It's quick & easy.

JSP Getting Started

100+
P: 539
I have a subject that covers JSP(new to me), accessing database through JSP(new to me), applets, UML(also new to me)

So, as im learning java, all i did to learn faster is i've created some applications(with sense). With my style of self learning, i hope it could be effective in JSP too....

I like to be advanced on the planned topic to be discussed by our instructor(He is not a certified Java Programmer though)

So now, all i know about JSP is the following:
-basic tags
expression <%= something%>
declaration <%! something%>
i dont know the term <% something %> I think it is an invocation
equivalent to java (Please correct me on that part)

-Setting up tomcat.
-The ROOT folder
-WEB-INF theoritically knew where to put classes/jars etc....
(though im not tested them yet)
-getting the value of a html form through request.getParameter("form input-type name)

Those are basics obviously...( I learned them through surfing)

And im planning to create a simple application.
What comes on my mind is to reimplement my previous project (Simple Hotel FrontDesk System) into Online Hotel Reservation System
-the registration is obviously done by the hotel frontdesk only....
-Maybe the BlackList feature would be added if i knew the real ( Deep )

Actually, we have proposed a project "Online Entrance Exam" to our instructor and he likes it... so he approved our proposal.....( Not Thesis )

But before we start banging that project, deadline is on October ( Final week in this semester )

I like to grasp the essentials of JSP and also the other techniques, new way, and theory behind that architectural design for web application platform....
(Please correct me on the phrase above)

Actually, the faculty decided to order a J2EE book from Sun( i think the book base is in Singapore) for about 2500php or $56.00

Unfortunately, that book is expected to be arrived on midterms.... unbelievable scenario(but that was true)....

I did attempt to buy a new book instead of waiting that precious book, but i doubt for having such book....(for $40.99)....

So i decided to wait that precious book, yet learning in advance from this forum and the other web resources....

What a long story :)

Well, anyway,

Here is what i suppose to know in JSP as im building my experimental application for the supplement on my final project( Not thesis though )

I like to know about the JSP Beans,

for example, i have a java class that contains setter and getter methods,
i like to jar them, unfortunately, i didn't found the right tutorial for it on the web...

so can you post a helpful url that would teach me how to do that?
and also, about how to access the jar file from the jsp container( doubt of terms )Please correct me....

Or can you show an example?
For example, i have a jar file in WEB-INF/lib/JARZKY.jar
and the content is com\myclass.class(with setSetter() and getGetter() methods)

How can you invoke that class through JSP?


A sincere Java Lover,
sukatoa
Jul 3 '08 #1
Share this Question
Share on Google+
35 Replies


10K+
P: 13,264
First read this one.
Then get this one which is free from sun for the more serious stuff.
Jul 3 '08 #2

100+
P: 539
First read this one.
Then get this one which is free from sun for the more serious stuff.
Thanks for the URL r035198x....
Im on my way.....
Jul 4 '08 #3

100+
P: 539
Thanks for the URL r035198x....
Im on my way.....
I have created a jar named Checker.jar
saved on WEB-INF\lib\

my problem is i cannot access the Checker.jar
with content dma.Checker.class

i've attempt:

<jsp:useBean id="Checker" scope="page" class="Checker" />

attempting to replace class into jar ==> invalid attribute

But i really have no idea how to fix this.....

Can you show me an example about this?
The url above does only explains how to deal with classes...( not jar )
Jul 4 '08 #4

100+
P: 539
Is this possible?

I would like to add a text file together with the classes folder inside WEB-INF
And that text-file is the source to be extracted for calculations and i will try to open that textfile from my java-bean.... possible? or not?

for example,
I have a text file that contains the list of all countries....
That is really disapointing on my part that all the countries will be coded on
bean. so i decided not to do it inside the code, instead on the database....
But for that it is just a constant data, having it on the database will add
another table and it will consume another bean that has 100 lines of code
(Approximately). Before attempting it into database, i need your advice, and
the answer on the above questions experts....

Looking forward on your replies,
sukatoa....
Jul 11 '08 #5

100+
P: 539
Im currently doing some experiments in JSP........

and I fail to debug my code.....

Here is the stacktrace:
Expand|Select|Wrap|Line Numbers
  1. org.apache.jasper.JasperException: For input string: ""
  2.     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370)
  3.     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
  4.     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
  5.     javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  6.  
  7.  
  8. root cause 
  9. java.lang.NumberFormatException: For input string: ""
  10.     java.lang.NumberFormatException.forInputString(Unknown Source)
  11.     java.lang.Integer.parseInt(Unknown Source)
  12.     java.lang.Integer.valueOf(Unknown Source)
  13.     org.apache.jsp.home.guests.Validator_jsp.checkAccountInformation(org.apache.jsp.home.guests.Validator_jsp:63)
  14.     org.apache.jsp.home.guests.Validator_jsp._jspService(org.apache.jsp.home.guests.Validator_jsp:270)
  15.     org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
  16.     javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  17.     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
  18.     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
  19.     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
  20.     javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
I am curious about that inputString.........

Here is the JSP code.

Expand|Select|Wrap|Line Numbers
  1. <%@ page 
  2.     contentType="text/html; 
  3.     charset=iso-8859-1" 
  4.     language="java" 
  5.     errorPage=""
  6.  
  7.     import = "beans.specific.constants.ImageRefGenerator"
  8.     import = "beans.reusable.checker.EmailChecker"
  9.     import = "java.util.ArrayList"
  10.     import = "java.util.GregorianCalendar"
  11. %>
  12. <jsp:useBean id="signupcookie" class="beans.specific.cookies.SignUpCookie" scope="session" />
  13.  
  14. <%!    
  15.     ImageRefGenerator img = new ImageRefGenerator();
  16.     String SenderLocation = "http://localhost:8080/home/guests/register.jsp";
  17.     EmailChecker checkemail = new EmailChecker();
  18.     ArrayList<String> reminder = new ArrayList<String>();
  19.  
  20.     private final boolean checkVerification(String confirm,String reference,boolean strict){
  21.         if(confirm.length()>0){
  22.             if(!strict)
  23.                 return confirm.equalsIgnoreCase(reference);
  24.             else
  25.                 return confirm.equals(reference);
  26.         }else{
  27.             return false;
  28.         }
  29.     }
  30.  
  31.     private final boolean checkMail(String email){
  32.         checkemail.resetValue(email);
  33.         return checkemail.checkStoredEmail();
  34.     }
  35.  
  36.     private final boolean checkAccountInformation(String username,
  37.                                                   String password,
  38.                                                   String verifypassword,
  39.                                                   String emailaddress,
  40.                                                   String verifyemailaddress,
  41.                                                   String creditcardnumber){
  42.         boolean confirmer = true;
  43.         if(username.length()==0){
  44.             reminder.add("username");
  45.             confirmer = false;
  46.         }
  47.         if(password.length()<6){
  48.             reminder.add("password");
  49.             confirmer = false;
  50.         }    
  51.         if(!checkVerification(password,verifypassword,true)){
  52.             reminder.add("verifypassword");
  53.             confirmer = false;
  54.         }    
  55.         if(!checkMail(emailaddress)){
  56.             reminder.add("email");
  57.             confirmer = false;
  58.         }    
  59.         if(!checkVerification(emailaddress,verifyemailaddress,true)){
  60.             reminder.add("verifyemail");
  61.             confirmer = false;
  62.         }    
  63.         if(Integer.valueOf(creditcardnumber)<16){
  64.             reminder.add("ccnumber");
  65.             confirmer = false;
  66.         }
  67.         return confirmer;
  68.     }
  69.  
  70.     private final boolean checkMemberInformation(String surname,
  71.                                                  String firstname,
  72.                                                  String middlename,
  73.                                                  String homeadd,
  74.                                                  String birthdate,
  75.                                                  String gender,
  76.                                                  String country,
  77.                                                  String passport,
  78.                                                  String placeissue){
  79.         boolean confirmer = true;
  80.         if(surname.length()==0){
  81.             reminder.add("surname");
  82.             confirmer = false;
  83.         }
  84.         if(firstname.length()==0){
  85.             reminder.add("firstname");
  86.             confirmer = false;
  87.         }
  88.         if(middlename.length()==0){
  89.             reminder.add("middlename");
  90.             confirmer = false;
  91.         }
  92.         if(homeadd.length()==0){
  93.             reminder.add("homeaddress");
  94.             confirmer = false;
  95.         }
  96.         if(!checkBirthDate(birthdate)){
  97.             reminder.add("birthdate");
  98.             confirmer = false;
  99.         }            
  100.         if(gender.equals("Select")){
  101.             reminder.add("gender");
  102.             confirmer = false;
  103.         }
  104.         if(country.equals("Select Country")){
  105.             reminder.add("country");
  106.             confirmer = false;
  107.         }
  108.         if(passport.length()==0){
  109.             if(placeissue.length()>0){
  110.                 reminder.add("passportnumber");
  111.                 confirmer = false;
  112.             }
  113.         }else{
  114.             if(placeissue.length()==0){
  115.                 reminder.add("placeissue");
  116.                 confirmer = false;
  117.             }
  118.         }
  119.         return confirmer;                             
  120.      }
  121.  
  122.      private final boolean checkContactInformation(String landline,
  123.                                                    String mobile,
  124.                                                    String fax,
  125.                                                    String alteremail){
  126.          boolean ret = true;
  127.          if(landline.length()!=0||mobile.length()!=0||
  128.             fax.length()!=0||alteremail.length()!=0){
  129.              if(alteremail.length()>0){
  130.                  if(!checkMail(alteremail)){
  131.                      reminder.add("alteremail");
  132.                      ret = false;
  133.                  }
  134.              }                 
  135.          }else{
  136.              reminder.add("nonecontactinfo");
  137.              ret = false;                                              
  138.          }
  139.         return ret;
  140.      }
  141.  
  142.      private final boolean checkBirthDate(String data){
  143.          String month = data.split("-")[0];
  144.          Integer day = Integer.valueOf(data.split("-")[1]);
  145.          Integer year = Integer.valueOf(data.split("-")[2]);
  146.          boolean ret = true;
  147.  
  148.          if(month.equals("February")){
  149.              if(new GregorianCalendar().isLeapYear(year))
  150.                  ret=day>28?false:ret;
  151.              else
  152.                  ret=day>29?false:ret;
  153.          }
  154.  
  155.          if(month.equals("April")||month.equals("June")||month.equals("September")||
  156.             month.equals("November"))
  157.                 ret = day==31?false:ret;
  158.          return ret;
  159.      }
  160.  
  161.     private final void VoidVerification(){
  162.         reminder.add("remoteuser");
  163.     }
  164. %>
  165.  
  166. <%
  167.     signupcookie.resetInstance();
  168.  
  169.     String username = request.getParameter("username");
  170.     String password = request.getParameter("password");
  171.     String checkpassword = request.getParameter("checkpassword");
  172.     String emailaddress = request.getParameter("emailaddress");
  173.     String checkemail = request.getParameter("checkemail");
  174.     String creditcard = request.getParameter("creditcard");
  175.  
  176.     String surname = request.getParameter("surname");
  177.     String firstname = request.getParameter("firstname");
  178.     String middlename = request.getParameter("middlename");
  179.  
  180.     String month = request.getParameter("month");
  181.     String day = request.getParameter("day");
  182.     String year = request.getParameter("year");    
  183.     String gender = request.getParameter("gender");
  184.     String homeaddress = request.getParameter("homeaddress");
  185.     String companyaddress = request.getParameter("companyaddress");
  186.  
  187.     String country = request.getParameter("country");
  188.     String passportnumber = request.getParameter("passportnumber");
  189.     String passportissue = request.getParameter("passportissue");
  190.  
  191.     String landline = request.getParameter("landline");
  192.     String mobile = request.getParameter("mobile");
  193.     String faxnumber = request.getParameter("faxnumber");
  194.     String altermail = request.getParameter("altermail");
  195.  
  196.     String confirmation = request.getParameter("confirmation");
  197.     String ref = request.getParameter("ref");
  198.  
  199.     signupcookie.setUsername(username);
  200.     signupcookie.setEmailAddress(emailaddress);
  201.  
  202.     signupcookie.setSurname(surname);
  203.     signupcookie.setFirstname(firstname);
  204.     signupcookie.setMiddlename(middlename);
  205.     signupcookie.setBirthDate(month,day,year);    
  206.     signupcookie.setGender(gender);
  207.     signupcookie.setHomeAddress(homeaddress);
  208.     signupcookie.setCompanyAddress(companyaddress);
  209.  
  210.     signupcookie.setCountry(country);
  211.     signupcookie.setPassportNumber(passportnumber);
  212.     signupcookie.setPassportIssue(passportissue);
  213.  
  214.     signupcookie.setLandline(landline);
  215.     signupcookie.setMobile(mobile);
  216.     signupcookie.setFaxNumber(faxnumber);
  217.     signupcookie.setAlterEmail(altermail);
  218.  
  219.     String validValue = img.getValue(ref);
  220.  
  221.     boolean indicator = true;
  222.     String brtdate = month.concat("-"+day).concat("-"+year);
  223.  
  224.  
  225.     if(!checkAccountInformation(username,password,checkpassword,
  226.                                emailaddress,checkemail,creditcard)){
  227.         indicator = false;                           
  228.     }
  229.     if(!checkMemberInformation(surname,firstname,middlename,homeaddress,brtdate,
  230.                                gender,country,passportnumber,passportissue)){
  231.         indicator = false;
  232.     }
  233.     if(!checkVerification(confirmation,validValue,false)){
  234.         VoidVerification();
  235.         indicator = false;
  236.     }
  237.  
  238.     if(!indicator){
  239.         response.sendRedirect(SenderLocation);
  240.     }
  241. %>
  242.  
Im loosing hope with this problem.....

Im using DreamWeaverMX, manual set-up of Apache Tomcat........

Can you experts tell me where is it on the code?
Or can you suggest any alternatives for this?

waiting,
sukatoa
Aug 3 '08 #6

100+
P: 539
Im always late to realize.........

ArrayList indexOf(Object o) uses the equals method that can't be used to compare null values........

Gotcha!!!!!!!

Expand|Select|Wrap|Line Numbers
  1. final boolean hasReminder(String exists){
  2.         boolean ret = false;
  3.         reminderloop:
  4.         for(String rem:reminder){
  5.             if(rem.equals(exists)){
  6.                 ret = true;
  7.                 break reminderloop;
  8.             }
  9.         }
  10.         return ret;
  11.     }
I wonder what implementation did lastIndexOf method used in ArrayList?
Is that only uses the reverse value arrangement then apply the indexOf method?
Aug 3 '08 #7

Expert 10K+
P: 11,448
I wonder what implementation did lastIndexOf method used in ArrayList?
Is that only uses the reverse value arrangement then apply the indexOf method?
If you wonder how those standard classes implemented things go to your jdk
directory and find the file src.zip. It's a zip file that contains all the source code
files for all the publicly available classes in your Java installation.

kind regards,

Jos
Aug 3 '08 #8

100+
P: 539
Just a quick,

I've planned to implement all the necessary validations using JSP,

Now, may i know what are the disadvantages of doing such thing?
except for the passing parameters?( takes a little bit of time ) through form.....

Unlike JS, that it runs on the browser as it validates the form just in time.......
Aug 6 '08 #9

100+
P: 283
Just a quick,

I've planned to implement all the necessary validations using JSP,

Now, may i know what are the disadvantages of doing such thing?
except for the passing parameters?( takes a little bit of time ) through form.....

Unlike JS, that it runs on the browser as it validates the form just in time.......
I think by validations you mean form validations? I normally do it in the client side as well as in the server side(just to be doubly sure, and thats the way it should be :)). I dont see it as a disadvantage because those are the things a web developer should do, lets say what happens when a ignorant(or any user) turns the javaScript off in the browser? I as a web developer ensure that the user doesnt skip the mandatory things that he needs to fill. I dont think that it comes down to performance, its a securuty thing for me.

regards,

ajos
Aug 6 '08 #10

10K+
P: 13,264
I think by validations you mean form validations? I normally do it in the client side as well as in the server side(just to be doubly sure, and thats the way it should be :)). I dont see it as a disadvantage because those are the things a web developer should do, lets say what happens when a ignorant(or any user) turns the javaScript off in the browser? I as a web developer ensure that the user doesnt skip the mandatory things that he needs to fill. I dont think that it comes down to performance, its a securuty thing for me.

regards,

ajos
Yes, always do validations both client side and server side. It's one of the golden rules of web programming.
Aug 6 '08 #11

Expert 10K+
P: 11,448
Yes, always do validations both client side and server side. It's one of the golden rules of web programming.
Just a question: while I do understand that just client side validation would be
silly (no Javascript, request synthesis etc.) but what's wrong with just having
server side validation? (except for the server load).

kind regards,

Jos

ps. sorry for the hijack.
Aug 6 '08 #12

10K+
P: 13,264
If there's client side validations as well then the user doesn't have to wait for the page to be posted to the server just to be told that they left a field blank.
Aug 6 '08 #13

Expert 10K+
P: 11,448
If there's client side validations as well then the user doesn't have to wait for the page to be posted to the server just to be told that they left a field blank.
Ah, ok; so the reason for double sided validation is just speed. But then you have
to keep both validations in sync with each other. (just wondering)

kind regards,

Jos
Aug 6 '08 #14

10K+
P: 13,264
Ah, ok; so the reason for double sided validation is just speed. But then you have
to keep both validations in sync with each other. (just wondering)

kind regards,

Jos
Can become a nightmare right? Generally the server side validator must not assume any client side validators. So his job is easier. He doesn't need to sync with anyone. The client side validator only needs to do the best he can to make sure that the server code is not called unnccessarily.
Aug 6 '08 #15

100+
P: 539
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.......
Aug 9 '08 #16

100+
P: 539
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?
Aug 9 '08 #17

100+
P: 539
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

Expand|Select|Wrap|Line Numbers
  1. ArrayList<String> ret = new ArrayList<String>();
  2. for(Object obj: ObjectList){
  3.        ret.add((String)obj);
  4. }
  5. 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........
Aug 9 '08 #18

100+
P: 539
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
Aug 10 '08 #19

10K+
P: 13,264
...

The conflict occurs, in any time interval, the conversion fails........
...
What was the exact error message?
Aug 11 '08 #20

Expert 100+
P: 785
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("yellow") else if (b==2) out.write("red") else out.write("green")}%> 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:
Expand|Select|Wrap|Line Numbers
  1. z=0;
  2. if (x=7) z=3; else z=5;
can be written as
Expand|Select|Wrap|Line Numbers
  1. z = (x=7) ? 3 : 5
or as a formula:
Expand|Select|Wrap|Line Numbers
  1. 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
Aug 11 '08 #21

100+
P: 539
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.getParameter() 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,retypeemail 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.......





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("yellow") else if (b==2) out.write("red") else out.write("green")}%> 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:
Expand|Select|Wrap|Line Numbers
  1. z=0;
  2. if (x=7) z=3; else z=5;
can be written as
Expand|Select|Wrap|Line Numbers
  1. z = (x=7) ? 3 : 5
or as a formula:
Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. if this is, 
  2.     then do this, 
  3. else, 
  4.     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
Aug 11 '08 #22

Expert 100+
P: 785

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:

Expand|Select|Wrap|Line Numbers
  1. if this is, 
  2.     then do this, 
  3. else, 
  4.     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:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. if this is, 
  2.     then do this, 
  3. else, 
  4.     do also this,
it is better to use
Expand|Select|Wrap|Line Numbers
  1. if this is, 
  2.     then do this and return, 
  3. 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:
Expand|Select|Wrap|Line Numbers
  1. if this, then do that
  2. if this, then do also that 
is reduced to
Expand|Select|Wrap|Line Numbers
  1. 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.
Aug 12 '08 #23

100+
P: 539
Im just wondering.....

I have observed the behavior of my JavaServer Pages such that it will encounter an IllegalStateException 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
Sep 3 '08 #24

Expert 10K+
P: 11,448
Im just wondering.....

I have observed the behavior of my JavaServer Pages such that it will encounter an IllegalStateException 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
Sep 3 '08 #25

100+
P: 539
thanks for your reply Jos,

But, how can i modify its default value?

Can you recommend me where to read that one?

regards,
sukatoa
Sep 4 '08 #26

Expert 100+
P: 785
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:
Expand|Select|Wrap|Line Numbers
  1. <%@ 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:
Expand|Select|Wrap|Line Numbers
  1. <% session.setMaxInactiveInterval(240*60); %>
2.) write in your web.xml
Expand|Select|Wrap|Line Numbers
  1. <session-config>
  2.        <session-timeout>240</session-timeout>
  3. </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)
Sep 4 '08 #27

10K+
P: 13,264
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

Expand|Select|Wrap|Line Numbers
  1. <session-config>
  2.     <session-timeout>100</session-timeout>
  3.   </session-config>
that is 100 minutes

For a particular session then that would be
int seconds = ....//your required timeout
Expand|Select|Wrap|Line Numbers
  1.  session.setMaxInactiveInterval(seconds);
Edit: Since when did chaar get a faster keyboard than mine?
Sep 4 '08 #28

100+
P: 539
If your page does not use session, you can use a jsp-directive. Just write at the top of your jsp-page:
Expand|Select|Wrap|Line Numbers
  1. <%@ 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:
Expand|Select|Wrap|Line Numbers
  1. <% session.setMaxInactiveInterval(240*60); %>
2.) write in your web.xml
Expand|Select|Wrap|Line Numbers
  1. <session-config>
  2.        <session-timeout>240</session-timeout>
  3. </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

Expand|Select|Wrap|Line Numbers
  1. <session-config>
  2.        <session-timeout>240</session-timeout>
  3. </session-config>
I expect that it was there, yet, it should be added.....

So, Here is the content of the web.xml

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!--
  3.   Copyright 2004 The Apache Software Foundation
  4.  
  5.   Licensed under the Apache License, Version 2.0 (the "License");
  6.   you may not use this file except in compliance with the License.
  7.   You may obtain a copy of the License at
  8.  
  9.       http://www.apache.org/licenses/LICENSE-2.0
  10.  
  11.   Unless required by applicable law or agreed to in writing, software
  12.   distributed under the License is distributed on an "AS IS" BASIS,
  13.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.   See the License for the specific language governing permissions and
  15.   limitations under the License.
  16. -->
  17.  
  18. <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  19.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  21.     version="2.4">
  22.  
  23.   <display-name>Welcome to Tomcat</display-name>
  24.   <description>
  25.      Welcome to Tomcat
  26.   </description>
  27.  
  28. <!-- JSPC servlet mappings start -->
  29.  
  30.     <servlet>
  31.         <servlet-name>org.apache.jsp.index_jsp</servlet-name>
  32.         <servlet-class>org.apache.jsp.index_jsp</servlet-class>
  33.     </servlet>
  34.  
  35.     <servlet-mapping>
  36.         <servlet-name>org.apache.jsp.index_jsp</servlet-name>
  37.         <url-pattern>/index.jsp</url-pattern>
  38.     </servlet-mapping>
  39.  
  40. <!-- JSPC servlet mappings end -->
  41.  
  42. </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
Sep 4 '08 #29

Expert 100+
P: 785
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)
Sep 5 '08 #30

100+
P: 539
Thank you Jos, r035198x and chaarmann.....
Sep 5 '08 #31

100+
P: 539
Now, new problem i have noticed on my page,

I can't compare the values between the content of my bean and the parameter value received in my controller,

How come? I remembered the Collator class or converting the two values into hashcode then compare, but before i'll start banging here, please let me know why is it happening?

curious,
sukatoa
Sep 7 '08 #32

100+
P: 539
I've always encounter IllegalStateException when the server executes the code below,

Expand|Select|Wrap|Line Numbers
  1. <%
  2.     String date = request.getParameter("date");
  3.     String force = request.getParameter("force");
  4.     String usr = cookie.getCookie();
  5.     boolean exec=true;
  6.     boolean exist=true;
  7.  
  8.     if(cookie.getCookie().equals(""))
  9.         response.sendRedirect(home);
  10.     if(!initDB())
  11.         response.sendRedirect(home+"error/error.jsp?path="+home+
  12.                 "member/message/mymessage.jsp");    
  13.  
  14.     if(force==null){
  15.         if(exists(usr,date)){
  16.             if(isModified(usr,date).equals("no")){
  17.                 exec=false;
  18.             }
  19.         }else{
  20.             exist=false;
  21.         }    
  22. //If i will going to remove this else, no illegalstateexception occured
  23.     }else{
  24.         if(force.equals("back"))
  25.             response.sendRedirect(home+"member/member.jsp");
  26.     }
  27.  
  28.     if(exec){
  29.         if(!delete(usr,date)){
  30.             response.sendRedirect(home+"error/error.jsp?path="+home+
  31.                     "member/message/delete.jsp?date="+date+"&force="+force);    
  32.         }else
  33.             response.sendRedirect(home+"member/member.jsp");
  34.     }
  35. %>
stacktrace from apache tomcat logs
Expand|Select|Wrap|Line Numbers
  1. java.lang.IllegalStateException
  2.     at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:432)
  3.     at org.apache.jsp.home.member.message.delete_jsp._jspService(org.apache.jsp.home.member.message.delete_jsp:146)
  4.     at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
  5.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  6.     at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
  7.     at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
  8.     at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
  9.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  10.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
  11.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
  12.     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
  13.     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
  14.     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
  15.     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
  16.     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
  17.     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
  18.     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:868)
  19.     at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:663)
  20.     at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
  21.     at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
  22.     at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
  23.     at java.lang.Thread.run(Unknown Source)
  24.  
Could you experts realize me how to avoid such exception?
and any comment about the code above is greatly accepted....
worst/better or suggestions about redesigning the code, it will be greatly appreciated.....

This was my first time to dive into a real application using JSP

get stucked,
sukatoa
Sep 7 '08 #33

100+
P: 539
Gotcha!!!

if forgot to think about MVC architecture......

Controller 1
Expand|Select|Wrap|Line Numbers
  1. <%
  2.     String date = request.getParameter("date");
  3.     String force = request.getParameter("force");
  4.     String usr = cookie.getCookie();
  5.     boolean exec=true;
  6.     boolean exist=true;
  7.  
  8.     if(cookie.getCookie().equals(""))
  9.         response.sendRedirect(home);
  10.     if(!initDB())
  11.         response.sendRedirect(home+"error/error.jsp?path="+home+
  12.                 "member/message/mymessage.jsp");    
  13.  
  14.     if(force==null){
  15.         if(exists(usr,date)){
  16.             if(isModified(usr,date).equals("no")){
  17.                 exec=false;
  18.             }
  19.         }else{
  20.             exist=false;
  21.         }    
  22.     }
  23.  
  24.     if(exec){
  25.         if(!delete(usr,date)){
  26.             response.sendRedirect(home+"error/error.jsp?path="+home+
  27.                     "member/message/delete.jsp?date="+date+"&force="+force);    
  28.         }else
  29.             response.sendRedirect(home+"member/message/mymessage.jsp");
  30.     }
  31. %>
Controller 2
Expand|Select|Wrap|Line Numbers
  1. <%
  2.     String date = request.getParameter("date");
  3.     String force = request.getParameter("force");
  4.  
  5.     if(cookie.getCookie().equals(""))
  6.         response.sendRedirect(home);
  7.     if(!initDB())
  8.         response.sendRedirect(home+"error/error.jsp?path="+home+
  9.                 "member/message/confirm_delete.jsp?date="+date+"&force="+force);
  10.  
  11.     if(force!=null&&force.equals("yes")){
  12.         if(!delete(cookie.getCookie(),date)){
  13.             response.sendRedirect(home+"error/error.jsp?path="+home+
  14.                 "member/message/confirm_delete.jsp?date="+date+"&force="+force);
  15.         }
  16.     }
  17.     response.sendRedirect(home+"member/message/mymessage.jsp");
  18. %>
always late to realize.....
Sep 7 '08 #34

100+
P: 539
I doubt about the following scenario,

Im going to add the naming-factory-dbcp.jar on my WEB-INF/lib to be able to run my beans that implements tablehandler classes.... (accessing database)
and im going to test it on some jsp hosting site....

What if the naming-factory-dbcp.jar already been exists in there server?
(found in common/lib/ directory)

So, that means, 2 the same jar appears,
What will happened when im going to use transaction with my tablehandler?
Is there any predictable conflicts? or something?

But before im going to test it, i wan't to know from you experts....
Sep 13 '08 #35

Expert 100+
P: 785
I doubt about the following scenario,

Im going to add the naming-factory-dbcp.jar on my WEB-INF/lib to be able to run my beans that implements tablehandler classes.... (accessing database)
and im going to test it on some jsp hosting site....

What if the naming-factory-dbcp.jar already been exists in there server?
(found in common/lib/ directory)

So, that means, 2 the same jar appears,
What will happened when im going to use transaction with my tablehandler?
Is there any predictable conflicts? or something?

But before im going to test it, i wan't to know from you experts....
The first jar-file included is the one from your common/lib directory.
If you try to add any class (contained in second jar) later on that was included already in your first jar, then this second jar will not be included and you get an error message:
15.09.2008 08:54:31 org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(D:\myDir\webapps\myApp\WEB-INF\lib\myJar.jar) - jar not loaded. Offending class: myPackage/MyClass.class
Sep 15 '08 #36

Post your reply

Sign in to post your reply or Sign up for a free account.