Using and Writing Java Servlets
Point your web browser to http://localhost.localdomain/examples/servlets/, and execute any one of the examples to confirm that your installation and environment is a success. Another way is to use http://localhost:8080/.
If you do experience problems, you will need to revisit the setup documentation. Unfortunately, the documentation is not very user-friendly for any of the servlet enablers I use. You also can read the various FAQs and forums available on the Apache web site and servlet enablers.
Listing 1 is an example of creating a servlet. This example will generate a web page that will display a simple message.
Note that a servlet extends the HttpServlet class. This is located in the Tomcat installation directory, in this instance /var/tomcat/lib/servlet.jar. When you want to generate HTML output, it is necessary to obtain the output channel with response.getWriter(). It also is necessary to set the CLASSPATH to include the full names of the .jar files, e.g.,
export CLASSPATH=/var/tomcat/lib/servlet.jar:$CLASSPATH
Next, compile with javac FirstServlet.java. Ensure that you are using the same JDK that Tomcat has been set up to use (as described previously).
To make things simpler for the this exercise, place the generated .class files into /var/tomcat/webapps/examples/servlets (the actual configuration of Apache and Tomcat are beyond the scope of this article). To execute the servlet, open the web page at http://localhost.localdomain/examples/servlet/FirstServlet or http://localhost:8080/servlet/FirstServlet. And viola, the output seen below has been created dynamically:
Hello Fellow Servlets
Listing 2 not only demonstrates how to display useful information, but also shows the security concerns involved. Servlets must be written so that no input from a remote browser can give a cracker access to certain resources. Even something simple, like causing an error in the servlet, may cause the JVM to perform differently.
The example generates the HTML code for a table, which a selective query populates by the output provided from a system API to retrieve certain configuration variable settings (see Listing 3). This, for instance, can be used as a template to a database query using JDBC (Java Database Connectivity).
Listing 4 demonstrates how a servlet can not only generate dynamic web pages, but also process incoming data via an HTML form. To process input from a form, it is necessary for the servlet to override two functions: doGet and doPost. The doGet function always needs to be defined and forms the default behavior of the servlet and the processing of HTML form data sent via the GET method. The doPost function is used only when HTML form data is sent using the POST method, which is a more robust way than the GET method. In this example, doGet reports an error because it will be invoked only if the HTML form data was not sent with the POST method. In the doPost function, the request.getParameter calls are used to retrieve the corresponding parameter's value. Those of you who have used other CGI scripts may notice that this is a very simple and straightforward way to retrieve these values, which usually can end up being an onerous task.
Listing 5 shows the HTML input form, which a servlet could also have generated. Figure 1 shows a snapshot of this form.
Listing 5. InputServlet HTML Input Form

Figure 1. InputServlet HTML Input Form Snapshot
The output reorganizes the entered data and displays it as an HTML page:
Hello, your name is Polly, Molly Polly
Again, this data could have been passed to a file or database.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- New Products
- RSS Feeds
- Tech Tip: Really Simple HTTP Server with Python
- Epistle
34 min 16 sec ago - Automatically updating Guest Additions
1 hour 42 min ago - I like your topic on android
2 hours 29 min ago - Reply to comment | Linux Journal
2 hours 50 min ago - This is the easiest tutorial
9 hours 4 min ago - Ahh, the Koolaid.
14 hours 43 min ago - git-annex assistant
20 hours 43 min ago - direct cable connection
21 hours 5 min ago - Agreed on AirDroid. With my
21 hours 15 min ago - I just learned this
21 hours 20 min ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.



Comments
code to display charts
i want to display more than one cahrts in a web page by using java by passing x-axis nad y-axis. and the data should fetch from oracle database. the query should execute in the backend.
Can u pls sort this out
hi.thanks for the wonderful stuff.
i have a problem, however.
I developed a login form in html,got it validated for user authentication through java servlets,and if user is valid the servlet is redirected to another html page, but now i wish to edit certain tags of the html page before redirection, kindly find some time to answer this soon.
Posting from Http DoPost without html form
Hi,
How can I call DoPost directly from the code, without html post method?
Thanks,
Re: Using and Writing Java Servlets
Good review. Right on target on the configuration being troublesome. The beating of old drums such as 'recompiling for new API's' or 'Java is slow' is unfortunately pure bantor. Everybody in every language recompiles code from time to time. And saying 'java is slow' is akin to the windoz jokers preaching that 'linux has no desktop interface'. How very yesterday....
running servlet
Hi,
I tried your example of form response servlet. Can you tell me where should I put the html form file and the class file of the sevlet? so that I can try it on Abyss Server/Tomcat/Servletrunner
Thanks,
Alankar
Re: Using and Writing Java Servlets
I like the tutorial, it will help me learn. I wish some of the value judgements could have been left out. I am, however, sick of Java people bashing "scripting" langs that they clearly have no knowledge of.
Eric
Re: Using and Writing Java Servlets
I really like this Tutorial too! I just start using Servlet yesterday and I have already create a WebApp information Table from what I have learned from this session. Thank you!
==Sandy
Re: Using and Writing Java Servlets
No credit is given to mod_perl, which only loads one instance of the Perl interpreter. Within mod_perl, pages are compiled once and run many times during the life of the server process. Many major web sites use the power of mod_perl to leverage the speed of Apache and the power and flexibility of Perl to generate rich, dynamic, and lightning-fast Web sites.
Re: Using and Writing Java Servlets
The article is not about mod_perl. However I do agree the article is a bit misleading as nobody in their right mind uses perl/php via CGI with Apache.
I like this article but can u
I like this article but can u plz tell me the full way to run Servlet on tomcat.And how to modified or create web.xml for the servlet.