So far, we have been writing static web pages, which means that you write them once and then
they are exactly the same forever, unless you later go change the HTML or CSS. Some important
sites on the web are based on static web pages, but not very many.
Pages like http://www.google.com, http://************************** and
http://en.wikipedia.org are dynamic, which means that you can interact with them
through your browser, getting different information at different times, without anyone having to
re-write the HTML code. To accomplish this, we must write a program that will generate the
HTML code automatically whenever it is needed.
Pretty much any language can be used for dynamically generating HTML code, but some are
better than others. Standard Java, in the way that we have learned to write it in the past, is not a
good way to dynamically generate HTML code because it would be necessary to start up another
copy of the entire Java virtual machine every time someone visits your web page – perhaps
thousands of times every second!
Fortunately, the developers of Java realized that the world needed a way to unleash the
power of Java on to the web, and released a product known as Java Enterprise Edition. There are
two differences between the Enterprise Edition (EE) and standard Java:
• The Enterprise Edition includes additional APIs, some of which are designed for building
dynamic web pages.
• The Enterprise Edition is not used for building standalone applications with a main method.
Instead, their classes are loaded into a web container.
The purpose of a web container is to have a single program, with a single copy of the virtual
machine, that is always running and that can respond to new requests when they come in. We
will be using a web container known as Apache Tomcat.
One of the APIs provided by Java EE is the HttpServlet class, which we can extend to
create a class that dynamically generates a web page when loaded into Tomcat.
Starting to develop with Java EE is a complicated process, but fortunately one that we will only
need to do once. The following steps assume you are working on a computer in the CS lab.
1. Start the program jee, which is a version of Eclipse modified to work with Java EE. You will
not be able to use the standard version of Eclipse to build web pages, and you should not
try to use the EE version to create standalone applications for other courses.
• Use the workspace /home/yourname/********* because the EE version of
Eclipse needs to have a different workspace than the standard version.
2. Create a new Dynamic Web Project.
• Name it CS256L-LastFirst (with your name).
• On the first page of the wizard, choose New Runtime.
• Then choose Apache Tomcat v9.0 and check the box Create a new local server.
• Then set the Tomcat installation directory to /opt/tomcat9/.
3. Create a new Servlet named Lab02 in the package
4. From the “Run” menu, choose “Run As”, then “Run on Server”. You should see a web page
that just displays Served at: /CS256L-LastFirst
5. Put this project into your GitHub repository (to be supplied).
• Create the directory /home/yourname/git
• Create a temporary repository in /home/yourname/git/temp.
• Add all files to the index.
• Make a local commit without pushing.
• Push branch master to your GitHub URL.
• Check the GitHub page to make sure that your files are there.
• Delete the project, including files on disk.
• Recursively delete /home/yourname/git/temp.
6. Re-clone your project from GitHub, into /home/yourname/*********
In the future, if you continue to work on your project on the same computer, you will only
need to repeat the first and last step.
However, the server that we set up when creating our project is part of the workspace, not
part of the project. So, any time you move to a different computer, or your workspace is deleted,
you will have to recreate the server – without it, your code will contain lots of compiler errors!
Here is how to do so:
1. From the “File” menu, choose “New”, then “Other”, then choose “Server” from the list.
2. Choose Apache, then Tomcat v9.0 Server from the list.
3. Choose the appropriate installation directory, as above.
If you would like to be able to work on labs on your own computer, you will need to install the
Java EE version of Eclipse. By default, it will have the same name as the normal version of Eclipse,
so I recommend that you rename it to jee, as was done in the lab. Just as we did here, you will
need to make a separate workspace from the one that normal Eclipse uses. You will also need to
download Apache Tomcat 9.0. And you will need to set up a server in your workspace.
Now that we have completed the setup, we can build a meaningful dynamic web page.
3.1 Part 1: A Simple Document
Let’s look at our servlet’s doGet method. This is automatically called every time a web client
sends a GET request to the web server for this page. (Most web requests are of the GET type; we
will learn about the POST type in a few weeks.)
You should see that this method has two parameters: request is an object that contains
information about the request that came in from the client. For now, we do not need to do
anything with it. response is an object that contains information about the message that we are
going to send back to the client – the web page that we want them to see.
There are three lines that we should always include at the beginning of the doGet method:
PrintWriter out = response.getWriter();
The first two are specifying that the document we will send back is written in HTML and
encoded using UTF. The last stores a Print Writer object that has all the methods you are
familiar with System.out having – because System.out is also a Print Writer! But printing
to this one does not cause text to appear in the terminal. Instead, it causes that text to become a
part of the document that will be sent back to the client.
Use the Print Writer to print out an entire simple HTML document. (You may want to look
at one of the documents from the last two labs to remember what that looks like.) The title
should be: ”My First Dynamic Web Page”, and for now the body can contain just a single
paragraph with the text ”A Java program wrote this document.”. When we were writing HTML
manually we used consistent indentation to make it easier to read the HTML code. When your
Java program is writing the HTML, we should try to do the same thing.
Use the Run on Server feature to confirm that your page looks correct.
3.2 Part 2: Deployment
For anyone else in the world to be able to see your document (including, crucially, the validator
tools), we need to get the code that generates it onto the web server. To do so, follow the
1. From the context menu on your project, choose “Export”, then “WAR file”. Put it in your
home directory (/home/yourname/) with the default name.
2. Copy the WAR file to the server. If you are using a Linux computer, you will do so like this
(replacing your name with your username, Last with your last name, and First with your
(That’s all one line, it just isn’t fitting across the page.)
3. Wait about one minute, then point your web browser to this URL:
If any of these steps fails, I will need to help you with debugging. If you would like to be able
to deploy from your own computer, arrange time with me to help you get it configured.
Send your generated web page through the validator and fix any problems it finds. You will
need to re-deploy every time that you make changes to your code.
3.3 Part 3: Some Random Numbers
So far, our dynamic web page is not very dynamic: no matter how many times you reload the
page, it will always be the same. In most of our work, what will make pages dynamic is the fact
that they are pulling information from the database and responding to user input. But those both
require a bunch of new technology, we will be learning about those in subsequent labs. Instead,
we will get some dynamic behavior from random numbers.
While it is used in some special ways and has some special capabilities, a servlet is just a Java
class like any other, and you can do anything in it that any other Java class can do.
Below the existing paragraph, add a level-1 heading that says, “Your lucky numbers for
Inside the doGet method, create an array of 20 integers, and fill it with 20 random numbers.
Then include in the HTML output a table in which each row contains two columns. The first
column counts down from 20 to 1, and the second displays the random numbers in the array,
from first to last. (You’ll have to do some slightly fancy arithmetic, since the first column values
are decreasing while the array indices are increasing).
Use CSS in an internal style sheet to:
• put all the random numbers (but not the counters) in a mono spaced font
• to center the table
• to put borders around all of the cells in the table
Add a paragraph below the table that says either “Your luckiest number is even!” or “Your
luckiest number is odd!”, whichever is true. The last number in the array is the user’s luckiest
Test your web page locally, and when you think it looks correct, deploy it to the server and
test it there. Make sure that when you reload the page you get new lucky numbers.
Validate both your HTML and CSS with http://validator.w3.org/ and http://jigsaw.w3.org/css-validator/ respectively. If there are any errors, fix them, redeploy,
and revalidate as necessary.
For this lab and all labs in which we build dynamic web pages, you must do the following to have
a complete submission:
• Commit and push all of the changes to your project and confirm that GitHub has them.
• Deploy the latest version of your project to the server.
• Submit to Moodle the URL of the exact page that you created.
Why Choose Us
At Myhomeworkwriters.com, we always aim at 100% customer satisfaction. As such, we never compromise the quality of our essay services. Our essay helpers ensure that they craft each paper carefully to match the requirements in the instruction form.
Professional Academic Writers
With Myhomeworkwriters.com, every student is guaranteed high-quality, professionally written papers. We ensure that we hire individuals with high academic qualifications who can maintain our quality policy. These writers undergo further training to sharpen their writing skills, making them more competent in writing academic papers.
Our company maintains a fair pricing system for all paper writing services to ensure affordability. Our pricing system generates quotations based on the properties of individual papers.
MyHomeworkWriters guarantees all students of swift delivery of papers. We understand that time is an essential factor in the academic world. Therefore, we ensure that we deliver the paper on or before the agreed date to give students ample time for reviewing.
Myhomeworkwriters.com maintains a zero-plagiarism policy. As such, MyHomeworkWriters ensure that they use the students’ instructions to deliver plagiarism-free papers. We are very keen on avoiding any chance of similarities with previous papers.
Customer Support 24/7
Our customer support works around the clock to provide students with assistance or guidance at any time of the day. Students can always communicate with us through our live chat system or our email and receive instant responses. Feel free to contact us via the Chat window or support email: email@example.com.
Try it now!
How it works?
Follow these simple steps to get your paper done
Place your order
Fill in the order form and provide all details of your assignment.
Proceed with the payment
Choose the payment system that suits you most.
Receive the final file
Once your paper is ready, we will email it to you.
Our writers complete papers strictly according to your instructions and needs, no matter what university, college, or high school you study in.
Our Essay Writing Services
MyHomeworkWriters holds a reputation for being a platform that provides high-quality academic services. All you need to do is provide us with all the necessary requirements and wait for quality results.
At MyHomeworkWriters, we have highly qualified academic gurus who will offer great assistance towards completing your essays. Our essay help service providers are well-versed with all the aspects of developing high-quality and relevant essays.
Admission and Business Papers
With Myhomeworkwriters.com, we will help you secure a position at your desired institution. Our essay writing services include the crafting of admissions papers. We will still help you climb your career ladder by helping you write the official papers that will help you secure a job. We will guide you on how to write an outstanding portfolio or resume.
Editing and Proofreading
Myhomeworkwriters.com has a professional editorial team that will help you organize your paper, paraphrase it, and eliminate any possible mistakes. Also, we will help you check on plagiarism to ensure that your final paper posses quality and originality.
My Homework Writers harbors professional academic writers from diverse academic disciplines. As such, we can develop essay help services in all academic areas. The simplicity or complexity of the paper does not affect the quality of essay writing services.