1 / 18

Servlets

Servlets. Enterprise Systems Programming. 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

dlemos
Download Presentation

Servlets

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. 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:<context-root-folder-name> • html file(s) referring to servlet pages • WEB-INF • web.xml • classes • <package-name> • 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 resideinside mywebapp) contains mapping(s) of URLto actual servlet code servlet code could be an elaboratepackage folder hierarchy

  6. Web-application example • Simple Example:mywebapp • welcomeform.html • WEB-INF • web.xml • classes • servlets • WelcomeServlet.class … <form action="/mywebapp/welcome" … maps "/welcome"to servlets.WelcomeServlet

  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 <web-app> <servlet> <servlet-name>welcome</servlet-name> <servlet-class> servlets.WelcomeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>welcome</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> </web-app>

  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 <form action="/mywebapp/welcome" method="get"> TYPE IN SOME TEXT <input type = "text" name ="firstname" /> <input type = "submit" value="Click" /> </form>

  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( "<h2> Welcome </h2>" );

  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( "<h2> Welcome " + name + "</h2>" ); 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( "<h2> Welcome " + name + "</h2>" ); out.println( "Your MOBILE number is " + mobileeNumber ); out.close(); • } getNum() is a method ofDBAccess.javaand containsJDBC 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)

More Related