Presentation is loading. Please wait.

Presentation is loading. Please wait.

JSP Implicit Objects Implicit Objects in JSP are objects that are automatically available in JSP. request: The request object retrieves the values that.

Similar presentations


Presentation on theme: "JSP Implicit Objects Implicit Objects in JSP are objects that are automatically available in JSP. request: The request object retrieves the values that."— Presentation transcript:

1 JSP Implicit Objects Implicit Objects in JSP are objects that are automatically available in JSP. request: The request object retrieves the values that the client browser passed to the server during an HTTP request response: This denotes the HTTP Response data. Session: This denotes the data associated with a specific session of user. The main use of Session Objects is for maintaining states when there are multiple page requests. Out

2 Methods of request Object
getParameter(): You call request.getParameter() method to get the value of a form parameter. getParameterValues(): Call this method if the parameter appears more than once and returns multiple values, for example checkbox. getQueryString(): Gets any query string that is part of the HTTP request URI.

3 Use JSP to Process a Form

4 Form Example <body>
<div>Compute the sum of two numbers</div> <form name="testForm" method="post" action="computeSum.jsp"> Enter num1: <input type="text" name="num1" value="" /><br> Enter num2: <input type="text" name="num2" value="" /><br><br> <input type="submit" value="ComputeSumJSP" name="btnCompute"/> </form> </body>

5 Computesum.jsp <body> <% String value1, value2;
double n1, n2, sum; value1=request.getParameter("num1"); value2=request.getParameter("num2"); n1= Double.parseDouble(value1); n2= Double.parseDouble(value2); sum=n1+n2; out.println("The sum is:" + sum); %> </body> Note 1: Double is an object, not “double” data type. Note 2: “out” is an implicit object that does not need to be declared.  It is already predefined .

6 Process form with various controls

7 Form Code <form name="fvForm" method="post" action="computeFV.jsp"> Enter present value: <input type="text" name="PV" value="" /><br><br> Select interest rate: <select name="Rate"> <option value=.04>4%</option> <option value=.05>5%</option> <option value=.06>6%</option> <option value=.07>7%</option> <option value=.08>8%</option> </select><br><br> Select year: <br> <input type="radio" name="Year" value="10" />10-year<br> <input type="radio" name="Year" value="15" />15-year<br> <input type="radio" name="Year" value="30" />30-year<br><br> <input type="submit" value="ComputeFVJSP" name="btnCompute" /> </form>

8 JSP Code Example <body> <% String myPV, myRate, myYear;
myPV=request.getParameter("PV"); myRate=request.getParameter("Rate"); myYear=request.getParameter("Year"); double FV, PV, Rate, Year; PV=Double.parseDouble(myPV); Rate=Double.parseDouble(myRate); Year=Double.parseDouble(myYear); FV=PV*Math.pow(1+Rate,Year); out.println("FutureValue is:"+ FV); %>

9 Using getparametervalues()
<form method="get“ action=“readmultiplevalues.jsp”> <input type="checkbox" name="author" value="Tan"> Tan <input type="checkbox" name="author" value="Mohd Ali">Ali <input type="checkbox" name="author" value="Kumar"> Kumar <input type="submit" value="Query"> </form>

10 Readmultiplevalues.jsp <% String[] authors = request.getParameterValues("author"); if (authors != null) { %> <h3>You have selected author(s):</h3> <ul> <% for (int i = 0; i < authors.length; ++i) <li><%= authors[i] %></li> <% } %> </ul>

11 LTC EXERCISES Write JSP code to validate the DATA entered by the user through HTML form Examples Password & Re-enter password fields MATCHED or not AGE field between 18 and 100 Password atleast 8 characters Mobile Number exactly 10 digits

12 Methods of response Object
addCookie(Cookie): Adds the specified cookie to the response. It can be called multiple times to set more than one cookie. sendRedirect(String): Sends a temporary redirect response to the client using the specified redirect location URL.

13 Example: Logon Restrict access to home page for valid user.

14 Example: Logon v2 index.html Home.html Logon.jsp <html>
<head><title></title></head> <body> <form action=“logon.jsp” method=“post”> Please logon:<br /> <input name="txtUserName" type="text" /><br /> <input name="txtPassWord" type="text" /><br /> <input name="btnLogon" type="submit" value="Logon" /> </form> </body> </html> index.html <html> <head><title>My Home page</title></head> <body> <p> Welcome to my home page.<br /> <img src="YouAreHere.jpg" /> </p> </body> </html> Home.html <% String un; String pw; String msg = ""; un = request.getParameter("txtUserName"); pw = request.getParameter("txtPassWord"); if (un.equals(“admin") && pw.equals(“12345")){ response.sendRedirect(“Home.html"); }else{ out.print(“Login details incorrect.“); } %> Logon.jsp

15 JSP Session Object Session Object denotes the data associated with a specific session of user. Methods: session.setAttribute(“Key”, object) add an object to the session object associated with a key. session.getId() return the unique identifier associated with the session session.getAttribute(“key”) return the object with the specified key given in parameter

16 DEMO


Download ppt "JSP Implicit Objects Implicit Objects in JSP are objects that are automatically available in JSP. request: The request object retrieves the values that."

Similar presentations


Ads by Google