13
25-Jul-11 JSP Java Server Pages Reference: http://www.apl.jhu.edu/~hall/java/Servlet- Tutorial/Servlet-Tutorial-JSP.html

36-jsp

Embed Size (px)

DESCRIPTION

Java Server Pages

Citation preview

25-Jul-11

JSPJava Server PagesReference: http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-JSP.htmlA Hello World servlet(from the Tomcat installation documentation)public class HelloServlet extends HttpServlet {public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();String docType ="\n";out.println(docType +"\n" +"Hello\n" +"\n" +"Hello World\n" +"");}}This is mostly Java with a little HTML mixed inServletsThe purpose of a servlet is to create a Web page in response to a client requestServlets are written in Java, with a little HTML mixed inThe HTML is enclosed in out.println( ) statementsJSP (Java Server Pages) is an alternate way of creating servletsJSP is written as ordinary HTML, with a little Java mixed inThe Java is enclosed in special tags, such as The HTML is known as the template textJSP files must have the extension .jspJSP is translated into a Java servlet, which is then compiledServlets are run in the usual wayThe browser or other client sees only the resultant HTML, as usualTomcat knows how to handle servlets and JSP pagesJSP scripting elementsThere is more than one type of JSP tag, depending on what you want done with the Java

The expression is evaluated and the result is inserted into the HTML page

The code is inserted into the servlet's service methodThis construction is called a scriptlet

The declarations are inserted into the servlet class, not into a method

Example JSP

Hello! The time is now

Notes:The tag is used, because we are computing a value and inserting it into the HTMLThe fully qualified name (java.util.Date) is used, instead of the short name (Date), because we havent yet talked about how to do import declarations

VariablesYou can declare your own variables, as usualJSP provides several predefined variablesrequest : The HttpServletRequest parameterresponse : The HttpServletResponse parametersession : The HttpSession associated with the request, or null if there is noneout : A JspWriter (like a PrintWriter) used to send output to the clientExample:Your hostname:

ScriptletsScriptlets are enclosed in tagsScriptlets do not produce a value that is inserted directly into the HTML (as is done with )Scriptlets are Java code that may write into the HTMLExample:

Scriptlets are inserted into the servlet exactly as written, and are not compiled until the entire servlet is compiledExample:

Have a nice day!

Have a lousy day!

DeclarationsUse for declarations to be added to your servlet class, not to any particular methodCaution: Servlets are multithreaded, so nonlocal variables must be handled with extreme careIf declared with , variables are local and OKData can also safely be put in the request or session objectsExample:

Accesses to page since server reboot:

You can use to declare methods as easily as to declare variablesDirectivesDirectives affect the servlet class itselfA directive has the form:

or

The most useful directive is page, which lets you import packagesExample: The include directiveThe include directive inserts another file into the file being parsedThe included file is treated as just more JSP, hence it can include static HTML, scripting elements, actions, and directivesSyntax: The URL is treated as relative to the JSP pageIf the URL begins with a slash, it is treated as relative to the home directory of the Web serverThe include directive is especially useful for inserting things like navigation barsActionsActions are XML-syntax tags used to control the servlet engine

Inserts the indicated relative URL at execution time (not at compile time, like the include directive does)This is great for rapidly changing data

Jump to the (static) URL or the (dynamically computed) JavaExpression resulting in a URL

JSP in XMLJSP can be embedded in XML as well as in HTMLDue to XMLs syntax rules, the tags must be different (but they do the same things)HTML: XML: expressionHTML: XML: codeHTML: XML: declarationsHTML: XML: The End