<chapter 2>2-2-2-1. Introduction Jsps,jstl, and El (Introducing JSPs, JSTL, and EL)

Source: Internet
Author: User

Our clock now shows the time in the UTC time zone. We want our app to allow the user to customize the time zone and remember the user's preferences for future access. To do this, we use Google accounts to identify which users are using the app.

Before we get into the details (before we go any further), we should introduce a way to keep HTML separate from our servlet code. This allows us to maintain the appearance and business logic of the application separately (business logic), which implements the main objectives of our application, making our logic easy to test and the application's appearance easy to change. Typically, you will use a template system to define the appearance of your app as a file, including thml,css,javascrpt and reserved whitespace for putting dynamic data. A lot of good templating systems are available in Java, such as Apache Velocity.

For this example, we use Java Servlet Pages, or JSPs. JSPs is the standard part of the Java EE, which means you don't need to install anything else to use them. A JSP contains text (HTML), a blend of Java code that defines this page's logic. The JSP is compiled into a servlet (the JSP compiles to a servlet), as we have defined clockservlet, which is equivalent to writing the HTML section and evaluating the Java section (that's equivalent to writing Out the HTML portions,and evaluating the Java portions). In a sense, JSPs is just another way to write the servlet code.

JSPs is often criticized for being too powerful. Because the entire Java language can be obtained from a JSP, there is a risk that the business logic may be written to the template and you are no longer effectively separated. To mitigate this, a later version of the JSP specification contains new methods to describe the template logic, which intentionally makes it not as powerful as the entire Java code: Java Servlet Templating Language (JSTL) and JSP Expression Language (EL). We use these features for this example, as well as for other areas of the book where templating output is required.

Edit Clockservlet.java into similar example 2-12.

Example 2-12. Clockservlet.java code showing Google accounts and links

pacage clock;Importjava.io.IOException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportJava.util.SimpleTimeZone;ImportJavax.servlet.RequestDispatcher;Importjavax.servlet.ServletException;Importjavax.servlet.http.*;ImportCom.google.appengine.api.users.User;ImportCom.google.appengine.api.users.UserService;Importcom.google.appengine.api.users.UserServiceFactory; @SuppressWarning ("Serial"); PublicCladd Clockservletextendshttpservlet{ Public voiddoget (httpservletrequest req,httpservletresponse resp)throwsioexception,servletexception{SimpleDateFormat FMT=NewSimpleDateFormat ("Yyyy-mm-dd hh:mm:ss. SSS "); Fmt.settimezone (NewSimpleTimeZone (0, "")); UserService UserService=Userservicefactory.getuserservice (); User usr=Userservice.getcurrentuser (); String loginurl= Userservice.createloginurl ("/"); String Logouturl= Userservice.createlogouturl ("/"); Req.setattribute ("User", user); Req.setattribute ("Loginurl", loginurl); Req.setattribute ("Logouturl", Logouturl); Req.setattribute ("CurrentTime", Fmt.format (NewDate ())); Resp.setcontenttype ("Text/html"); RequestDispatcher JSP= Req.getrequestdispatcher ("/web-inf/home.jsp");        Jsp.forward (REQ,RESP); }}

Next, create a new file in your project's war/web-inf/directory called home.jsp, and add something like example 2-13.

Example 2-13. Clockservlet.java code showing Google accounts and links

<chapter 2>2-2-2-1. Introduction Jsps,jstl, and El (Introducing JSPs, JSTL, and EL)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.