Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Chat

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
Print Button
 
JavaLive Transcripts Index

Core Servlets and JavaServerPagesTM
August 08, 2000



Speaker Marty Hall

Speaker Marty Hall (Marty)

This is a moderated forum.

EdO: Welcome to Java Live! Our session today is on servlets and JSP. Our guest is Marty Hall, senior computer scientist at Johns Hopkins University. Marty is an educator and author. He's the author of the recently published Core Servlets and JavaServer PagesTM, and the earlier book Core Web Programming. Marty will answer your questions on servlets and JavaServer Pages technology. So let's begin. Who has the first question?

Marty: Hi! I'm glad to be here today, and am happy to take as many questions as we have time for.

victor: Hello, I noticed that on many different sevlet/JSP engines, if you want to use <jsp:usebean> the actual bean you are trying to access needs to be in a package. Is this a safe assumption? Is this how Sun developed the functionality? Thanks, Vic.

Marty: Re: Packages for beans. No, the spec doesn't require beans to be in packages, as far as I can tell. I'm unclear as to why some servers seem to require this. Fortunately, you would almost certainly want to use packages anyhow to avoid name conflicts, so this isn't really costing you much, IMHO.

manju: How cam XML be used along with JSPs and servlets?

Marty: Re: XML and servlets/JSP, I see a couple of possible approaches:

  1. A servlet process XML (say, with DOM) and creates regular HTML content that gets sent to the server.
  2. A JSP page generates XML, which is sent to the server to be used with XSLT or whatever. JSP constructs have XML-compatible variations to make this easier.

theidf.com: What do you see in the near future reagrding debugging JSP?

Marty: Re: debugging JSP. That is a good question. I saw some demos at the 2000 JavaOneSM on using the Java Remote Debugger interface (using JBuilder, I think) to debug JSPs. They identified the servlet that came from the JSP, changed the server startup script to allow remote debugging, and connected on the fly. It seemed to work well. However, I must confess that most people rely on simpler fallbacks most of the time: starting the server from the command line and looking at print statements, using the log, returning error pages, and connecting to the JSP interactively to look at the HTTP response headers.

Plan B: What is the most efficient way for servlets to share hashtable or vector data?

Marty: Well, you have various mechanisms for sharing data. The most common is to use the servlet context; that's shared by all servlets and JSP pages in the same web application (like the same server if no custom web applications are defined). You can also share data using the HttpSession if the data is specific to a client. You can, in principle, even use static methods and singleton classes. Most people reserve that for things like database connection pools. BTW, I have examples of all of these approaches in Core Servlets and JSP, and all the source code is online .

Walter Snel: Hi, I have a question on session tracking in a distributed environment: Can I assume the session object to be a safe repository for session data, or do I need to code a session wide data store myself? Note (Section 4.3.1 of Servlet 2.2 spec): Context Attributes exist locally to the JavaTM Virtual Machine1 in which they were created and placed. This prevents the servlet context from being used as a distributed shared memory store. If information needs to be shared between servlets running in a distributed environment, that information should be placed in a session, a database, or in an Enterprise JavaBeanTM.

Marty: My read of the spec is that session objects are required to be distributed. Furthermore, application servers like BEA WebLogic do, in practice, make them accessible across machines in a distributed-server environment.

RonCrawford_II: Why is the SingleThreadModel interface and option in JSP when the Java platform offers the synchronized keyword? What are the benefits of placing synchronization on the JSP engine? -thanks

Marty: Well, SingleThreadModel gives the server more flexibility than you would have if you manually synchronized all your code in service or _jspService. For example, a server can queue up requests to a single servlet instance or make multiple servlet instances and send requests concurrently (as long as a single instance gets only one request at a time). That all being said, I do agree that in general it is better NOT to use SingleThreadModel and to synchronize access to shared data yourself.

Mike Shelnutt: Is there an easy integration for JSP and Enterprise JavaBeanTM?

Marty: Good question. I hear it a lot. In principle, since scriptlets in JSP can be arbitrary Java code, theycould call the EJB directly. In practice, I see people usually call a bean from the JSP page, then have the bean talk to the EJB and maybe store data in between. BTW, there is an example of this at the end of the J2EETM blueprint, available online or in the Addison-Wesley book.

amit: Marty, do you think, in near future, JINITM and servlets will have some common meeting point by which services can be searched for on the Net like web sites are searched. How do you think servlets work with JINI to provide and an UI for the service.

Marty: Hmm. The integration of Jini and servlets is not something I've thought much about. Off thetop of my head, I would guess that perhaps the real integration might be wih JINI and JNDITM, and servlets (by virtue of usually being in a J2EE environment), could reap the benefits easily. But that's just an off-the-cuff opinion.

Alex: Hi, I'm quite new to servlets, but have four years of full-time Java programming experience. After doing some evaluation of the server-side approach, I've concluded that in order to separate the content from the behavior, I need to adopt templates, rather than delve into the prescribed JSP approach. This is due to the business modle in my company, which insists that designers work independently from developers. Am I on the right track? Thanks.

Marty: I'm not exactly sure what you mean by either "adopt templates" or "the" recommended approach. A common model for complex applications is for the initial request to come to a servlet. The servlet reads the query data (form data), the HTTP request headers, extracts session data, does database lookups, and so forth. The servlet then puts the results into beans, then forwards the request to one of various JSP pages (perhaps what you mean by "templates"?) that produces the final result. The original servlet might use different JSP pages in different situations, each appropriate to the kind of result it wants to present. I use this approach in the Online Travel Agent example in my chapter on integrating servlets and JSP. This is not to say that you would never use a JSP page directly. On the contrary, many pages are very close to static HTML pages with only a moderate amount of dynamic data. My rule of thumb is that a single JSP page corresponds to a single look in the output.

An Lam: Both JSP and pure servlets create dynamic HTML output. What benefits do I get by using JSP instead of just servlets alone?

Marty: JSP doesn't fundamentally give you anything that servlets don't. In fact, a JSP page gets translated into a servlet, and it is the servlet that actually runs at request time. So, the advantages of JSP aren't in their fundamental power; they are in their convenience. First, generating all that HTML in print statements is a huge pain in the neck and is hard to maintain. Second, with JSP you can use one set of developers for page design and layout and another for hard-core Java programming. How many of the CGI developers at amazon.com do you think design amazon's web pages? (None, probably, since the two sets of skills only overlap slightly). Next, JSP lets you use standard HTML design tools like HomeSite and MacroMedia DreamWeaver. Finally, this whole separation promotes the idea of separating the look (presentation) from the content (data). It encourages a style where you can change the presentation without changing much of the code that is creating/manipulating the content.

EdO: What types of things do you tend to see people misunderstand (or misuse) regarding JSP technology?

Marty: Good question. Here are the misunderstandings I hear the most:
  1. Much to my surprise, many people don't understand that JSP is a purely server-side technology. They think that JSP gives them a web page that is more interactive or can do more things than a regular web page could. I was taken aback at how widespread this perception is--that JSP runs on the client or changes the fundamental nature of the type of content the browser can display.
  2. The previous misunderstanding comes from managers and people who hear about JSP, not usually from programmers. The biggest misunderstanding I get from developers has to do with the role of servlets and JSP. Some think that you should never do servlets and only need JSP now. Others think you should always answer the initial request with a servlet. Of course, you can tell I'm biased since I cover both servlets and JSP in my book, but I strongly feel that you need to understand both in order to do effective development in either!
  3. File inclusion, beans, and server setup details are the next main set of questions I get, I think.

veera: Hi, my question is how can I use JavaMailTM with servlets and JSP?

Marty: JavaMail is part of J2EE, so it is directly available to you on many servers. Of course, from JSP you wouldn't want to try to use JavaMail directly from a scriptlet, but would instead use either a bean or a custom tag.

vincentf: One of the major issues many servlet/JSP developers face is the ability to invalidate an http session if the user exits the browser without the proper "logoff". Currently there isn't an easy way to do this as the browser manufacturers do not provide an onWindowClose event that can be used. Has Sun given any thought to this? Vinny

Marty: Hmm.Good question. But I'm not sure you need an onWindowClose event for this to work. If session tracking is accomplished throughURL-rewriting, the session will certainly end when the user exits the browser. If session tracking is based on cookies, it depends on how the underlying cookie is set up as to whether the session ends when the user exits the browser. I know they are trying to hide implementation details, but there are many times I wish I could easily get at the cookie associated with the session.

Lee Hodsdon: How can I get Tomcat to work with IIS? Our product has added JSP support. This is all new to me. In fact, before this I lucked out and didn't even have to do the CGI web server stuff (well, once I did but even then I didn't have to work with servlets). So far, I've got Apache web server installed and the Tomcat Jserv added and this is working under Solaris and Windows NT. However, the reqs for testing also say I'm supposed to test JSP using Microsoft's IIS web server. Tomcat says it'll run under Apache, IIS, and Enterprise (or whatever Netscape's web server it's now called). I cannot find any info saying how to add Tomcat to IIS. For Apache, I just start Apache. Tomcat is not run standalone. For IIS, it seems Tomcat must be started standalone before starting IIS. But I still can't get IIS to use Tomcat. Microsoft isn't going to give any integration info since obviously they'll want to promote only their ASP. I couldn't find any info at jakarta.apache.org. rg. Any site I can go to where I can get information on how to integrate Tomcat into IIS?

Marty: The Tomcat docs are being updated. Last I looked, they had some scanty info on how to integrate Tomcat with servers other than Apache, but not much. I'm sure the response from the Tomcat community would be "OK; please write this up for us and we'll post it." :-) Commercial servlet add ons like Allairte JRun and Unify ServletExec are more clear on how to do this.

santsingh: Marty, Why do we need HTTP Tunneling? In which situation should we use it?

Marty: The general term "HTTP tunneling" means to embedanother protocol inside HTTP. That way you canpiggyback on things HTTP has (such as, encryptionvia SSL, access through your corporate firewall, etc.) The place HTTP tunneling is used the most withservlets/JSP, and the area in which I have examplesin my book (again--all source code is online free at www.coreservlets.com"--no restrictions on use) is when servlets talk to applets. There, using tunnelinglets applets and servlets send serialized objects back and forth.

Walter Snel: Marty, a question on the interaction of EJB security and JSP in an EJB application. It seems to me that:
  1. Authenticating unauthenticated users using a specialised JSP login page when they hit a protected page.
  2. Subsequently providing access to a user-associated profile from within any JSP is a very frequently occurring thing. Is there a best way of doing these things in the EJB/JSP combination. (My understanding of the EJB security and realms is limited)

Marty: Excellent question. I think that the "declarative security" of J2EE was supposed to make thiseasy, but it isn't clear to me how wildely implementedthis is. You're supposed to be able to use the XMLsetup file to specify the login page, the rolesthat have access, and so on.

bwang: What is the best way to create a real time data display on a web browser using JSP/servlet technology?

Marty: First, I want to refer you back to the question Ed asked about common misunderstandings of JSP...

Marty: That means that your question partly amounts to "how can a web page to display real time data"...

Marty: If you are talking about data that scrolls continuously, or a graph that moves continuously, then the answer is that a regular HTML page can't do it, but an applet can. I have an example in my book of a servlet sending real-time data to an applet that displays it. Bottom line, though: remember that servlets and JSP don't fundamentally change the type of content that the web browser can display. They just create that content.

EdO: We're down to our last few minutes of the session. We've got lots of questionsin the queue, so obviously Marty won't be able to answer all of them. We apologize if we haven't gotten to your question. You can send your questions about servlets to servlet-interest@java.sun.com and JSP questions to jsp-interest@java.sun.com We hope the session has been helpful.

Marty: Thanks for listening! Stop by www.coreservlets.com for the book TOC, index, source code, and information on servlet and JSP short courses. The Java Developer's ConnectionSM currently has two chapters from the book on-line (one on servlets and one on JSP custom tags).

Cheers!

EdO: Well, the hour has quickly gone by. As I mentioned earlier, we had a very large number of questions in the queue. We're sorry we didn't get to all of them. I want to thank all the pariticipants, and especially to our guest, Marty Hall.

Marty: Thanks again!

EdO: Last moderator (me) signing off. The forum is now unmoderated

Participate in the JavaServer Pages Forum.

coffeecup


Reader Feedback

Tell us what you think of this transcript.

Duke

Very worth reading Worth reading Not worth reading

If you have other comments or ideas for future articles, please type them here

_______
1 As used on this web site, the terms "Java virtual machine" or "JVM" mean a virtual machine for the Java platform.


Print Button
[ This page was updated: 21-Sep-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary | Feedback | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.