Servlets Enterprise Systems Programming Servlets p p Servlets
- Slides: 18
Servlets Enterprise Systems Programming
Servlets p p 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 n n e. g. , Apache Tomcat These servers are also called web containers or servlet containers
Web application structure A web application consists of several files placed inside a context root folder p Structure: <context-root-folder-name> p n n html file(s) referring to servlet pages WEB-INF web. xml p classes p § <package-name> § Java servlet class file(s)
Web-application example p Simple Example: mywebapp n n welcomeform. html WEB-INF web. xml p classes p § servlets § Welcome. Servlet. class
Web-application example p Simple Example: refers to servlet page (html source need not reside inside mywebapp) mywebapp n n welcomeform. html WEB-INF web. xml p classes p contains mapping(s) of URL to actual servlet code § servlets § Welcome. Servlet. class could be an elaborate package folder hierarchy servlet code
Web-application example p Simple Example: mywebapp n n welcomeform. html WEB-INF web. xml p classes p … <form action="/mywebapp/welcome" … maps "/welcome" to servlets. Welcome. Servlet § servlets § Welcome. Servlet. class
web. xml contents web. xml: deployment descriptor p Most important portions: p n n Establishing aliases: associate servlet name to actual servlet code Mapping: associates a URL to a servlet name
web. xml example <web-app> <servlet-name>welcome</servlet-name> <servlet-class> servlets. Welcome. Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>welcome</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> </web-app>
HTML forms HTML form: section of a webpage that contains input elements (e. g. , text boxes) p Often contains a submit element (a button) that enables submission of the form contents to a web server p Submission is associated with an action (the URL of the page that processes the form) p
HTML form example <form action="/mywebapp/welcome" method="get"> TYPE IN SOME TEXT <input type = "text" name ="firstname" /> <input type = "submit" value="Click" /> </form>
Form methods Two types of form submission methods p GET: form data is appended to the URL (data separated from the action URL by question mark) p n p Use this for query-type actions or indempotent actions POST: form data is “passed” or included as a message to the web-server n Use this for actions with side-effects
Http. Servlet class p p Class that servlets extend Expect to override at least one of the following methods: n n p protected do. Get( Http. Servlet. Request request, Http. Servlet. Response response ) throws Servlet. Exception, IOException protected do. Post( Http. Servlet. Request request, Http. Servlet. Response response ) throws Servlet. Exception, IOException Read form data through the request parameter, generate output/resulting webpage through the response parameter
Http. Servlet. Request p p Most important method: get. Parameter() 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. parse. Int()) to convert to the appropriate type for processing Examples: n n String name = request. get. Parameter( "firstname" ); int year = Integer. parse. Int( request. get. Parameter( "year" ) );
Http. Servlet. Response p p p Used to facilitate result of processing a form “generates” html content Invoke the get. Writer() method to get a Print. Writer handle, then issue print, println methods on that handle Invoke get. Content. Type(“text/html”) to specify that you are generating html content Example: n response. set. Content. Type( "text/html" ); Print. Writer out = response. get. Writer(); out. println( "<h 2> Welcome </h 2>" );
Complete do. Get() example protected void do. Get( Http. Servlet. Request request, Http. Servlet. Response response ) throws Servlet. Exception, IOException { String name = request. get. Parameter( "firstname" ); int favorite. Number = name. length(); response. set. Content. Type( "text/html" ); Print. Writer out = response. get. Writer(); } out. println( "<h 2> Welcome " + name + "</h 2>" ); out. println( "Your favorite number is “ + favorite. Number ); out. close();
Connecting to a database Combine JDBC concepts and servlets p Better to separate code that connect to the database p n p 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
do. Get() with database access protected void do. Get( Http. Servlet. Request request, Http. Servlet. Response response ) throws Servlet. Exception, IOException { String name = request. get. Parameter( "firstname" ); String mobile. Number; get. Num() try is a method of { mobile. Number = DBAccess. get. Num( name ); DBAccess. java } and contains catch( Exception e ) JDBC code { mobilee. Number = "no mobile number"; } response. set. Content. Type( "text/html" ); Print. Writer out = response. get. Writer(); out. println( "<h 2> Welcome " + name + "</h 2>" ); out. println( "Your MOBILE number is " + mobilee. Number ); out. close(); p }
Summary p Servlets process form data through java n p Java programs that extend Http. Servlets are part of a web application n web. xml indicates appropriate mappings Other servlet features: redirection, sessions/cookies p Next: JSP (simplifies servlet coding) p
- Coreservlets
- Servlets notes
- Putting the enterprise into the enterprise system
- Putting the enterprise into the enterprise system
- Java ee 101
- Perbedaan linear programming dan integer programming
- Greedy programming vs dynamic programming
- Definition of system programming
- Integer programming vs linear programming
- Definisi linear
- Enterprise resourse planning systems
- Free enterprise system def
- Basic rights with in enterprise system yaitu
- Enterprise systems include analytical tools.
- Supply chain management life cycle diagram
- Enterprise business system
- Benefits of enterprise systems
- Enterprise ticketing systems
- Phases of crm