Java Servlets Java Server Pages JSP Yi Lin



















- Slides: 19

Java Servlets Java Server Pages (JSP) Yi Lin

Overview of History CGI (in C) (java, C++) Speed, Security Servlet Template (ASP, PHP) complexity JSP

Hello. World import java. io. *; import javax. servlet. http. *; public class Hello. World extends Http. Servlet { public void do. Get(Http. Servlet. Request request, Http. Servlet. Response response) throws IOException, Servlet. Exception { response. set. Content. Type("text/html"); Print. Writer out = response. get. Writer(); out. println("<html>"); out. println("<body>"); out. println("<head>"); out. println("<title>Hello CS 764!</title>"); out. println("</head>"); out. println("<body>"); out. println("<h 1>Hello CS 764!</h 1>"); out. println("</body>"); out. println("</html>"); out. close(); } }

<html><head></head> <body> <a href=". . /servlet/Hello. World"> <h 1>Execute Hello. World Servlet</h 1> </a> </body> </html> <head> <title>Hello CS 764!</title></head> <body> <h 1>Hello CS 764!</h 1> </body> </html>

Client - Server - DB Trigger Servlet, JSP (Request) Client (browser) Through internet Return html file (Response) Web server (Apache, JWS) JDBC, intranet Return data Database server (DB 2) Request data

Life Cycle of Servlet servlet Http. Servlet Generic. Servlet init(Servlet. Config); service(Servlet. Request, do. Get(Http. Servlet. Request, Http. Servlet. Response); do. Post(Http. Servlet. Request, Http. Servlet. Response); destroy(); …….

Interaction with Client • Http. Servlet. Request – String get. Parameter(String) – Enumeration get. Parameters(String[]) • Http. Servlet. Response – Writer get. Writer() – Servlet. Output. Stream get. Output. Stream() • Handling GET and POST Requests

Assignment 2: Get Stock Price Ass 2. html <html><head></head> <body> <form action=". . /servlet/Ass 2 Servlet" method=POST> <h 2>Stock Symbol name: <input type=text name="stock. Symbol"></h 2> <input type="submit" value = "get price"> </form> </body></html> Client Side

import java. io. *; java. util. *; javax. servlet. http. *; Ass 2 Servlet public class Ass 2 Servlet extends Http. Servlet { public void do. Post(Http. Servlet. Request request, Http. Servlet. Response res) throws IOException, Servlet. Exception { res. set. Content. Type("text/html"); Print. Writer out = res. get. Writer(); String stock. Symb = request. get. Parameter("stock. Symbol"); Stock. Grabber sg = new Stock. Grabber(); sg. set. Stock. Symbol(stock. Symb); // Set the stock symbol as “input” String stock. Price = sg. get. Price(); // Get the price of stock System. out. println("After Stock. Grabber. get. Price --"+stock. Price); // Debug out. println("<html><head></head><body> "); out. println(stock. Symb + " -- " + stock. Price); out. println("<hr>"); out. println("<form action=". . /servlet/Ass 2 Servlet" method=POST>"); out. println("<h 3>Stock Symbol name: <input type=text name=" stock. Symbol"></h 3>"); out. println("<input type=submit value="get price">"); out. println("</form>"); out. println("</body></html>"); } }

Java Server Pages (JSP) Client’s Computer 1. Browser requests HTML 7. Server sends HTML back to browser Server 6. Java Engine sends HTML to server servlet class Bean servlet 5. The servlet runs and generates HTML Java Engine 2. Server sends requests to Java Engine 3. If needed, the Java Engine reads the. jsp file 4. The JSP is turned into a servlet, compiled, and loaded

A First JSP <html> <head></head> <body> <p>Enter two numbers and click the ‘calculate’ button. </p> <form action=“calculator. jsp” method=“get”> <input type=text name=value 1> <input type=text name=value 2 > Calculator. html <input type=submit name=calculate value=calculate> </form> </body> </html>

<html> <head><title>A simple calculator: results</title></head> <body> <%-- A simpler example 1+1=2 --%> 1+1 = <%= 1+1 %> <%-- A simple calculator --%> <h 2>The sum of your two numbers is: </h 2> <%= Integer. parse. Int(request. get. Parameter("value 1")) + Integer. parse. Int(request. get. Parameter("value 2")) %> </body> </html> Calculator. jsp

JSP Tags • Comments <%-- …. . . text…. . . --%> • Declaration <%! int i; %> <%! int num. Of. Students(arg 1, . . ) {} %> • Expression <%= 1+1 %> • Scriptlets <% … java code … %> • include file <%@ include file=“*. jsp” %> • …. . .

Using Java Bean Declaration 1. <jsp: use. Bean id=“bean 1” class=“Bean 1”/> 2. <jsp: use. Bean id=“bean 1” class=“Bean 1” name=“ser. Bean” type=“Ser. Bean 1”/> Setting property 1. <jsp: set. Property name=“bean 1” property=“color” value=“red”/> 2. <jsp: set. Property name=“bean 1” property=“color”/> 3. <jsp: set. Property name=“bean 1” property=“color” param=“bg. Color”/> 4. <jsp: set. Property name=“bean 1” property=“*”/> Getting property 1. <jsp: get. Property name=“bean 1” property=“color”/> 2. <%=bean 1. get. Color() %>

Assg 2 example <html> <head></head> <body> <center> <table border = 0> <form action=ass 2. jsp method = POST> <tr><td><font color=blue>choose a stock market: </font></td> <td><select name="stock. Market"> <option value="Waterhouse">Waterhouse</option> <option value="Yahoo">Yahoo</option> <option value="Chicago. Stockex">Chicago Stockex</option> <option value="Reuters">Reuters</option> </select></td> </tr> <tr><td><font color = blue>input a stock symbol: </font></td> <td><input type="edit" name="stock. Symbol" size=15></td> </tr> <tr><td></td><input type="submit" value = "get price"></td></tr> </table> </form></center> </body></html> Client side Ass 2. html

ass 2. jsp <html><head> <jsp: use. Bean id="ass 2" scope="session" class="ass 2. Stock. Grabber" /> <jsp: set. Property name="ass 2" property="*" /> </head> <body><h 2><% <jsp: set. Property name=“ass 2” property=“stock. Symbol”/> ass 2. process. Input(); <jsp: set. Property name=“ass 2” property=“stock. Market”/> ass 2. get. Price(); %> <center><table border=5> <tr><td># of data</td> <td>Stock Market</td> <td>Stock Symbol</td> <td>Stock Price </td> </tr><% String[] stock. Markets = ass 2. get. Stock. Markets(); String[] symbols = ass 2. get. Symbols(); String[] prices = ass 2. get. Prices(); for(int i=0; i<prices. length; i++){ %> <tr><td> <%= i+1 %> </td> <%= stock. Markets[i] %> </td> <%= symbols[i] %> </td> <td><font color=red><%= prices[i] %></font></td> </tr><% } %> </table> </center> </h 2> <hr><%@include file="ass 2. html" %></html> Server side


Without using JDBC Public class Stock. Grabber {. . . public void process. Input(){ if(stock. Market. compare. To("Waterhouse")==0){ set. Pre. Price. String("<!--Last-->"); set. Post. Price. String("</FONT>"); set. Url. Prefix("http: //research. tdwaterhouse. com/ waterhouse/quote. asp? ticker="); } else if(stock. Market. compare. To("Yahoo")==0){ set. Pre. Price. String("<td nowrap><b>"); set. Post. Price. String("</b></td>"); set. Url. Prefix("http: //finance. yahoo. com/q? s="); }. . . else if(. . . ){}. . . else{. . . } }. . . }

Using JDBC --> Database import java. sql. *; Public class Stock. Grabber {. . . public void process. Input(){ try { Class. for. Name("sun. jdbc. odbc. Jdbc. Odbc. Driver"); String source. URL="jdbc: odbc: stock. Info"; Connection database. Connection=Driver. Manager. get. Connection(source. URL); Statement statement=database. Connection. create. Statement(); Result. Set info =statement. execute. Query( "select t. Pre. Price. Str, t. Post. Price. Str, t. Url. Prefix from stock. Market. Data where t. Stock. Market = stock. Market”); while(inf. next()) { pre. Price. String = info. get. String(”t. Pre. Price. Str"); post. Price. String = info. get. String(“t. Post. Price. Str”); url. Prefix = info. get. String(“t. Url. Prefix”); } } catch(SQLException e){. . . } }