Saturday, 29 October 2011

Servlets FAQ -Part5


Comments and alternative answers
Comments on E and F Page parsers have been written....
Author: Brett Knights (http://www.jguru.com/guru/viewbio.jsp?EID=8054), Jan 27, 2000
Comments on E and F Page parsers have been written. Go to http://www.docuverse.com/htmlsdk/index.html and you can download the pieces necessary to parse html files into a dom Document. You can then use dom methods to modify then emit the results. OR if you prefer F you can suck most of an html page into an xsl template tag and insert <xsl: tags to format your dynamic output. It's not great for really dynamic sites but it's an excellent way to have a page designer do a bunch of work then turn it over to you for installing dynamism (dynamicality? :-) Saxon is an excellent engine for this.

There's another option: use SSI for Java. It's an SSI...
Author: Joe Morse (http://www.jguru.com/guru/viewbio.jsp?EID=91113), Jun 29, 2000
There's another option: use SSI for Java. It's an SSI parser that runs in your VM, and it's open source (GPL). It also includes some cool utility classes (which you can mimic or extend) for creating stateful form elements and other things. There's even an iterator for printing variable-length collections (e.g. db recordsets, etc). Plus, it's portable; it will run on any web server with little or no changes. Check it out at http://www.freecode.com/cgi-bin/viewproduct.pl?8543.
XMLC
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3), Oct 27, 2001
A great new "third way" solution -- with templates on one side, and servlets on the other -- is XMLC.
Faced with the problem of separating presentation from code, XMLC takes the radical step of... (drumroll please...) actually separating the presentation from the code!
The presentation "layer" is literally an HTML file. The code "layer" is a servlet (or any Java class) that reaches into the HTML file and changes its content, based on "ID" attributes embedded inside the HTML tags. (The way it accomplishes this is by compiling the HTML file into a Java class and data structure, but that's almost beside the point.)
How do I send email from a servlet?
Location: http://www.jguru.com/faq/view.jsp?EID=154
Created: Sep 3, 1999 Modified: 2000-07-25 12:18:54.284
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3)

From: James Cooper (pixel@bitmechanic.com)
GSP and GnuJSP both come with SMTP classes that make sending email very simple. if you are writing your own servlet you could grab one of the many SMTP implementations from www.gamelan.com (search for SMTP and java). All the ones I've seen are pretty much the same -- open a socket on port 25 and drop the mail off. so you have to have a mail server running that will accept mail from the machine JServ is running on.
See also the JavaMail FAQ for a good list of Java mail resources, including SMTP and POP classes.
Comments and alternative answers
If you want to use the JavaMail API, get it and create...
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7), Jul 24, 2000
If you want to use the JavaMail API, get it and create a program similar to the Hello World program.

See also
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3), Sep 20, 2001
This thread: Re: Automatically send and get mails using servlet...

Some examples
Author: Chris Lack (http://www.jguru.com/guru/viewbio.jsp?EID=326717), Sep 21, 2001
I've written an "EMailClient" class for sending e-mails for my guestbook entries. I've also done an "InBox" servlet class that lists e-mails in your pop mail in-box so that you can delete or bounce them before downloading to your PC. Have a look at the code, it might help -
http://www.chris.lack.org and choose the java option on the professional menu.
By the way you'll need JavaMail and Java Activation foundation from Sun if you've not already downloaded them. You don't need your own mail server.
Are there any ISPs that will host my servlets?
Location: http://www.jguru.com/faq/view.jsp?EID=155
Created: Sep 3, 1999 Modified: 2003-04-04 10:57:05.916
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3)

The Adrenaline Group maintains a list of over 50 ISP's who host Java Servlets (http://www.adrenalinegroup.com/jwsisp.html) . Another list is at http://www.servlets.com.
Those that our gurus (you) have had specific experience with include:
A few ISPs have also said that they can host Java applications:
Please report any experiences, good or bad, you have with these services to this thread.
See also What ISPs provide hosting services which include JSP support?
Comments and alternative answers
Horus Networks is very flexible on all services, and...
Author: Denis BUCHER (http://www.jguru.com/guru/viewbio.jsp?EID=7742), Jan 22, 2000
Horus Networks is very flexible on all services, and we would be pleased to host servlets... c.f. our own programmation tests under http://www.horus.ch/servlets.shtml http://www.horus-networks.com/servlets.shtml

Web Hosting
Author: dufunk Eugene Rozum (http://www.jguru.com/guru/viewbio.jsp?EID=1193732), Aug 17, 2004
Prokmu jsp hosting offers the best quality/price JSP/Servlet services!

Re: Web Hosting
Author: dufunk Eugene Rozum (http://www.jguru.com/guru/viewbio.jsp?EID=1193732), Sep 6, 2004
http://www.prokmu.com

Re[2]: Web Hosting
Author: Eugene Rozum (http://www.jguru.com/guru/viewbio.jsp?EID=1254770), Jul 24, 2005
Prokmu Jsp hosting http://www.prokmu.net

http://www.ebpcs.net is a very cool one too
Author: Steve Nguyen (http://www.jguru.com/guru/viewbio.jsp?EID=39558), Apr 24, 2000
http://www.ebpcs.net is a very cool one too

If anyone looking for free Servlet/JSP hosting check...
Author: Nilesh Shah (http://www.jguru.com/guru/viewbio.jsp?EID=1810), May 30, 2000
If anyone looking for free Servlet/JSP hosting check this out. http://www.mycgiserver.com
http://www.servlets.net http://www.tricreations.co...
Author: Melanie Munden (http://www.jguru.com/guru/viewbio.jsp?EID=138305), Aug 30, 2000

Check out www.wantjava.com They support Tomcat, O...
Author: James Ward (http://www.jguru.com/guru/viewbio.jsp?EID=242591), Nov 1, 2000
Check out www.wantjava.com They support Tomcat, Oracle, and mySQL! They also have great customer service!

Another good listing of ISPs that support Java Servlets...
Author: Bill Day (http://www.jguru.com/guru/viewbio.jsp?EID=135825), Dec 8, 2000
Another good listing of ISPs that support Java Servlets is at:
www.servlets.com
The listings include contact info and rates.

More more more
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3), Jul 16, 2001


CWI Hosting (http://www.cwihosting.com)
Author: Scott Barstow (http://www.jguru.com/guru/viewbio.jsp?EID=226863), Oct 11, 2001
Do not use these guys. I have had nine kinds of grief with them, and their support is less than adequate. I have had outages of three days, and mail outages of three - four days.

Re: CWI Hosting (http://www.cwihosting.com)
Author: Joost Schouten (http://www.jguru.com/guru/viewbio.jsp?EID=581960), Jan 24, 2004
I just wanted to say that I use CWI hosting aswell, and do appreciate their service. They have provided me with good support, even on my own code at times.
Just wanted to give them a fair chance.
Joost

re: Webhosting for servlets
Author: richard freeman (http://www.jguru.com/guru/viewbio.jsp?EID=752499), Feb 8, 2002
Zetnet.com will host jsp, sql, mysql, etc. with all of their standard hosting packages (Ranging from UK£100 per annum).
Drop me a line on 01524 34918 if you are interested in doing so...

4Java.ca - inexpensive and reliable host
Author: support 4javaca (http://www.jguru.com/guru/viewbio.jsp?EID=804843), Mar 20, 2002
They host JSP, Servlet, J2EE. Very inexpensive, only USD$8.50/month! http://www.4java.ca

MyServletHosting.com
Author: Walter Meyer (http://www.jguru.com/guru/viewbio.jsp?EID=23642), Mar 27, 2002
I've been with MyServletHosting.com for over a year. The experience was great at first, but over that last 5 or 6 months, stability has really degraded.
When I contact tech support they always apologize and say they're in the middle of moving their clients to more stable servers.
Maybe they'll straighten everything out in the future, but for now, I'm looking for a new host.


Re: Scorpions.net
Author: John Colucci (http://www.jguru.com/guru/viewbio.jsp?EID=816265), Mar 28, 2002
I am with Scorpions.net for about 8 months now. I have personal and business sites there. They use Tomcat,iPlanet, Java web server. Oracle hosting is super cheap and no set up fees. So far I am happy with them. John.

Check out http://www.ragemedia.ca
Author: Alex Iljin (http://www.jguru.com/guru/viewbio.jsp?EID=894025), May 27, 2002
Check out http://www.ragemedia.ca They provide J2EE application hosting (including Servlets, JSP and EJB's) based on Tomcat/Jboss platform, plus PostgreSQL database is standard for all plans.
Rimu Hosting
Author: Peter B (http://www.jguru.com/guru/viewbio.jsp?EID=1071364), Mar 29, 2003
Try http://rimuhosting.com
Rimu Hosting provide Virtual Dedicated servers. That includes 128MB of memory, 8GB of disk space, and 30GB of transfers.
Accounts come with JBoss and include JDK1.4, JSP, EJB and servlet support. You don't share the Java VM and you get to configure JBoss the way you need it.
Other features include SSH root access, Webmin CP, MySQL, PHP, Redhat Linux.

Re: Rimu Hosting
Author: Peter B (http://www.jguru.com/guru/viewbio.jsp?EID=1071364), Oct 15, 2003
A customer who just signed up pointed that our plans now come with 4GB of disk.
Our Red Hat file systems have been updated to use Java 1.4.2, JBoss 3.2.1 as well a choice of a recent Tomcat or Jetty servlet engine.


RE: Java Hosting
Author: Alacarte Java (http://www.jguru.com/guru/viewbio.jsp?EID=1118665), Sep 29, 2003
I am with Alacarte Java Hosting (http://www.alacartejava.com) and we specialize in low cost Java JSP Hosting, Servlet Hosting, and much more. Checkout our Web Site at http://www.alacartejava.com

« previous
beginning
What is the difference between URL encoding, URL rewriting, HTML escaping, and entity encoding?
Location: http://www.jguru.com/faq/view.jsp?EID=156
Created: Sep 3, 1999 Modified: 2000-09-17 15:25:28.842
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3)

URL Encoding is a process of transforming user input to a CGI form so it is fit for travel across the network -- basically, stripping spaces and punctuation and replacing with escape characters. URL Decoding is the reverse process. To perform these operations, call java.net.URLEncoder.encode() and java.net.URLDecoder.decode() (the latter was (finally!) added to JDK 1.2, aka Java 2).
Example: changing "We're #1!" into "We%27re+%231%21"
URL Rewriting is a technique for saving state information on the user's browser between page hits. It's sort of like cookies, only the information gets stored inside the URL, as an additional parameter. The HttpSession API, which is part of the Servlet API, sometimes uses URL Rewriting when cookies are unavailable.
Example: changing <A HREF="nextpage.html"> into
<A HREF="nextpage.html;$sessionid$=DSJFSDKFSLDFEEKOE"> (or whatever the actual syntax is; I forget offhand)

(Unfortunately, the method in the Servlet API for doing URL rewriting for session management is called encodeURL(). Sigh...)
There's also a feature of the Apache web server called URL Rewriting; it is enabled by the mod_rewrite module. It rewrites URLs on their way in to the server, allowing you to do things like automatically add a trailing slash to a directory name, or to map old file names to new file names. This has nothing to do with servlets. For more information, see the Apache FAQ 

(http://www.apache.org/docs/misc/FAQ.html#rewrite-more-config) . 

Servlets FAQ -Part4


How do I use Session Tracking? That is, how can I maintain "session scope data" between servlets in the same application?
Location: http://www.jguru.com/faq/view.jsp?EID=151
Created: Sep 3, 1999 Modified: 2000-09-10 10:49:31.243
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3)

Session Tracking is one of the most powerful features of Servlets and JSP. Basically, the servlet engine takes care of using Cookies in the right way to preserve state across successive requests by the same user. Your servlet just needs to call a single method (getSession) and it has access to a persistent hashtable of data that's unique to the current user. Way cool.
See section 2.3 of the Servlet Essentials tutorial (http://www.novocode.com/doc/servlet-essentials/) . Also see The Session Tracking API (http://webreview.com/wr/pub/1999/01/08/bookshelf/) , excerpted from Java Servlet Programming (http://www.oreilly.com/catalog/jservlet/) by Jason Hunter (http://webreview.com/wr/pub/au/Hunter_Jason).
A point I haven't seen emphasized enough is that you should only add objects that are serializable to an <mono>HttpSession</mono>. Specifically, a JDBC Connection object is not serializable, so should not be added to the session (despite the example in Jason Hunter's otherwise admirable Java Servlet Programming). If you'd like to associate a connection with a session, then store some arbitrary, unique handle in the session, then use that to key off your own hashtable of connections. (Make sure upon retrieval that the returned connection is not null, and if it is, create a new connection!)
The reason is that sessions may, at the whim of the server, be swapped out to disk, in order to save memory or reboot the server. This behavior can be disabled by setting a configuration parameter in your server or servlet engine. (I can't remember offhand what these are -- please email me if you know.) From the spec:
Some servlet engine implementations will persist session data or distribute it amongst multiple network nodes. For an object bound into the session to be distributed or persisted to disk, it must implement the Serializable interface.
Comments and alternative answers
Also see the FAQ What servlet code corresponds to...
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3), May 21, 2000
Also see the FAQ What servlet code corresponds to the various "scope" values for the <jsp:useBean> tag? .

Re: Also see the FAQ What servlet code corresponds to...
Author: Aditya Sharma (http://www.jguru.com/guru/viewbio.jsp?EID=1151271), Mar 10, 2004
Sorry i feel it is just an explanation by words.A small illustration of code for session tracking will help

Re[2]: Also see the FAQ What servlet code corresponds to...
Author: Link Tree (http://www.jguru.com/guru/viewbio.jsp?EID=1212794), Nov 24, 2004
Very simple code - let say that the
public void doPost(HttpServletRequest request, HttpServletResponse response){
...
//lets take the session obj
HttpSession session = request.getSession(true);
//now let us take the user name assosiated with this session
String currUserLogin = (String) session.getAttribute("currentUserLogin")
...
//now we do the work according to the user name
doSomeWork(currUserLogin);
...
 
}
You should do the same thing in the login servlet where you take the user name and use the addAttribute() of the session obj.

what about the "jsessionid" added to keep the session ?
Author: M. washu (http://www.jguru.com/guru/viewbio.jsp?EID=1217388), Dec 21, 2004
Hi all,
Is there a way to include jsessionid in a hidden field (in a form) rather than in the URL (by the URL rewriting mechanism ) ? I saw that HttpSessionContext class was deprecated for security reason, but for security reasons too i would like to know if there is a way to prevent the jessionid from being logged in the HTTP server log files (of course without using cookies) ? Thanks in advance.
How can I detect whether the user accepted my cookie?
Location: http://www.jguru.com/faq/view.jsp?EID=152
Created: Sep 3, 1999 Modified: 2000-09-06 16:35:12.955
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3)

Here's a clever trick: use a redirect. Drop a cookie on the HttpServletResponse object, then call response.sendRedirect() to a "phase two" servlet. The "phase two" servlet then tests whether the cookie was sent back to it. If so, that means the user accepted the cookie the first time; if not, it means that either the client rejected the cookie, or the browser doesn't support cookies.
Note that this technique only works if the "phase two" servlet is hidden from the user; if the user can jump directly to the test phase, then the servlet can't tell the difference between newly-arrived clients and cookie-unfriendly clients. That means you should send a redirect from the test phase, to make sure the user doesn't have a chance to bookmark the test phase's URL.
Alex Chaffee (http://www.stinky.com/alex/, alex@jguru.com) has written a Servlet that demonstrates this technique called CookieDetector (http://www.purpletech.com/code/CookieDetector.html)
How do I integrate HTML design into my Servlet?
Location: http://www.jguru.com/faq/view.jsp?EID=153
Created: Sep 3, 1999 Modified: 2001-01-15 07:31:27.091
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3)

The question can be rephrased, "How can I design a work flow that incorporates HTML designers and programmers to build a dynamically generated web site that will keep expanding and growing over time?" This is a special case of the intractable Content Management Problem of the Web in general. The real problem is to allow HTML designers (that is, humans) to use their favorite HTML editing tools without learning Java, and to allow marketing people (arguably humans) to change the look of the site on a whim, without having to alter the database access code inside the servlet. (And vice versa -- to alter the business logic and data access without altering the user interface.)
See my article at Servlet Central (http://www.servletcentral.com/1998-12/designprocess.dchtml) for an expansion on these themes.
There are many, many possibilities... The list below is not complete, but should give you some guidelines.
A. Hardcode HTML. You can just put HTML inside of print statements in your Servlet's doGet() or doPost() method.
Pro: easy to code, easy to understand for the programmer.
Con: difficult to understand for the designer; when a change has to be made, the programmer has to wait for the designer to finish her HTML, then re-hard-code it all back into print statements, then make sure the generated HTML actually does what the original HTML did. Basically, good for a hello world servlet, not good for a real web site.

B. Server Side Includes (SSI). Use the <SERVLET> tag inside your HTML file (and rename it .shtml). The HTML designers will make pretty pages; your servlets will output small pieces of text that get spliced in to the web page by the server.
Pro: separates UI (HTML) and code.
Con: You have two possible end paths with SSI: either your servlet outputs many tiny bits of text with no HTML tags, or your servlet outputs a big bunch of text with embedded HTML tags. In the first case, your code can't take advantage of its knowledge of the structure of the data -- for example, you can't format an HTML table from a database. In the second case, you're back to hardcoding HTML, thus making it hard to change the look of your pages.

C. Presentation Templates. This is a good way to put common navigation elements (button bar, credits, etc) on all of your pages. The technology is built in to the Java Web Server, and implemented by several (though not all) of the servlet engines.
Pro: you don't have to enter in the same common HTML for all the countless pages in your web site.
Con: your servlet code still has to hardcode HTML if it wants to be powerful (see item B, SSI).

D. JSP Java Server Pages. You write files in HTML format, and embed actual Java code inside the HTML. This is kind of like using JavaScript, only it's on the server, and it's real Java. This is directly parallel to Microsoft's ASP.
Pro: it's really cool; you only need a single file to do both UI and layout code; you don't have to type "println" so much.
Con: if you do anything interesting, then your HTML designers will get really confused looking at the interlaced Java and HTML code -- so make sure to put the complicated code inside a JavaBean where it belongs, not in the JSP page.

The new version of the JSP spec has lots of features for integrating with JavaBeans, which is a great way to separate user interface (JSP) from data and business logic (beans). See also the JSP FAQ (see our References section for a link).
Halcyon Software has a product called Instant ASP, which allows you to execute Microsft IIS-style ASPs (including code in VBScript, Jscript, Perl, Java, and JavaScript) in any Servlet Engine. Also Live Software has CF_Anywhere, which executes Cold Fusion CFML pages. See the References section for links.
E. Write your own page parser. If for some reason you're not happy with the standard mechanisms for doing templates (see B-D above), you can always write your own parser. Seriously. It's not rocket science.
F. HTML Object Model Class Libraries e.g. htmlKona, XML. With these class libraries, you write code and build an object model, then let the objects export HTML. This doesn't really work for complicated layouts -- and forget about letting your designer use an HTML editor -- but it can be useful when you have a highly dynamic site generating HTML, and you want to automate the process. Unfortunately, you still have to learn HTML, if only to understand and validate the output. See the References section of this FAQ for a listing of some class libraries that can help.
G. Do it all yourself Develop a database-driven content management system. Think C|Net. It has a lot of standard content, but the database is king. HTML designers have little pieces of the page that they can play with, but ultimately they're just putting content into a database, and the site (servlet) is generating every page request dynamically. This sort of system is very difficult to design and build, but once you've built it, it can really pay off -- but only if you have dozens of writers, editors, designers, and programmers all working on the same site on an ongoing basis.
For a brief list of alternate page template systems, see the References section of this FAQ.