Presentation is loading. Please wait.

Presentation is loading. Please wait.

©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)

Similar presentations


Presentation on theme: "©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)"— Presentation transcript:

1 ©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)

2 ©SoftMoore ConsultingSlide 2 Servlets Servlets are Java classes that support the request/response computing model of the web. Servlet methods are invoked by the web server in the process of handling requests. Servlets run on the server, not the client. –server-side, not client-side The most basic type of servlet writes an HTML page as the response.

3 ©SoftMooreSlide 3 Example: HelloWorld Servlet ( HelloWorld.java ) package com.softmoore.hello; import java.io.PrintWriter; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { private static final long serialVersionUID = 3727697617277754138L; @Override public void doGet(HttpServletRequest request, HttpServletResponse response) { try { response.setContentType("text/html"); (continued on next slide)

4 ©SoftMooreSlide 4 Example: HelloWorld Servlet (continued) PrintWriter out = response.getWriter(); out.println(" "); out.println(" Hello "); out.println(" "); out.println(" Hello, world. "); out.println(" "); } catch (Exception ex) { log("Unexpected exception", ex); }

5 HelloWorld Servlet in a Browser ©SoftMoore ConsultingSlide 5

6 ©SoftMoore ConsultingSlide 6 Using Servlets The real power of servlets becomes apparent when we need to create content dynamically based on user feedback. Servlets can handle requests with parameters; e.g., from HTML forms. The servlet can examine the parameters and formulate a response unique to the user. In formulating the response, the servlet can authenticate the user, query a database, etc. Example. Consider a query to Amazon or Barnes & Noble for books about Java.

7 ©SoftMoore ConsultingSlide 7 Example: HTML Form ( HelloUserForm.html )... Enter your name:... calls servlet HelloUser

8 HelloUserForm.html in a Browser ©SoftMoore ConsultingSlide 8

9 ©SoftMoore ConsultingSlide 9 Using a Servlet to Process the Form ( HelloUser.java ) package com.softmoore.hello; import java.time.LocalTime; import java.io.PrintWriter; import javax.servlet.http.*; public class HelloUser extends HttpServlet { private static final long serialVersionUID = -6380883006263258103L; private static final LocalTime NOON = LocalTime.of(12, 0, 0); (continued on next page)

10 ©SoftMoore ConsultingSlide 10 Using a Servlet to Process the Form (continued) @Override public void doGet(HttpServletRequest request, HttpServletResponse response) { try { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String userName = request.getParameter("userName"); String message; if(LocalTime.now().isBefore(NOON)) message = "Good morning, " + userName + "."; else message = "Good afternoon, " + userName + "."; (continued on next page)

11 ©SoftMoore ConsultingSlide 11 Using a Servlet to Process the Form (continued) out.println(" "); out.println(" Hello "); out.println(" "); out.println(" " + message + " "); out.println(" "); } catch (Exception ex) { log("Unexpected exception", ex); }

12 HelloUser Servlet in a Browser ©SoftMoore ConsultingSlide 12 parameter name/value are passed as part of URL for GET method

13 JavaServer Pages (JSP) JSP is a server-side technology for building web applications that serve dynamic content JSP combines standard HTML markup and content with special JSP tags for dynamic content. –more powerful than static HTML –much simpler than Java servlets Behind the scenes, a JSP page is automatically translated to a servlet, usually the first time that the page is requested. –translation to servlet (performed only once) –servlet is compiled (performed only once) –servlet is what actually gets invoked for each request ©SoftMoore ConsultingSlide 13

14 Five Types of JSP Tags –directive (e.g., to import a class) –scriptlet (block of Java code) –expression (Java expression displayed as a string) –declaration (declare variables) –comment ©SoftMoore ConsultingSlide 14

15 ©SoftMoore ConsultingSlide 15 Example: HTML Form ( HelloUserForm.html )... Enter your name:... calls JSP page HelloUser.jsp

16 HelloUserForm.html in a Browser ©SoftMoore ConsultingSlide 16

17 ©SoftMoore ConsultingSlide 17 JSP Example ( HelloUser.jsp ) Hello Hello,.

18 HelloUser.jsp in a Browser ©SoftMoore ConsultingSlide 18

19 ©SoftMoore ConsultingSlide 19 Using JSP A JSP page can contain Java code fragments called scriptlets embedded within tags. Similar to servlets, scriptlets provide the full power of the Java programming language. The user can create Java objects, query a database, etc. A page directive can be used to import Java classes

20 ©SoftMoore ConsultingSlide 20 JSP Example: Scriptlets ( HelloUser2.jsp ) Hello <% LocalTime NOON = LocalTime.of(12, 0, 0); String userName = request.getParameter("userName"); String message; (continued on next slide)

21 ©SoftMoore ConsultingSlide 21 JSP Example: Scriptlets (continued) <% LocalTime NOON = LocalTime.of(12, 0, 0); String userName = request.getParameter("userName"); String message; if (LocalTime.now().isBefore(NOON)) message = "Good morning, " + userName + "."; else message = "Good afternoon, " + userName + "."; %> scriptlet Drawback: Too much code Java within the JSP page.

22 HelloUser2.jsp in a Browser ©SoftMoore ConsultingSlide 22

23 ©SoftMoore ConsultingSlide 23 Recommended Practices Limit the amount of Java code in JSP pages. –use separate utility classes and JavaBeans –define all methods in classes, not JSP declarations Place all classes in packages.

24 ©SoftMoore ConsultingSlide 24 Class AmPm package com.softmoore.hello; import java.time.LocalTime; public class AmPm { private static final LocalTime NOON = LocalTime.of(12, 0, 0); public static boolean isAm() { return LocalTime.now().isBefore(NOON); }

25 JSP Example: Scriptlets ( HelloUser3.jsp ) Hello Good,. ©SoftMoore ConsultingSlide 25


Download ppt "©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)"

Similar presentations


Ads by Google