Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Programming: If Statement and Servlet

Similar presentations


Presentation on theme: "Web Programming: If Statement and Servlet"— Presentation transcript:

1 Web Programming: If Statement and Servlet
Web Programming: If Statement and Servlet Z517: Web Programming Xiaozhong Liu

2 Variable String int double boolean String name = “Obama”;
Variable type Variable name (no space, start With alphabet, case sensitive) String String name = “Obama”; Variable value int int age = 30; double double price = 15.25; boolean boolean dataplan = true; (or false)

3 More Assignment Statements
The value on the right-hand-side of an assignment statement can be computed with an arithmetic expression pricewithtax = price * ; This can include other variables or even the same variable price = 5; price = price ; Move that value to be the new value of the variable First compute the value

4 No Sorry, you are not old enough!
Information problem… Output 1 Input Process Output 2 How old are you? Age > 20??? I need some wine Yes  great, you get it! No Sorry, you are not old enough!

5 Selection If-Then statement if (comparison) (boolean variable) {
[some actions] } ELSE { [some different actions]

6 Some errors… if (comparison) ; { if (...) [some actions] }
statement 1; else statement 2; ... if (comparison) something 1; something 2;

7 Example 1 If your age > 20, serve you wine; else, serve you soda…
int age; String drink; age = ??; if (age > 20) { drink = “wine”; } else { drink = “soda”; System.out.println(drink);

8 Conditions and operators
Example Meaning Equal to == (int, double, Boolean) EmpCode == 0 Is employee code equal to 0? .equals (String) str.equals(“abc”) Is str variable equal to “abc”? Greater than > Hours > 40 Are the hours greater than 40? Greater than or equal to >= Revenue >= Costs Is Revenue greater than or equal to costs? Less than < thisyear < lastyear Is this year’s revenue less than last year’s? Less than or equal to <= Total <= Is the total value less than or equal to 1000 (dollars)? Not equal to != Number <> 0 Is the number not equal to zero?

9 Rank of operators Highest Lowest ! ++ -- * / % + -
! * / % + - < <= > >= == != && ||

10 Practice 1 Shopping problem.
If you purchase $50 or more, you get 10% off for your total bill; otherwise, you get 5% discount. double cost; cost = Double.parseDouble(jTextField1.getText()); if (cost > 50) { cost = cost * (1-0.1); } else { cost = cost * (1-0.05); jLabel2.setText("Your discounted price: $" + Double.toString(cost));

11 Example 2 If you have data plan, your monthly payment is 59.99; if you don’t have data plan, you only pay 39.99 boolean dataplan; double payment; dataplan = true; //true or false if (dataplan) { payment = 59.99; } else { payment = 39.99;

12 Multiple conditions If you are VIP user, and you purchased $100 above, you get 15% discount boolean VIP; double payment; //VIP and payment input if (VIP == true && payment > 100) { payment = payment * (1 – 0.15); }

13 Multiple conditions If you are VIP user, or you purchased $100 above, you get 15% discount boolean VIP; double payment; //VIP and payment input if (VIP == true || payment > 100) { payment = payment * (1 – 0.15); }

14 Practice 2 February days. int year, month; int feb_days;
// write some code to figure out number of days of Feb System.out.println(“Feb days = ” + feb_days);

15 Practice 3 Shopping problem.
Suppose that there is a String variable called PayStatus that is either “Hourly” or “Salaried”, and that we want to compute the Pay of an employee using a variable Hours that tells how many hours they worked that week, and the variable PayRate that tells how much per hour that they make. Salaried employees are always paid as if they worked 40 hours, no matter how many hours they actually worked. If hourly employees work more than 40 hours in a week, then they get 1.5 times their pay rate for any hours over 40.

16 Attention! if (condition1 && condition2) ...
If condition1 is false, then condition2 is NOT evaluated if (condition1 || condition2) ... If condition1 is true, then condition2 is NOT evaluated

17 What is variable? What is expression? What is Standard arithmetic? What is IF-ELSE statement? What is multiple conditions? How to add the code to GUI?

18 Classes and Functions Java organizes programs and its programming libraries into units called “classes” • Some classes contain useful functions • Functions are named by giving the class name, “.”, and the function name • Example: the Math class contains a large number of mathematical functions e.g. Math.pow computes exponents • To call a function, give its name followed by any argument: Math.pow(2.0 , 3.0 ) expression that computes 2 to the power 3 • Value of this expression is 8.0 • Or use the value in an assignment: result = Math.pow(2.0 , 3.011)

19 Form Objects and Functions
The object-oriented part of Java is that many values are created as “objects” that are “instances” of classes Examples of classes used in creating forms: – JFrame, JButton, jLabel, jTextField Each time that we create a textfield on a form, Java creates an instance of the class – jTextField1, jTextField2, jTextField3 There are functions that we can use for any instance, and these functions also use the “.” – // getText() returns a string from the TextField String text = jTextField1.getText ( ); – // setText takes a string argument and puts it into the TextField jTextField1.setText( text );

20 Web Programming <html><head>
<title>Compute test</title></head><body> <h2>Please submit your information</h2> <form method="post" action ="/week1/web_coompute" > <table border="0"> <tr> <td valign="top"> First number: </td> <td valign="top"> <input type="text" name="firstnum" size="20"> </td> </tr> <tr><td valign="top"> Second number: </td> <input type="text" name="secondname" size="20"> </td> <tr> <input type="submit" value="Submit Info"></td> </table></form> </body></html>

21 Java Servlet javax.servlet, javax.servlet.http
The predominant language for server-side programming to generate dynamic web content User send request, servlet takes the request and generates real-time response (some HTML result) and return to user Alternatives, ASP, ASP.net, PHP… Support multithread to improve performance javax.servlet, javax.servlet.http

22 Java Servlet - Tomcat You can publish your code via Tomcat
Integrate other packages Deployment configuration Servlet + Server vs Applet + Browser

23 Web Programming (dynamic) HTML
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter(); out.println("<html><head>"); out.println("<title>Compute test</title></head><body>"); out.println("<h2>Please submit your information</h2>"); out.println("<form method=\"post\" action =\"" + request.getContextPath() + "/web_coompute\" >"); out.println("<table border=\"0\"><tr><td valign=\"top\">"); out.println("First number: </td> <td valign=\"top\">"); out.println("<input type=\"text\" name=\"firstnum\" size=\"20\">"); out.println("</td></tr><tr><td valign=\"top\">"); out.println("Second number: </td> <td valign=\"top\">"); out.println("<input type=\"text\" name=\"secondname\" size=\"20\">"); out.println("<input type=\"submit\" value=\"Submit Info\"></td></tr>"); out.println("</table></form>"); out.println("</body></html>"); } (dynamic) HTML

24 Web Programming protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter(); out.println("<html><head>"); out.println("<title>Result</title></head><body>"); out.println("<h2>Compute result:</h2>"); int firstnum = Integer.parseInt(request.getParameter("firstnum")); int secondnum = Integer.parseInt(request.getParameter("secondname")); int sum = firstnum + secondnum; out.println(Integer.toString(sum)); out.println("</body></html>"); }

25 Web Programming protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter(); out.println("<html><head>"); out.println("<title>Result</title></head><body>"); out.println("<h2>Compute result:</h2>"); int firstnum = Integer.parseInt(request.getParameter("firstnum")); int secondnum = Integer.parseInt(request.getParameter("secondname")); int sum = firstnum + secondnum; out.println(Integer.toString(sum)); out.println("</body></html>"); }


Download ppt "Web Programming: If Statement and Servlet"

Similar presentations


Ads by Google