Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Being a JSP. JSP introduction JSP is a solution for two issues  Servlet is difficult for HTML designers since they may not know Java  Formatting.

Similar presentations


Presentation on theme: "Chapter 7 Being a JSP. JSP introduction JSP is a solution for two issues  Servlet is difficult for HTML designers since they may not know Java  Formatting."— Presentation transcript:

1 Chapter 7 Being a JSP

2 JSP introduction JSP is a solution for two issues  Servlet is difficult for HTML designers since they may not know Java  Formatting HTML into a String literal is really ugly

3 In the end, JSP is just a servlet Your JSP eventually becomes a full-fledged servlet running in your web app It is a lot like any other servlet, except that the servlet class is written for you-by the Container The Container takes what you’ve written in your JSP, translates it into a servlet class source (.java) file, the compile that into a Java servlet class Also, if you changes JSP files and redeploy them to Tomcat, you do not have to restart tomcat in order to make the changes to be effective

4 Container translates JSP into Servlet This is the servlet tranlated from MyJSP.jsp

5 JSP Pauline wants to use JSPs in her web apps She understands that you can put regular old Java code in a JSP using a scriptlet which just means Java code within a tag

6

7 class MyClass { float aFloat; } class MyClass { static float aFloat; } Instance variable Class variable Instance variable is created for each instance of a class, the runtime system allocates class variables once per class regardless of the number of instances created of that class.

8 JSP When she deploys it and runs it, she got an exception page

9

10 Import packages You can put import statement in a JSP…you just need a directive  You may use the page directive to import packages A directive is a way for you to give special instructions to the Container at page translation time  Directives come in three flavors: page, include, and taglib

11 Import packages You can import a single package  E.g., You can also import multiple packages  E.g.,

12

13 Expression element Part of the whole point of JSP is to avoid println()!  JSP expression element automatically prints out whatever you put between the tags  Please remember that there is no semicolon when using expression element E.g.

14 Expression element The container takes everything you type between the and puts it in as the argument to a statement that prints to the implicit response PrintWriter out. When the Container sees this: It turns it into this out.print(Counter.getCount());

15 So far we’ve seen three element types Scriptlet: Directive: Expression:

16 Question

17 Declaring a variable in a scriptlet The variable declaration is legal, but it does not quite work the way we want. The same page is displayed no matter how many times the page is hit

18 All scriptlet and expression code lands in a service method.That means varaibles declared in a scriptlet are always LOCAL variables.

19 Declaration There is another JSP element called a declaration Anything between the tag is added to the class outside the service method

20

21

22 Time to see real generated servlet The real generated servlet is put under a directory of tomcat  E.g., I have a counter.jsp file under the directory C:\tomcat-6.0.26\webapps\ct The generated servlet file is located at:  C:\tomcat-6.0.26\work\Catalina\localhost\counter\org\apache\jsp\counter_jsp.java When you look at the generated servlet code, you did not see doGet() or doPost() methods there. Instead, you see a service method called _jspService()  This is because this _jspService() is called by the servlet superclass’s overridden service() method

23 Implicit object With implicit objects, you can write a JSP knowing that your code is going to be part of a servlet

24 Implicit object For example, you can put the following scriptlet into your JSP: <% String[] picked = request.getParameterValues(“fruits”); …….. %> This extract the parameter fruits that reflected what the user selected in a group of checkboxes in a HTML page


Download ppt "Chapter 7 Being a JSP. JSP introduction JSP is a solution for two issues  Servlet is difficult for HTML designers since they may not know Java  Formatting."

Similar presentations


Ads by Google