Presentation is loading. Please wait.

Presentation is loading. Please wait.

Servlets Enterprise Systems Programming. Servlets  Servlets: server-side Java programs that enable dynamic processing of web-based requests  Web-based.

Similar presentations


Presentation on theme: "Servlets Enterprise Systems Programming. Servlets  Servlets: server-side Java programs that enable dynamic processing of web-based requests  Web-based."— Presentation transcript:

1 Servlets Enterprise Systems Programming

2 Servlets  Servlets: server-side Java programs that enable dynamic processing of web-based requests  Web-based requests are coursed through html forms  Servlets process these requests and typically generates html-formatted responses to these requests  Servlets resides on a web server that supports servlet functionality e.g., Apache Tomcat These servers are also called web containers or servlet containers

3 Web application structure  A web application consists of several files placed inside a context root folder  Structure: html file(s) referring to servlet pages WEB-INF  web.xml  classes   Java servlet class file(s)

4 Web-application example  Simple Example: mywebapp welcomeform.html WEB-INF  web.xml  classes  servlets  WelcomeServlet.class

5 Web-application example  Simple Example: mywebapp welcomeform.html WEB-INF  web.xml  classes  servlets  WelcomeServlet.class refers to servlet page (html source need not reside inside mywebapp) contains mapping(s) of URL to actual servlet code servlet code could be an elaborate package folder hierarchy

6 Web-application example  Simple Example: mywebapp welcomeform.html WEB-INF  web.xml  classes  servlets  WelcomeServlet.class …


7 web.xml contents  web.xml: deployment descriptor  Most important portions: Establishing aliases: associate servlet name to actual servlet code Mapping: associates a URL to a servlet name

8 web.xml example welcome servlets.WelcomeServlet welcome /welcome

9 HTML forms  HTML form: section of a webpage that contains input elements (e.g., text boxes)  Often contains a submit element (a button) that enables submission of the form contents to a web server  Submission is associated with an action (the URL of the page that processes the form)

10 HTML form example TYPE IN SOME TEXT

11 Form methods  Two types of form submission methods  GET: form data is appended to the URL (data separated from the action URL by question mark) Use this for query-type actions or indempotent actions  POST: form data is “passed” or included as a message to the web-server Use this for actions with side-effects

12 HttpServlet class  Class that servlets extend  Expect to override at least one of the following methods: protected doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException protected doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException  Read form data through the request parameter, generate output/resulting webpage through the response parameter

13 HttpServletRequest  Most important method: getParameter()  Given the input parameter name (indicated in the HTML form), returns a string that represents the associated form data  May need to use Java conversion features (such as Integer.parseInt()) to convert to the appropriate type for processing  Examples: String name = request.getParameter( "firstname" ); int year = Integer.parseInt( request.getParameter( "year" ) );

14 HttpServletResponse  Used to facilitate result of processing a form  “generates” html content  Invoke the getWriter() method to get a PrintWriter handle, then issue print, println methods on that handle  Invoke getContentType(“text/html”) to specify that you are generating html content  Example: response.setContentType( "text/html" ); PrintWriter out = response.getWriter(); out.println( " Welcome " );

15 Complete doGet() example protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { String name = request.getParameter( "firstname" ); int favoriteNumber = name.length(); response.setContentType( "text/html" ); PrintWriter out = response.getWriter(); out.println( " Welcome " + name + " " ); out.println( "Your favorite number is “ + favoriteNumber ); out.close(); }

16 Connecting to a database  Combine JDBC concepts and servlets  Better to separate code that connect to the database Tip: have a collection of methods that carry out query or update methods on the database and then invoke these methods from the servlet  Database code could be in a separate package or be part of the package containing servlet code

17 doGet() with database access protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { String name = request.getParameter( "firstname" ); String mobileNumber; try { mobileNumber = DBAccess.getNum( name ); } catch( Exception e ) { mobileeNumber = "no mobile number"; } response.setContentType( "text/html" ); PrintWriter out = response.getWriter(); out.println( " Welcome " + name + " " ); out.println( "Your MOBILE number is " + mobileeNumber ); out.close();  } getNum() is a method of DBAccess.java and contains JDBC code

18 Summary  Servlets process form data through java Java programs that extend HttpServlet  Servlets are part of a web application web.xml indicates appropriate mappings  Other servlet features: redirection, sessions/cookies  Next: JSP (simplifies servlet coding)


Download ppt "Servlets Enterprise Systems Programming. Servlets  Servlets: server-side Java programs that enable dynamic processing of web-based requests  Web-based."

Similar presentations


Ads by Google