Java Servlets Joe Komar 1142022 Komar Associates 1

  • Slides: 32
Download presentation
Java Servlets Joe Komar 1/14/2022 Komar Associates 1

Java Servlets Joe Komar 1/14/2022 Komar Associates 1

Servlets Overview n n n n n Java programs Stored and executed on the

Servlets Overview n n n n n Java programs Stored and executed on the server Additional “helper” classes Part of javax. servlet java. servlet. http API Memory resident Server needs to support it Can have persistent connection to a database Can do servlet chaining SDK from Sun needed 1/14/2022 Komar Associates 2

A Simple Servlet - HTML <html> <head><Title>Komar's first servlet</title></head> <body> <form action=http: //140. 209.

A Simple Servlet - HTML <html> <head><Title>Komar's first servlet</title></head> <body> <form action=http: //140. 209. 124. 107: 8080/servlet/JAK 1 method=POST> Enter your name: <input type=text name=name> Enter your favorite movie: <input type=text name=movie> Thanks! <input type=submit> </form> </body> </html> 1/14/2022 Komar Associates 3

A Simple Servlet - Browser Screen 1/14/2022 Komar Associates 4

A Simple Servlet - Browser Screen 1/14/2022 Komar Associates 4

A Simple Servlet - The Code import java. io. *; import javax. servlet. http.

A Simple Servlet - The Code import java. io. *; import javax. servlet. http. *; 1/14/2022 Komar Associates 5

A Simple Servlet - The Code public class JAK_Servlet 1 extends Http. Servlet {

A Simple Servlet - The Code public class JAK_Servlet 1 extends Http. Servlet { public void do. Post (Http. Servlet. Request req, Http. Servlet. Response res) throws Servlet. Exception, IOException { Servlet. Output. Stream out = res. get. Output. Stream(); res. set. Content. Type("text/html"); String name = req. get. Parameter("name"); String movie = req. get. Parameter("movie"); out. println("<html><head><title>Movies</title></head>"); out. println("<body><h 1>What you said. . </h 1><hr>"); out. println("<p>Name: " + name); out. println("<p>Movie: " + movie); out. println("</body></html>"); out. close(); } 1/14/2022 Komar Associates 6 }

A Simple Servlet -- Output 1/14/2022 Komar Associates 7

A Simple Servlet -- Output 1/14/2022 Komar Associates 7

Servlet Development at UST n Create the Form (or Java Applet) u Action =

Servlet Development at UST n Create the Form (or Java Applet) u Action = POST 140. 209. 124. 107: 8080/servletname n FTP it to the server: u IP address 140. 209. 124. 107 u Username -- class (case sensitive) u Password -- skeeter (case sensitive) u Change to “public_html” directory, then to your directory (three initials) u Put HTML file there 1/14/2022 Komar Associates 8

Servlet Development at UST n n n Check out your form -140. 209. 124.

Servlet Development at UST n n n Check out your form -140. 209. 124. 107: 8080/initials/pagename. html Write your servlet (put package initials; as the first line) and compile it Move the. class file to the server u Same IP, username, and password u Change to servlets directory, then your subdirectory (initials) and put it there 1/14/2022 Komar Associates 9

Servlet Development at UST n Register your servlet on the server u From browser,

Servlet Development at UST n Register your servlet on the server u From browser, enter address: 140. 209. 124. 107: 9090 u Login as user “admin”, password “testitout” u Make sure “Web Service” is highlighted then click on “Manage” u Click on “Servlets” button on top of page u Click on “Add” choice in list 1/14/2022 Komar Associates 10

Servlet Development at UST n Registering your servlet (continued): u u u u 1/14/2022

Servlet Development at UST n Registering your servlet (continued): u u u u 1/14/2022 The “Name” field is the name you use in your form’s Action attribute -- e. g. , JAK 1 The class is the name of the class file you transferred with your initials, period prepended and without the. class extension -- e. g. , jak. Joe. One Click on the “Add” button Enter whatever you want in the “description” Click on “Save” Close the Web Service window Click on the “Log Out” button Komar Associates 11

Servlet Development at UST n n Check out your Form/servlet application Hints: u Make

Servlet Development at UST n n Check out your Form/servlet application Hints: u Make sure you type things like the name of the class correctly on the server u Keep the FTP session open when you check things out -- will probably have to make changes and FTP things again u Make sure you hit the Reload or Refresh buttons after FTP changes n Don’t go changing other server settings!! 1/14/2022 Komar Associates 12

Servlet Assignment n Create an HTML form that has the following: u Text box

Servlet Assignment n Create an HTML form that has the following: u Text box for Operator (+ - * /) (select box? ? ) u Two text boxes for operands u Submit button u Action is a POST to a named servlet n Write the Java Servlet u Takes the operation and operand, performs the requested operation u Creates a web page to show the inputs and results 1/14/2022 Komar Associates 13

Servlet JDBC Example HTML <html> <head><Title>Komar's Directory Servlet</title></head> <body> <form action=http: //140. 209. 124.

Servlet JDBC Example HTML <html> <head><Title>Komar's Directory Servlet</title></head> <body> <form action=http: //140. 209. 124. 107: 8080/servlet/JAK 3 method=POST> Enter the name or a portion thereof: <input type=text name=name> Thanks! <input type=submit> </form> </body> </html> 1/14/2022 Komar Associates 14

Servlet JDBC Example Browser 1/14/2022 Komar Associates 15

Servlet JDBC Example Browser 1/14/2022 Komar Associates 15

Servlet JDBC Example Servlet package jak; import java. io. *; import javax. servlet. http.

Servlet JDBC Example Servlet package jak; import java. io. *; import javax. servlet. http. *; import java. sql. *; 1/14/2022 Komar Associates 16

Servlet JDBC Example Servlet public class JAK_Directory extends Http. Servlet { public void do.

Servlet JDBC Example Servlet public class JAK_Directory extends Http. Servlet { public void do. Post (Http. Servlet. Request req, Http. Servlet. Response res) throws Servlet. Exception, IOException{ Servlet. Output. Stream out = res. get. Output. Stream(); res. set. Content. Type("text/html"); String name = req. get. Parameter("name"); 1/14/2022 Komar Associates 17

Servlet JDBC Example Servlet String url = "jdbc: odbc: Directory"; String query = "select

Servlet JDBC Example Servlet String url = "jdbc: odbc: Directory"; String query = "select * from STUDIR where Name like " + "'%" + name + "%'"; Statement stmt; Connection con; try { Class. for. Name("sun. jdbc. odbc. Jdbc. Odbc. Driver"); } catch (java. lang. Class. Not. Found. Exception e){ System. err. print("Driver class not found"); } 1/14/2022 Komar Associates 18

Servlet JDBC Example Servlet try { con = Driver. Manager. get. Connection(url); stmt =

Servlet JDBC Example Servlet try { con = Driver. Manager. get. Connection(url); stmt = con. create. Statement(); Result. Set rs = stmt. execute. Query(query); out. println("<html><head><title>Query Result</title></head>"); out. println("<center><h 1>Results of query for " + "'" +name + "'</h 1></center>"); out. println("<hr>"); 1/14/2022 Komar Associates 19

Servlet JDBC Example Servlet while (rs. next()) { out. println("<p>Name: " + rs. get.

Servlet JDBC Example Servlet while (rs. next()) { out. println("<p>Name: " + rs. get. String(1)); out. println(" Username: " + rs. get. String(2)); out. println(" College Code: " + rs. get. String(3)); } stmt. close(); con. close(); } catch(Exception ex) { System. err. print("SQL Exception: " + ex. get. Message()); } } } 1/14/2022 Komar Associates 20

Servlet JDBC Example Output 1/14/2022 Komar Associates 21

Servlet JDBC Example Output 1/14/2022 Komar Associates 21

Core Servlet Interfaces and Class n Servlet interface; u Methods all servlets must implement

Core Servlet Interfaces and Class n Servlet interface; u Methods all servlets must implement u Typically extend Generic. Servlet or Http. Servlet classes u Server initializes the servlet with the init() method, asks for services with the service() method, destroys a it with the destroy() method u get. Servlet. Config() and get. Servlet. Info() 1/14/2022 Komar Associates 22

Core Servlet Interfaces and Class n Servlet. Config interface: u access to configuration data

Core Servlet Interfaces and Class n Servlet. Config interface: u access to configuration data u get. Init. Parameter(name) -- value of parameter u get. Init. Paramenter. Names() -- Enumeration of parameter names u get. Server. Context() -- returns a Servlet. Context object 1/14/2022 Komar Associates 23

Core Servlet Interfaces and Class n Servlet. Context interface: u access to information about

Core Servlet Interfaces and Class n Servlet. Context interface: u access to information about the environment u get. Attribute(name) -- value of the named attribute of the network service u get. Mime. Type(file) -- MIME type of the specified file u get. Real. Path(virtual-path) -- returns real path u get. Server. Info() -- name version of server u get. Servlet(name) -- servlet named u get. Servlets() -- Enumeration of Servlets objects 1/14/2022 Komar Associates 24

Core Servlet Interfaces and Class n Generic. Servlet class: u implements Servlet and Servlet.

Core Servlet Interfaces and Class n Generic. Servlet class: u implements Servlet and Servlet. Config interfaces u provides init(), destroy(), Servlet. Config methods, and log() method u must override and implement the service() method 1/14/2022 Komar Associates 25

Core Servlet Interfaces and Class n Servlet. Request interface: u access data passed from

Core Servlet Interfaces and Class n Servlet. Request interface: u access data passed from the client to the servlet u contains parameter names, parameter values, attributes, and a Servlet. Input. Stream u get. Input. Stream() -- returns a Servlet. Input. Stream object for reading the request body u many other methods to get information about the request and its context 1/14/2022 Komar Associates 26

Core Servlet Interfaces and Class n Servlet. Response interface: u to return response data

Core Servlet Interfaces and Class n Servlet. Response interface: u to return response data to client u get. Output. Stream() -- returns an output stream to write to u set. Content. Length() -- sets content length for the response u set. Content. Type() -- sets the content type for the response 1/14/2022 Komar Associates 27

Core Servlet Interfaces and Class n Servlet. Input. Stream class: u read. Line() n

Core Servlet Interfaces and Class n Servlet. Input. Stream class: u read. Line() n -- to read into an array of bytes Servlet. Output. Stream class: u overloaded print() and println() methods available 1/14/2022 Komar Associates 28

HTTP Servlet Interfaces and Classes n Http. Servlet abstract class: u extends Generic. Servlet

HTTP Servlet Interfaces and Classes n Http. Servlet abstract class: u extends Generic. Servlet u your class extends this one and override at least one method u do. Post() and do. Get() most commonly overridden u to support HTTP 1. 1 commands (OPTION, PUT, DELETE, TRACE), override the service() method 1/14/2022 Komar Associates 29

HTTP Servlet Interfaces and Classes n Http. Servlet. Request interface: u inherits from Servlet.

HTTP Servlet Interfaces and Classes n Http. Servlet. Request interface: u inherits from Servlet. Request u methods to extract HTTP header information u get. Date. Header(), get. Header. Names(), get. Path. Info(), get. Remote. User(), etc. u get. Parameter(name) -- returns the String value of the named parameter 1/14/2022 Komar Associates 30

HTTP Servlet Interfaces and Classes n Http. Servlet. Response interface: u inherits from Servlet.

HTTP Servlet Interfaces and Classes n Http. Servlet. Response interface: u inherits from Servlet. Response u variables for error standard error conditions u most of the response header information is standard and need not change 1/14/2022 Komar Associates 31

HTTP Servlet Interfaces and Classes n Http. Utils class: u collection of HTTP utility

HTTP Servlet Interfaces and Classes n Http. Utils class: u collection of HTTP utility methods u get. Request. URL() -- URL of the Http. Servlet. Request object u parse. Post. Data() -- returns POST’s form data in a Hashtable u parse. Query. String() -- returns a Hashtable of name/value pairs 1/14/2022 Komar Associates 32