Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sessions and servlets

Status
Not open for further replies.

wduty

Programmer
Jun 24, 2000
271
US
I do a lot of asp development and for session tracking I usually write a session ID into all the querystrings. I know it's primitive but there seems to be a lot of press about asp sessions affecting scalability (even microsoft says so) and I don't like using cookies because I had this client once and...
The session object is actually much easier to use and I use it for sites where there's going to be low hit volume.

However, lately I've been rewriting some old asp and cold-fusion sites as java servlets. Transfering the programming logic and functionality to java hasn't been a problem. However, it seems like every page would have to be a servlet in order to get the session value to the next page. Is jsp the only way?

In an asp site, it's not a problem to make every page an asp page even if it's just for the purpose of writing one variable into one link to maintain a session. It's just a text file. However, it seems like a hassle to do this with servlets. I say this because although I don't mind recompiling stuff, I work with designers who are used to being able to go into asp pages I've written and just change the HTML around for their design changes. They wouldn't appreciate being asked to make changes to java files and recompile them every time they want to add a line break somewhere.

I'd appreciate anyone's comments on how they deal with sessions in a servlet based site.
 
> Is jsp the only way?

The only way to accomplish all the other criteria you mention, Yes.

JSP has much in common with ASP from the developers standpoint. The architecture however is very different. The engine actually compiles your JSP pages into servlet byte code and that is what is executed when the vistor requests your page. So the JSP runtime environment is exactly the same as the Servlet runtime environment since it is actually a Servlet.

-pete
 
Yeah hi pete. We had another discussion on this a while back if you remember. Speaking of jsp, one thing I've encountered is the question of how much to encapsulate in a class called by a jsp page. Suppose I have a table in a database which I want to list. This is a typical type of operation for dynamic websites. In asp you would do something like: 1.open connection 2.execute query/get recset 3.loop through recset and write to response 4.close objects.
However, this takes up a lot of room in the asp page because of all the ado statements and the loop which usually involves writing in table cells, plus in a real website there's tons of additional code and so on. You can take care of this with COM to an extent but the analogy in jsp would be to encapsulate the whole database query into a class and just have a few lines in the jsp page which call the class. Here's a simple example of what I mean:

jsp page
-----------------------------------
[red]<jsp:useBean id=&quot;test&quot; scope=&quot;page&quot; class=&quot;sunflower.sftestJDBC&quot; />
<%
test.setProductList(&quot;1&quot;);
out.println(test.getProductList());
%>[/red]

class (bean) called by the jsp page:
-----------------------------------
[red]package sunflower;
import java.sql.*;
public class sftestJDBC{
[tab]private Statement st;
[tab]private ResultSet rs;
[tab]private String productList = &quot;&quot;;
[tab]public void setProductList(String type) {
[tab][tab]try {
[tab][tab]Class.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;);
[tab][tab]java.sql.Connection conn = java.sql.DriverManager.getConnection(&quot;jdbc:eek:dbc:Sunflower&quot;);
[tab][tab]st = conn.createStatement();
[tab][tab]rs = st.executeQuery(&quot;SELECT * FROM products WHERE type=&quot;+type);
[tab][tab]int counter = 1;
[tab][tab]while(rs.next()) {
[tab][tab][tab][tab]this.productList += counter+&quot;: &quot;+rs.getString(&quot;description&quot;)+&quot;<br><br>&quot;;
[tab][tab][tab][tab]counter++;
[tab][tab][tab]}
[tab][tab]rs.close();
[tab][tab]}
[tab][tab]catch (SQLException e) {}
[tab][tab]catch (ClassNotFoundException e) {}
[tab][tab]}
[tab]public String getProductList()
[tab][tab]{return productList;}
}[/red]

This works fine on my computer but the speed decreases dramatically if I start putting table cells and other stuff in the output string. Putting all the JDBC statments in the jsp page is quite zippy because you can output with out.print() instead of loading them into a string which I assume takes up memory and is presumably the cause of the problem. You could alternately pass only the recordset from the class to the jsp page but then you still have to format it and you're back to writing code in the jsp page. Is there some way to write from the underlying class without creating a big string. I tried using a printWriter from the class and labelling the method as void but I got various method not found errors.

Another question is this. In the above example, I just call the class methods directly. But I could just as easily use the bean:setProperty and bean:getProperty statements. Is there an advantage to doing it one way over the other?

Finally, a big question: I find that if I make changes to the jsp page, the page gets recompiled and therefore updated. However, if I make changes to the underlying class, I have to restart the javawebserver to see the changes. This is fine locally but if I were developing on a rented server with virtual hosting how would I make changes to class files which are used by the jsp pages? I can't really ask them to restart the server everytime I need to make changes. How is this usually dealt with?

Any comments appreciated.
 
> This works fine on my computer but the speed decreases dramatically if I
> start putting table cells and other stuff in the output string. Putting all
> the JDBC statments in the jsp page is quite zippy because you can output with
> out.print() instead of loading them into a string which I assume takes up
> memory and is presumably the cause of the problem.

That would certainly NOT correlate with what we have seen in our JSP development (Java Web Server, NT/Solaris, Oracle, WebToGo environments ). We use memory String variables to build html and pass the string back to JSP for output all the time. In JSP defined classes as well as Beans. Mostly we do this at the JSP level since our decision was to draw the line for beans at the presentation layer, a decision that I fully support.

> Another question is this. In the above example, I just call the class
> methods directly. But I could just as easily use the bean:setProperty
> and bean:getProperty statements. Is there an advantage to doing it one
> way over the other?

Performance advantage? I don't know. As stated above we have not seen any performance problems related to the techniques we have been using. There is a distinct advantage in terms of readability and code maintenance in my opinion.

> I can't really ask them to restart the server everytime I need to make
> changes. How is this usually dealt with?

Ah... yes, you have spotted the most obvious barrier to widespread use of JWS. This is why you will not see JWS implementations of Hosting sites. Indeed the comments from the Tomcat project states that the JWS should not be considered a 'Production' Web Server.

I am not aware of any JSP hosting products yet although there may well be some. They likely run or will run a proprietary server in which they have addressed this issue. Also, if you go to the Tomcat site you may find that the current release or beta contains a solution to this issue, or perhaps that future releases plan to provide this solution.

&quot;But, that's just my opinion... I could be wrong&quot;.
-pete



 
As for the slowdown, that's what I'm seeing here. I've tried a dozen different approaches and it seems to keep boiling down to that. It may have something to do with the whole set up since I'm running this in a test environment on a desktop with PWS and Access rather than on a real server set up like you are. But like I said, outputting directly to the response is faster.
So if you are using JWS do you only periodically update the site for a set of changes at a time? Or perhaps the underlying functionality doesn't change often enough to be a problem? I could use any feedback on these issues because I'm pushing to try working with jsp or at least servlets in some upcoming project.
 
Will,

Yes we unit test, then publish to the QA server where regression testing is performed by QA. Then when all the cycles are finished we stop the production server publish the new site compenents to the production server and then start the server.

Good luck
-pete
 
My One cent on the Strings and Performance:
There is a known issue with the performance with Strings in Java. The recommendation is to use StringBuffer instead if you are using a lot of repeatitive Concatenations and such.

And My other cent on the original question:
You can also use products like WebMacro instead of Jsp. I prefer jsp because I can hire more people who are familiar with the subject. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top