Lecture3x - Francisco R. Ortega

cabbagewheatInternet and Web Development

Nov 13, 2013 (3 years and 10 months ago)

76 views

Today’s Lecture


05/16/12


Some Java questions to do at home.


Chapter 2


Remember I have office hours Today!

Java Questions (@home)


Write logic for a controller using JSP that
calls
confirm.jsp

if:


Button was pressed and amount of total users
currently requesting the page is less that 10000


Hint USE static variable to add or remove users.


Otherwise, send to
Busy.jsp


Java Questions (@home)


Write logic for a controller using JSP that
calls
sold.jsp

if


Button buy was pressed and textbox quantity is
greater than 1.


Check that value of textbox is present before
asking if > 1

Java Questions (@home)


Write
a controller using a servlet that checks
to see if
edit,confirm

or cancel was pressed


If none pressed, send them to
BadQuery.jsp


If any of them pressed send them to their page.


Check that the button was pressed and that
value is not an empty string.

Java Questions(@home)


How do you open a
PrintWriter

and write
something to it in Java?


How do you read a file using a scanner?


Write the code that will do the following: if a
number is less than 0, display 'negative'; if a
number is greater than 100, display 'over 100';
for all other numbers, display 'valid grade'.


How do you test if a string is empty as opposed
to not existing?


Java Questions(@home)


How do you define a variable for a class
named
ShoppingCart
?


How do you create a new object for a class
named
ShoppingCart
?


Assume it takes
CartList

as parameter


Java Questions(@home)


What is an abstract class?


What is an interface ?


Create some classes (and extend them) to
show:


Abstract Classes


Use of interface


Create a class person and then student.


How do you call the base constructor of person?


File Structure

Tutorials/

WEB
-
INF/

classes/


*.class

lib/

*.jar

web.xml

index.jsp

edit.jsp

confirm.jsp

Web Applications


Web application can have many properties in
common


For the rest of the chapter, we will see


Edit page


Confirm page


Process page


For a more smooth operation


Controller class will be needed


A servlet page

Sending Data


<form
action=“
Confirm.jsp
">



Sending Data


<form
action
=“
Confirm.jsp
">


<p> Test
<
br
>



<
input type
=“text"
name="hobby"



value="${
param.hobby
}">



<
input type="
submit“ name=“
confirmBtn
"


value=“Confirm">


</
form>


Relative and Absolute Reference


Same as we sow in HTML


Questions


http://server.com/path/Edit.jsp


http://
server.com/path/Confirm.jsp


http://
server.com/utils/files/validate.jsp


What is the absolute and relative reference
for
Confirm.jsp

if called from
Edit.jsp

?


What is the absolute and relative reference
for
validate.jsp

if called from
Confirm.jsp

?

Retrieving Values (Form Element)


${
param.field
}


For example


${
param.hobby
}


Try
Edit.jsp

from book (page 38)

Hidden Field Technique


Edit.jsp

sends data to
Confirm.jsp


How to send data back to
Edit.jsp
?


Hidden Fields


<input type=“
hidden
” name=“hobby”


value = “${
param.hobby
}”>

Hidden Fields! Let’s Compare


Edit


<
input type
=“
text

name=“hobby”


value = “${
param.hobby
}”>


Confirm


<input
type=“
hidden
” name=“hobby”


value = “${
param.hobby
}”>


More in Sending Data!


How do we send data to either
edit.jsp

or
process.jsp
?


Inefficient Solution

<
form action="
Edit.jsp
">


<input type="hidden" name="hobby"


value="${
param.hobby
}">


<input type="submit" name="
editButton
"


value="Edit">

</
form>

<
form action="
Process.jsp
">


<input type="hidden" name="hobby"


value="${
param.hobby
}">


<input type="submit" name="
processButton
"


value="Process">

</
form>

Better Solution: Controller


Using a controller is better solution


Example:
Confirm.jsp


<form action="
Controller.jsp
">


<p>


<input type="hidden" name="hobby"


value="${
param.hobby
}">


<input type="submit" name="
editButton
"


value="Edit">


<input type="submit" name="
processButton
"


value="Process">




</
form>

Controller


It only contains java code


Each JSP will send its data to the controller


First, it will be shown as a JSP (for simplicity)


Objects:


HttpServletRequest


HttpServletResponse


Reference Parameters:


Request.getParameter
(“hobby”)

Testing Button


Button clicked will show in the query string


If (
request.getParameter
(“
processButton
”) != null)

Controller Logic

1.
Each form’s actions is mapped to controller

2.
E
ach submit button has unique name

3.
Controller decides which page to call next

4.
Controller knows URL of all JSP that controls

Controller Logic


if (
request.getParameter
("
processButton
") != null
)


{



address = "
Process.jsp
";


}


else
if (
request.getParameter
("
confirmButton
") != null
)


{



address = "
Confirm.jsp
";


}


else


{



address = "
Edit.jsp
";


}

Forward Control!


RequestDispatcher

dispatcher =
request.getRequestDispatcher
(address
);



d
ispatcher.forward
(
request,response
)

JSP or Servlet?


You can use either one


Easier to read in JSP


Better performance in Servlet


See Controller Code page 50 (or next slide)

Controller Code JSP

<%


String address;




if (
request.getParameter
("
processButton
") != null)


{


address = "
Process.jsp
";


}


else if (
request.getParameter
("
confirmButton
") != null)


{


address = "
Confirm.jsp
";


}


else


{


address = "
Edit.jsp
";


}


RequestDispatcher

dispatcher =

request.getRequestDispatcher
(address
);



dispatcher.forward
(request, response);

%>


Confirm + Controller


<form
action="
Controller.jsp
"
>


<p>


<input type="hidden" name="hobby"


value="${
param.hobby
}">


<input type="submit" name="
editButton
"


value="Edit">


<input type="submit" name="
processButton
"


value="Process">




</form>


JSP
vs

Java Servlet

JSP


It will be recreated each
time


Prefer when lots of HTML
and little JSP


Easy to write HTML


Hard to debug

Java Servlet


Does not have to recreate


Prefer when lots Java and
little HTML


Java IDE very useful

More JSP/Servlet


If you have lots of HTML and JAVA


Redesign your site:


Write more code in the controller


JAVA CLASSPATH


Windows



setx

CLASSPATH=
classpath1
;
classpath2
...


Linux/Unix/
MacOsX


setenv

CLASSPATH

classpath1
:
classpath2
...



Classpath

+ Packages


http://users.cs.fiu.edu/~downeyt/webdev/p
ackages.html


http://javarevisited.blogspot.com/2011/01/h
ow
-
classpath
-
work
-
in
-
java.html


http://docs.oracle.com/javase/1.4.2/docs/to
oldocs/solaris/classpath.html


http://docs.oracle.com/javase/1.4.2/docs/to
oldocs/solaris/classpath.html

More about Packages


If your class directory is called “classes”


What is the name of the package for
classes/store?


Store


How about classes/store/hardware ?


store.hardware


Controller Servlet


Place servlet in a package


Location should not be in the
default package


Import the following classes:


import
java.io.IOException
;


import
javax.servlet.ServletException
;


import
javax.servlet.http.HttpServlet
;


import
javax.servlet.http.HttpServletRequest
;


import
javax.servlet.http.HttpServletResponse
;


import
javax.servlet.RequestDispatcher
;



Controller Servlet


Extend
HttpServlet


public class Controller
extends

HttpServlet

{ …. }


Override some methods of
HttpServlet

Class


Place controller logic in this method:

@Override


protected void
doGet
(
HttpServletRequest

request,


HttpServletResponse

response)


throws
ServletException
,
IOException

{


...


}



Controller Logic


Override
doGet

or
doPost

methods

@Override


protected void
doGet
(
HttpServletRequest



request,HttpServletResponse

response)


throws
ServletException
,
IOException

{


...


}


Servlet Controller Code (page 55)

public
class Controller extends
HttpServlet

{


protected void
doGet
(
HttpServletRequest

request,HttpServletResponse

response)




throws
ServletException
,
IOException


{


if
(
request.getParameter
("
processButton
") != null)


{


address
= "../
Process.jsp
";



}



else
if (
request.getParameter
("
confirmButton
") != null)


{



address = "../
Confirm.jsp
";


}



else



{



address
= "../
Edit.jsp
";


}


RequestDispatcher

dispatcher
=
request.getRequestDispatcher
(address);


dispatcher.forward
(request
, response);


}

}

Jsp

+ Servlet


<form action=“
Controller
”>


What’s the difference?

Servlet Location


Source file could be anywhere


.class file must be in subdirectory classes


classes directory must already exist in
CLASSPATH


Define mapping in web.xml


JSP must placed in the folder that is
referenced


Servlet Identity FQN


Fully Qualified Name (FQN)


is an unambiguous name that specifies which
object, function, or variable a call refers
to (WIKI)


The package and the class name are
combined to define the unique name of a
class.

Servlet Identity FQN


We can use the FQN to define a short name
for tomcat to be used internally


<servlet>


<servlet
-
name>
Ch2Controller


</servlet
-
name>


<
servlet
-
class>ch2.servletController.Controller


</
servlet
-
class>


</servlet>

Servlet Access


Create a short name for servlet in web.xml


See previous slide


Create a mapping


<servlet
-
mapping>


<servlet
-
name>
Ch2Controller
</servlet
-
name
>


<
url
-
pattern
>
/ch2/
servletController
/Controller
</
url
-
pattern>


</
servlet
-
mapping>

More about Servlet Mapping

1.
If the next JSP is in the same directory
where the controller is mapped


address=“
Confirm.jsp


2.
If is not in the same directory


address=“/ch2/
servletController
/
Confirm.jsp


Servlet mapping

WAR Files


W
eb
application

AR
chive

(WAR)


http
://users.cis.fiu.edu/~
downeyt/webdev/cl
assPage.shtml?CSS


If you scroll down to Tomcat Information


Deploying a WAR file


Web Servlet Annotation


One may skip web.xml mapping


It does not work in all cases.


If you do the mapping in web.xml


Must remove the annotation from the class

Web Servlet Annotation


Example

@
WebServlet
(
urlPatterns
={"/ch2/
servletController
/annotated/Controller"})

public class Controller extends
HttpServlet

{



}


Servlet Engine for Servlets


Developer must re
-
compiled


Default behaviors does not update .class


Some systems may be configured to
automaticly

update


Servlet in IDEs


After you build web project


Execute them


When WAR file is built


Source packages will be copied to classes


To export WAR File


File


Export


Web


War File


Class Files


Avoid using default package


Only keep your configuration files


If web.xml is not created


File
-
>New
-
> Standard Deployment
Desctiptor


Questions


Any Questions


Review your java!