wood burning stoves*
The moose likes Servlets and the fly likes Servlet API 2.2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet API 2.2" Watch "Servlet API 2.2" New topic
Author

Servlet API 2.2

Rashmi Trivedi
Ranch Hand

Joined: Aug 13, 2001
Posts: 60
Hi all
I used to have Servlet API 2.1 and in that i used to use HttpSession class to get session id:
HttpSession session = request.getSession(true);
Once i get session, i used to put values on that session:
session.putValue ("staffID", staffID);
Now i have downloaded Servlet API 2.2 and session.putValue and session.getValue are depcrated. Use session.setAttribute and session.getAttribute() instead.
I tried that but my program didn't work. When i submit my logon page, it suppose to bring another page after database look up and pin check, it says page cannot be displayed. I debug the servlet and it fails at session.setAttribute and session.getAttribute.
Any thoughts on this will be appreciated. Looking forward to hearing from you guys.
Regards,
Rashmi
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
You may need to post some code. what do you mean by "it fails at session.setAttribute and session.getAttribute."? It compiles, so you have access to these methods. Did it give you an error in the call to this methods or what?

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

How about this:

Compiling against an API version is one thing, but what servlet runner are you using?

For example, if you compile a servlet against the servlet 2.3 spec, and you use a method found only in that spec, but then you use Tomcat 3.2.1 to run it... you will throw an error during runtime.

Rashmi Trivedi
Ranch Hand

Joined: Aug 13, 2001
Posts: 60
Dear Mike
Thank you for your input. I appreciate it.
I am using IBM Visua Age for Java 3.5 Application server and in that you can add servlet API tool kit to write servlet.
That servlet API is 2.1. I have downloaded Servlet API 2.2 from sun web site and add this new API in to WebSphere. You can do this very easily. All you have to do is import those java files from the folder to the package and VAJ will compil for you. That part is OK. Servlet compiles OK too but it doesn't go furthure when it reads setAttribute and it suppose to do response.redirect("URL") but it fails and brings "Page canno be displayed".
I don't know what exactly is wrong with this code. If i change it 2.1 it works fine. Here is the doPost method in which i used setAttribute and change it back to setValue.

Hope this make sense.

public void doPost (HttpServletRequest request,
HttpServletResponse response) throws IOException {
String queryString = "select password from intentiastaff where staffid = ?";
String insertString = " insert into intentiastaff values (" +
"?, ?, ?, ?, ?, " + // 1 = Staff ID
// 2 = passwrod
// 3 = Family Name
// 4 = First Name
// 5 = Address
"?, ?, ?, ?, ?, " + // 6 = Subrub
// 7 = State
// 8 = PostCode
// 9 = Date Of Birth
// 10= Phone No
"?, ?, ?)"; // 11 = Mobile No
// 12 = Administrator
// 13 = Locked
try {
response.setContentType ("text/html"); // returns text fields
// Get handle to output stream
PrintWriter out = response.getWriter ();
HttpSession session = request.getSession (true);
//out.println(session);
//session.getValue() is deprecated. USE session.getAttribute() instead
//String administratorID = (String)session.getAttribute ("staffID");
String administratorID = (String)session.getValue ("staffID");
//out.println(administratorID);

//session.getValue() is deprecated. USE session.getAttribute() instead
//String administrator = (String)session.getAttribute ("administrator");
String administrator = (String)session.getValue ("administrator");
//out.println(administrator);

if (administratorID == null) {
out.println ("<HTML>");
out.println ("<HEAD><TITLE>Inentia Change Staff Servlet</TITLE>");
out.println ("<SCRIPT LANGUAGE=\"JavaScript\" ");
out.println ("TYPE=\"text/javascript\">");
out.println ("</SCRIPT>");
out.println ("</HEAD>");
out.println ("<BODY bgcolor=\"white\">");
out.println ("<H2>You must Logon as an Administrator First.</H2>");
out.println ("</BODY>");
out.println ("</HTML>");

out.close ();
return;
} // end if
if (administrator.equals("Y")) {
// good
} // end if
else {

out.println ("<HTML>");
out.println ("<HEAD><TITLE>Inentia Change Staff Servlet</TITLE>");
out.println ("<SCRIPT LANGUAGE=\"JavaScript\" ");
out.println ("TYPE=\"text/javascript\">");
out.println ("</SCRIPT>");
out.println ("</HEAD>");
out.println ("<BODY bgcolor=\"white\">");
out.println ("<H2>Only Administrators can Query the Staff.</H2>");
out.println ("</BODY>");
out.println ("</HTML>");
out.close ();
return;
} // end else
// Get data from request object (form)
String staffID = request.getParameter ("staffid");

String password = request.getParameter ("password");
String FamilyName = request.getParameter ("familyname");
String FirstName = request.getParameter ("firstname");
String Address = request.getParameter ("streetadd");
String Suburb = request.getParameter ("subrub");
String State = request.getParameter ("state");
String PostCode = request.getParameter ("postcode");
String Year = request.getParameter ("year");
String Month = request.getParameter ("month");
String Day = request.getParameter ("day");
String PhoneNo = request.getParameter ("telephone");
String MobileNo = request.getParameter ("mobile");

String Admin = request.getParameter("administrator");

if (Admin == null) {
Admin = "N";
}// end if
String LOCKED = request.getParameter("locked");

if (LOCKED == null) {
LOCKED = "N";
}// end if
Date DATEOFBIRTH = Date.valueOf(Year.trim() + "-" + Month.trim() + "-" + Day.trim());

String errorMessage = " ";
PreparedStatement prepQueryStmt = dbConn.prepareStatement (queryString);
prepQueryStmt.setString (1, staffID);
ResultSet rs = prepQueryStmt.executeQuery ();
if (rs.next ()) {
// already on file
out.println ("<HTML>");
out.println ("<HEAD><TITLE>Inentia Change Staff Servlet</TITLE>");
out.println ("<SCRIPT LANGUAGE=\"JavaScript\" ");
out.println ("TYPE=\"text/javascript\">");
out.println ("</SCRIPT>");
out.println ("</HEAD>");
out.println ("<BODY bgcolor=\"white\">");
out.println("Staff ID is already on File.");
out.println ("</BODY>");
out.println ("</HTML>");
//processErrorTemplate (out, "Staff ID is already on file.");
out.close ();
return;
} // end if
PreparedStatement prepInsertStmt = dbConn.prepareStatement (insertString);
prepInsertStmt.setString (1, staffID);
prepInsertStmt.setString (2, password);
prepInsertStmt.setString (3, FamilyName);
prepInsertStmt.setString (4, FirstName);
prepInsertStmt.setString (5, Address);
prepInsertStmt.setString (6, Suburb);
prepInsertStmt.setString (7, State);
prepInsertStmt.setString (8, PostCode);
prepInsertStmt.setDate (9, DATEOFBIRTH);
prepInsertStmt.setString (10, PhoneNo);
prepInsertStmt.setString (11, MobileNo);
prepInsertStmt.setString (12, Admin);
prepInsertStmt.setString (13, LOCKED);
prepInsertStmt.executeUpdate ();
out.println ("<HTML>");
out.println ("<HEAD><TITLE>Inentia Change Staff Servlet</TITLE>");
out.println ("<SCRIPT LANGUAGE=\"JavaScript\" ");
out.println ("TYPE=\"text/javascript\">");
out.println ("</SCRIPT>");
out.println ("</HEAD>");
out.println ("<BODY bgcolor=\"white\">");
out.println ("<B>Data Added for: " + staffID + "</B>");
out.println ("</BODY>");
out.println ("</HTML>");
out.close (); // Always close the output stream
prepQueryStmt.close();
prepInsertStmt.close();
} // end try
catch (Exception except) {
PrintWriter out = response.getWriter ();
out.println ("Thrown Exception:" +except);
} // end catch
} // end doPost method
Regards,
Rashmi
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Umm... aside from the fact that one of my teachers would cringe and weep at the use of return's in your code (and enough said about that, since I'm not the world's best coder either)...

What does VAJ use for *running* that servlet? You indicate it is a simple enough thing to have VAJ use the 2.2 API for *compiling*, but what about when you run it using Websphere? What version of the API does your runtime use? The same? Are you sure?

Hopefully Kyle Brown will notice this post and weigh in.
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Thanks Mike -- Rashmi -- please clarify something for me. Are you trying to do this in the VAJ 3.5 WTE (WebSphere Test Environment?)
If so then simply bringing in the new JAR files will certainly NOT make it work! You must upgrade to version 3.5.3 (or 4.0) of VisualAge to use the WTE with the servlet 2.2 specification! You can't change out the classes underneath of the application server (WebSphere, which runs in the WTE) and expect it to work.
BTW, trying to make a back-level version of WebSphere work by switching JAR files won't work either. You need to use WebSphere 3.5.3, 3.5.4 or 4.0 to use Servlet 2.2.
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Rashmi Trivedi
Ranch Hand

Joined: Aug 13, 2001
Posts: 60
Dear Kyle
Yes you're right. It is VAJ 3.5 and WebSphere Test Environment and Upgrate to 3.5.3 should solve the probelm.
I will try that next week sometimes and let you know how it goes.
Once again thank you for your time and consideration.
Thank you Mike, you have been very help ful.
Regards,
Rashmi
Originally posted by Kyle Brown:
Thanks Mike -- Rashmi -- please clarify something for me. Are you trying to do this in the VAJ 3.5 WTE (WebSphere Test Environment?)
If so then simply bringing in the new JAR files will certainly NOT make it work! You must upgrade to version 3.5.3 (or 4.0) of VisualAge to use the WTE with the servlet 2.2 specification! You can't change out the classes underneath of the application server (WebSphere, which runs in the WTE) and expect it to work.
BTW, trying to make a back-level version of WebSphere work by switching JAR files won't work either. You need to use WebSphere 3.5.3, 3.5.4 or 4.0 to use Servlet 2.2.
Kyle

Rashmi Trivedi
Ranch Hand

Joined: Aug 13, 2001
Posts: 60
Dear Kyle Borwn
One of my program requires javax.mail package which doesn't come in VAJ 3.5.3 professional edition. I couldn't even find on the internet somewhere.
Could you please guide as to how can i install that package into VAJ??
I really appreciate your time and consideration.
Regards,
Rashmi
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Originally posted by Rashmi Trivedi:
Dear Kyle Borwn
One of my program requires javax.mail package which doesn't come in VAJ 3.5.3 professional edition. I couldn't even find on the internet somewhere.
Could you please guide as to how can i install that package into VAJ??
I really appreciate your time and consideration.
Regards,
Rashmi

Those classes are part of JavaMail. Download that here: http://java.sun.com/products/javamail/index.html
You can install it in its own project in VAJ.
Kyle

------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
Rashmi Trivedi
Ranch Hand

Joined: Aug 13, 2001
Posts: 60
Dear Mike
I would really appreciate if you can assist me to improve writing program in Java. As you mentioned in your reply about the use of return's.... Can you please suggest what would better to use in that case and some other thing which you may think i should use instead......
I really want to work on this part coz i fell that the code i write must at least good if not the best...
I am willing to learn mate....help me out.....
Regards,
Rashmi

Originally posted by Mike Curwen:
Umm... aside from the fact that one of my teachers would cringe and weep at the use of return's in your code (and enough said about that, since I'm not the world's best coder either)...

What does VAJ use for *running* that servlet? You indicate it is a simple enough thing to have VAJ use the 2.2 API for *compiling*, but what about when you run it using Websphere? What version of the API does your runtime use? The same? Are you sure?

Hopefully Kyle Brown will notice this post and weigh in.

Rashmi Trivedi
Ranch Hand

Joined: Aug 13, 2001
Posts: 60
Hi Kyle
Thank you for your help. I have downloaded the javamail.zip file. There are few .jar file like mail.jar, mailapi.jar, smtp.jar, pop3.jar, i will extract them and see which i nee to import to VAJ to compile and run my program...
Thank you once again Kyle.
Regards,
Rashmi

Originally posted by Rashmi Trivedi:
Dear Kyle Borwn
One of my program requires javax.mail package which doesn't come in VAJ 3.5.3 professional edition. I couldn't even find on the internet somewhere.
Could you please guide as to how can i install that package into VAJ??
I really appreciate your time and consideration.
Regards,
Rashmi

Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

You should need
- mail.jar
- activation.jar
to get your JavaMail programs to compile and run. The third thing you'd need is a 'provider'... like smtp.jar and pop.jar. I don't have one of those on my system, so I wonder if you actually *need* the third one. All I do though is send email, I don't try to retrieve email or make an inbox. In those cases, I'd probably need a provider package. But just for sending, all I need is the first two i mentioned.

As for 'better coding'... the javaranch Java In General - Beginningforum is a good place. As a general rule though, try to code your methods so that they have exactly *one* exit point (that is: one return). And as a first-level hint, consider that everywhere you have a return statement, you can replace this will well chosen else if statements.

What I can do is take another look at your code and write you some pseudocode for you to re-write your method against. This will take a couple days, I'm going to be busy at work early this week.
Rashmi Trivedi
Ranch Hand

Joined: Aug 13, 2001
Posts: 60
Dear Mike
I investigate over past few days about writing better java servlet and came up with some thoughts. Now i have started one of my small project on that basis and planning to write servlet according to recommendation....
I was wondering that if can take another look at my servlet and send me modified servlet than i can compare your servlet with the one i am planning to write....
I really appreciate your time and consideration.
Regards,
Rashmi Trivedi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet API 2.2