Syntax of JSP Scriptles are: <% //java codes %> JSP Scriptlets begins with <% and ends %> .We can embed any amount of java code in the JSP Scriptlets. JSP Engine places these code in the _jspService() method. Variables available to the JSP Scriptlets are:
* request: request represents the clients request and is a subclass of HttpServletRequest. Use this variable to retrieve the data submitted along the request. Example: <% //java codes String userName=null; userName=request.getParameter("userName"); %> * response: response is subclass of HttpServletResponse. * session: session represents the HTTP session object associated with the request. * out: out is an object of output stream and is used to send any output to the client. JSP Scriptlets Example In this section, we will understand Scriptlets with the help of an Example. JSP allows you embed java code inside JSP page. The java code is written inside <% and %> , This block is known as Scriplets. Scriplets is executed every time a JSP page is invoked.The syntax to declare JSP Scriplets to include valid Java code is: <% Java code %> Scriptlets can't generate HTML itself . Using "out" variable , it can generate HTML. This variable does not need to be declared. It is already predefined for Scriptlets, along with some other variables. The "out" variable is of type "javax.servlet.jsp.JspWriter". Scriptlets Example <HTML> <BODY> <% // This scriptlet declares and initializes "date" System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> Hello! The time is now <% // This scriptlet generates HTML output out.println( String.valueOf( date )); %> </BODY> </HTML> Output [cid:image001.png@01CB74F9.9033BE60] Predefined Scriptlet variable "request" The JSP "request" variable is used to obtain information from the request as sent by the browser. For instance, you can find out the name of the client's host (if available, otherwise the IP address will be returned.) Let us modify the code as shown : <HTML> <BODY> <% // This scriptlet declares and initializes "date" System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> Hello! The time is now <% out.println( date ); out.println( "<BR>Your machine's address is " ); out.println( request.getRemoteHost()); %> </BODY> </HTML> OUTPUT [cid:image001.png@01CB74F9.9033BE60] A similar variable is "response". This can be used to affect the response being sent to the browser. For instance, you can call response.sendRedirect( anotherUrl ) to send a response to the browser that it should load a different URL From: java-ee-j2ee-programming-with-passion@googlegroups.com [mailto:java-ee-j2ee-programming-with-pass...@googlegroups.com] On Behalf Of Arumaikkani Sent: Monday, October 25, 2010 2:08 PM To: java-ee-j2ee-programming-with-passion@googlegroups.com Subject: [java ee programming] Scriptlet in JSP hi Can any body explain about scriptlet, why we are using, can i make without the scriplet page in jsp? How can i test one of my Jsp file Ex:"login.jsp" .? regards Kani -- You received this message because you are subscribed to the Google Groups "Java EE (J2EE) Programming with Passion!" group. To post to this group, send email to java-ee-j2ee-programming-with-passion@googlegroups.com To unsubscribe from this group, send email to java-ee-j2ee-programming-with-passion+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/java-ee-j2ee-programming-with-passion?hl=en?hl=en ________________________________ This e-mail contains privileged and confidential information intended for the use of the addressees named above. If you are not the intended recipient of this e-mail, you are hereby notified that you must not disseminate, copy or take any action in respect of any information contained in it. If you have received this e-mail in error, please notify the sender immediately by e-mail and immediately destroy this e-mail and its attachments. -- You received this message because you are subscribed to the Google Groups "Java EE (J2EE) Programming with Passion!" group. To post to this group, send email to java-ee-j2ee-programming-with-passion@googlegroups.com To unsubscribe from this group, send email to java-ee-j2ee-programming-with-passion+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/java-ee-j2ee-programming-with-passion?hl=en?hl=en
<<inline: image001.png>>