Download presentation
Presentation is loading. Please wait.
Published byCaroline Andrews Modified over 9 years ago
1
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg
2
Requesting a Web Page over the Internet: (two-tiered architecture) Works best for smaller companies Client requests information User interface usually here Server receive information and processes it Data Base services are usually on a server
3
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.
4
Many Software Options php jsp servlets CGI Server side javascript Microsoft active server pages VBScript Perl AND MORE
5
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 Server Request for CGI Internet
6
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
7
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
8
Hosted in a container. Part of JAVA Servlets
9
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)
10
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.
11
Several methods in these packages below are called automatically import javax.servlet.*; import javax.servlet.http.*;
12
Java Servlets Life Cycle 1) init () method is called as “life” begins 2) services () services all requests and is first method called a) Normally this method is not implemented directly by programmer b) In our applications we will be using service in HttpServlet class 3) destroy () is called at the end servlet’s life
13
HttpServlet Defines processing for servlets that extend functionality of a WEB server. We will extend HttpServlet class
14
HttpServlet The methods in HttpServlet to accomplish tasks are called by service (): doGet doPost (Others are on Page 432 of text, Table 11.1)
15
Arguments of doPost and doGet Make it easy to access data HttpServletRequest and HttpServletResponse
16
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”
17
HttpServlet doPost: to post data to a WEB server Subtle difference – post will not cache the information Often used to obtain form information
18
Lets Try It! Create a new Java project in the Eclipse Development Environment Project Name: WelcomeServletSP07 Next
19
Adding servlet library to project! Libraries Add External Jar In TomCat – Common/lib Servlet-api.jar
20
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.*;
21
Alternately import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException; import java.io.PrintWriter;
22
Add global variables public class WelcomeServletSP07 extends HttpServlet { static final private String CONTENT_TYPE = "text/html"; String firstName; String lastName; String emailName;
23
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();
24
Complete doGet Method out.println (" " + " " + " "); out.println(" "); out.println( " Processing get requests with data " ); out.println( " " ); out.println( " Hello " + firstName + " " + lastName + " " ); out.println( "Email Address: " + emailName + " " ); out.println(" The servlet has received a GET. " + "This is the reply. "); out.println( " " ); out.close(); }
25
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!
26
{ 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. }
27
{. out.println (" " + " " + " "); out.println(" "); out.println( " Processing get requests with data " ); out.println( " " ); } //Creates an html document which has //the response to the get
28
Processing get requests with data <form action="../servlet/WelcomeServletSP07" method="get"> Type information requested and press the Submit button First Name: Last Name: Email Address:<input type ="text" name="emailName" size="20"/>
29
1.Add code to obtain: a)streetName b)cityName c)zipName from the form Add code to print all information on the screen
30
Lab Completion! 1.Expand the form on the html page to include the needed inputs.
31
TomCat Configuration
32
1.WEB-INF (in servlet-examples) a.classes – places.class file in this location b.May want to keep.java files here also 2.servlets-examples directory a.Place.html files in this directory
33
http://http://localhost/servlets- examples/ WelcomeServletSP07.html 1.Update the web.xml file according to instructions for TomCat 2.Your Servlet can now be run in a browser:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.