1 / 33

INFSY 547: WEB-Based Technologies

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.

Download Presentation

INFSY 547: WEB-Based Technologies

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg INFSY 547: WEB-Based Technologies

  2. 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

  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 Internet Server Request for CGI

  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. Servlets • Hosted in a container. • Part of JAVA

  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 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

  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 forminformation

  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 ("<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(); } }

  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. 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 . . . }

  27. 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

  28. <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

  29. Add code to obtain: • streetName • cityName • zipName • from the form • Add code to print all information on the screen

  30. Lab Completion! • Expand the form on the html page to • include the needed inputs.

  31. TomCat Configuration

  32. 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

  33. 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

More Related