330 likes | 427 Views
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg. INFSY 547: WEB-Based Technologies. Requesting a Web Page over the Internet: (two-tiered architecture). Server receive information and processes it Data Base services are usually on a server.
E N D
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg INFSY 547: WEB-Based Technologies
Requesting a Web Pageover the Internet:(two-tiered architecture) Server receive information and processes it Data Base services are usually on a server Client requests information User interface usually here Works best for smaller companies
Client/Server Software • Multi-tier architecture • Improves performance for larger numbers of users • Middle tier can perform services e.g. • queuing, • application execution, • database • The middle layer may also add scheduling and prioritization handling.
Many Software Options php jsp servlets CGI Server side javascript Microsoft active server pages VBScript Perl AND MORE
Common Gateway Interface (CGI) Protocol used between a browser and server software Has been a standard for interfacing external applications with information servers, such as HTTP servers. See Chapter 10 of your text Internet Server Request for CGI
Java Servlets Advantages Time Portable Powerful Developed in an object-oriented language Secure (security provided by WEB server and also coding) Platform independent Work well with databases
Java Server Pages • Java Server Pages (JSP) • Extension of servlets • Facilitates use of “scriptlets” of Java • code directly on the page • Compiled into servlets • Chapter 11, 456-470
Servlets • Hosted in a container. • Part of JAVA
TomCat • A container for hosting Java Applications: • Java servlets • jsp applications • A nested environment of component parts • We will set up and use along with Oracle database environment • accessed via the servlet • accessed via lab • TomCat container may be downloaded • (see TomCat Instructions attached to Syllabus)
Java Servlets Architecture Two packages are important to our use of servlets GenericServlet (import javax.servlet) HttpServlet (import javax.servlet.http) Most servlets extend one or the other WEB servlets usually extend HttpServlet.
Several methods in these packages below are called automatically import javax.servlet.*; import javax.servlet.http.*;
Java Servlets Life Cycle init () method is called as “life” begins services () services all requests and is first method called Normally this method is not implemented directly by programmer In our applications we will be using service in HttpServlet class destroy () is called at the end servlet’s life
HttpServlet Defines processing for servlets that extend functionality of a WEB server. We will extend HttpServlet class
HttpServlet The methods in HttpServlet to accomplish tasks are called by service (): doGet doPost (Others are on Page 432 of text, Table 11.1)
Arguments of doPost and doGet Make it easy to access data HttpServletRequest and HttpServletResponse
HttpServlet Requests: contain data passed between the client and the server Common Request Types: doGet : to supply data as part of the request Parameters are passed to a servlet as name/value pairs Caches the data obtained for fast processing Next Page for the “doPost”
HttpServlet doPost: to post data to a WEB server Subtle difference – post will not cache the information Often used to obtain forminformation
Lets Try It! Create a new Java project in the Eclipse Development Environment Project Name: WelcomeServletSP07 Next
Adding servlet library to project! Libraries Add External Jar In TomCat – Common/lib Servlet-api.jar
Create a WelcomeServletSP07 class in the IDE import javax.servlet.*; import javax.servlet.http.*; //handles get and post requests //implements servlet.Servlet import java.io.*;
Alternately import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException; import java.io.PrintWriter;
Add global variables public class WelcomeServletSP07 extends HttpServlet { static final private String CONTENT_TYPE = "text/html"; String firstName; String lastName; String emailName;
Create doGet Method/initialize variables protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { firstName = request.getParameter( "firstName" ); lastName = request.getParameter ("lastName"); emailName = request.getParameter("emailName"); response.setContentType( CONTENT_TYPE); PrintWriter out = response.getWriter();
Complete doGet Method out.println ("<html>" + "<head><title>" + "</title></head>"); out.println("<body>"); out.println( "<title>Processing get requests with data</title>" ); out.println( "</head>" ); out.println( "<body>" ); out.println( "<p><br/><br/><h3>Hello " + firstName + " " + lastName + "<br />" ); out.println( "Email Address: " + emailName + "</h3>" ); out.println("<p>The servlet has received a GET. " + "This is the reply.</p>"); out.println( "</body>" ); out.println( "</html>" ); out.close(); } }
doGet Procedure overridden ___________ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException //if doGet cannot handle a response or encounters //any error, it will throw an exception!
Explanations { response.setContentType(CONTENT_TYPE); //Note: String CONTENT_TYPE = "text/html“ //indicates that the response goes to a browser PrintWriter out = response.getWriter(); //getWriter () obtains a reference to PrintWriter //to send output to the client . . . }
Explanations { . . out.println ("<html>" + "<head><title>" + "</title></head>"); out.println("<body>"); out.println( "<title>Processing get requests with data</title>" ); out.println( "</head>" ); } //Creates an html document which has //the response to the get
<html> <head><title>Processing get requests with data</title></head> <body> <form action="../servlet/WelcomeServletSP07" method="get"> <TABLE width="85%" style="background-color:#eff7ff" border="0"> <TR><TD width="100%" style="text-align:center"><br/><br/> <br/>Type information requested and press the Submit button <br /><br/><br/> First Name: <input type = "text" name = "firstName" size="20"/> <br /> Last Name: <input type = "text" name = "lastName" size="20"/> <br /><br/><br/><br/> </TD></TR> <TR><TD width="100%" style="text-align:left"> <br/> Email Address:<input type ="text" name="emailName" size="20"/> <br/></br> <input type = "submit" value = "Submit" /> </TD></TR> </form> </body> </html> SampleHTML
Add code to obtain: • streetName • cityName • zipName • from the form • Add code to print all information on the screen
Lab Completion! • Expand the form on the html page to • include the needed inputs.
WEB-INF (in servlet-examples) • classes – places .class file in this location • May want to keep .java files here also • servlets-examples directory • Place .html files in this directory
Update the web.xml file according to instructions for TomCat • Your Servlet can now be run in a browser: http://localhost/servlets-examples/WelcomeServletSP07.html