You are on page 1of 4


Content: Writing JSP Pages

Have a strong background in writing JSP pages Write dynamic web pages using JSP Utilize scriptlets Introduce Implicit Objects in JSP out and request

JSP gives the capability to put Java inside HTML pages What makes JSP very useful is the ability to embed Java in your JSP pages As an example:
<html> <head><title>Writing JSP</title></head> <body> The date today is <%= new java.util.Date() %> </body> </html>

Put the following in a text file, save it in your JSP directory with a .jsp extension and then view it in your browser. The character sequence <%= %> enclose Java expressions which are evaluated at runtime. JSP also allows you to write block of Java code inside your JSP. o You do this by placing your Java code between <% and %> characters This block of code is known as scriptlet. o A scriptlet contains Java code that is executed every time the JSP is invoked.
<html> <head><title>A Scriptlet Example</title></head> <body> <% java.util.Date d = new java.util.Date(); %> The date today is <%= d %> </body>


The PrintWriter
By itself a scriptlet does not generate HTML. If a scriptlet wants to generate HTML, JSP can write and generate HTML using the PrintWriter. It is referenced by the reserved keyword out, which represents a variable name for output. You generally access the PrintWriter directly via the following two functions: o out.print : Print strings and variables directly to the clients web page exactly as formatted. o out.println : An abbreviation of print line: prints strings and variables directly to the clients web page but with a carriage return inserted at the end.
<html> <head><title>Implicit Object out</title></head> <body> <h1>Implicit Object out</h1> <% java.util.Date d = new java.util.Date(); %> Hello there! <br /> The date now is <% out.println(""+d); %> </body> </html>

The request Object

A request is the message that the clients browser sends to the web server JSP and servlets are primarily concerned with the GET and POST results A GET request is represented solely by the URL typed in the browsers address bar A POST request is submitted from a form tagged as a POST action. o It passes the users data inside the forms data, which is submitted as a body following the page request headers

Methods request.getParameter(String parameter_name) This is the method used for getting the value of the HTML form attribute This method returns the string type value i.e. the value of the specified field of an HTML form This method takes a string parameter which is the name of the attribute of the html which value has to be retrieved through the request object

request.getParameterNames( ) This is the method of the request object used for getting the enumeration of the attributes of the html form.

These values are later retrieved through the java programming language by enumerating the retrieved enumerated data by the method of the request object.

request.getParameterValues(String name) This is the method of the request object used for getting the string array containing all of the values which are contained by the request parameter. This method takes a String type parameter which is the name of the field of the html form. This method is used where the array of controls of the HTML lies. All the control of the HTML form contains same name and then makes a control array.

request.getCookies( ) This is the method of the request object used for getting all the cookies existed in the HTTP request object.

<html> <head><title>Request Object In JSP.</title></head> <body> <form action="RequestObjectInJSP.jsp" method="post"> <table border="0" cellspacing="0" cellpadding="0"> <tr> <td>User Name: </td> <td> <input type="text" size="20" name="txtUserName" /> </td> </tr> <tr> <td>Password: </td> <td> <input type="password" size="20" name="txtPassword" /> </td> </tr> <tr> <td>&nbsp;</td> <td> <input type="submit" value="Submit" name="B1" /> </td> </tr> </table> </form> </body> </html>

<%@page import="java.util.*" %> <% String username, password; if(request.getParameter("txtUserName") == null) username = ""; else username = request.getParameter("txtUserName"); if(request.getParameter("txtPassword") == null) password = ""; else password = request.getParameter("txtPassword"); %> <html><head><title>Using the request Object</title></head> <body> <table align="center" bgcolor="ffff00" border="1" cellspacing="0" cellpadding="0"> <tr> <td align><b>Your User Name: </b></td> <td><%=username %><br/></td> </tr> <tr> <td><b>Your Password: </b></td> <td><%=password %></td> </tr> </table> </body> </html>