<< Chapter < Page Chapter >> Page >

Tedious and error prone

Writing raw HTML code is not fun even under the best of circumstances. That is why I use a WYSIWYG HTML editor for authoring these modules.

Even for this very simple servlet, the process of constructing the output HTML code using Java println statements was very tedious and error prone. Much of the difficulty arises from the fact that Java strings must be enclosed inmatching double quotes and all HTML attribute values must also be enclosed in matching double or single quotes. As you can see from Listing 16 , the requirement to escape double quotes inside Java strings with the backslashcharacter can lead to some very confusing code. (I suppose it might be less confusing if I were to use single quotes instead of double quotes on HTML attributevalues. I will try that in the next module.)

In any event, the next section presents a JSP version of the same servlet. JSP intermingles HTML code with Java code and greatly reduces the amount of HTMLcode that must be constructed using Java println statements in a servlet. If you are unfamiliar with JSP, you can learn about it in the modules that begin at Java4305: Preface to JSP .

The JSP program

A complete listing of the JSP version of this program is provided in Listing 17 . I found this JSP version much easier to write than the servlet version discussed above. Admittedly, however, I wrote the servlet version first, so that probablyhelped to make it easier to write the JSP version.

I wrote all of the HTML code using Microsoft Expression Web 4, which provides both a WYSIWYG capability and a raw HTML editing capability. Once I confirmed that theHTML code was correct and valid, I went back and inserted the JSP tags containing the Java code.

As mentioned earlier, the JSP does not get and use the name of the computer on which it is running. Instead it uses the generic name localhost, which limitsthe ability to access the JSP from another computer on the network.

Interesting code fragments

Once again, I will discuss this program in fragments. A complete listing of the program is provided in Listing 17 .

Create basic web page structure

Listing 10 shows the HTML code that is roughly equivalent to the Java code in Listing 4 through Listing 7 . This code creates the HTML header and the basic structure of the web page shownin Figure 1 .

Listing 10 - Create basic web page structure.
<html><head><title>Java4550a</title></head><body><form method="get" action="http://localhost:8080/Java4550a.jsp"><p>Enter a name and press the button</p><p>Name:<input type="text" name="firstName"/></p><input type="submit" value="Submit Name"/><p>Your list of names is:<br/>

A JSP scriptlet

Listing 11 is a scriptlet containing pure Java code. It begins by getting the data from the request object. Then it deals withthe situation where nothing has been entered into the text field in Figure 1 , reporting that the list is empty. It ends with the beginning of a for loop that is executed when the array is not empty. Recall that in JSP, Java code blocks can begin in one scriptlet andend in another scriptlet later in the page. That is what is happening here.

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask