0% found this document useful (0 votes)
9 views19 pages

Java Servlets and JSP Overview Guide

Servlets and Java Server Pages (JSP) are technologies used to create dynamic web content. Servlets extend functionality by processing requests and generating responses. JSPs simplify development by mixing static and dynamic content using tags. The document outlines the history and components of servlets and JSPs. It provides examples of a simple servlet and JSP that retrieve stock prices by interacting with clients and using Java classes. Database access is demonstrated without and with JDBC to retrieve configuration values.

Uploaded by

mamaguevo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views19 pages

Java Servlets and JSP Overview Guide

Servlets and Java Server Pages (JSP) are technologies used to create dynamic web content. Servlets extend functionality by processing requests and generating responses. JSPs simplify development by mixing static and dynamic content using tags. The document outlines the history and components of servlets and JSPs. It provides examples of a simple servlet and JSP that retrieve stock prices by interacting with clients and using Java classes. Database access is demonstrated without and with JDBC to retrieve configuration values.

Uploaded by

mamaguevo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Java Servlets

Java Server Pages


(JSP)

Yi Lin
Overview of History

CGI CGI Template


(in C) (java, C++) (ASP, PHP)

complexity
Speed, Security

Servlet JSP
HelloWorld
import [Link].*;
import [Link].*;
import [Link].*;

public class HelloWorld extends HttpServlet {

public void doGet(HttpServletRequest request,


HttpServletResponse response)
throws IOException, ServletException
{
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html>");
[Link]("<body>");
[Link]("<head>");
[Link]("<title>Hello CS764!</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<h1>Hello CS764!</h1>");
[Link]("</body>");
[Link]("</html>");
[Link]();
}
}
<html><head></head>
<body>
<a href="../servlet/HelloWorld">
<h1>Execute HelloWorld Servlet</h1>
</a>
</body>
</html>

<html>
<head>
<title>Hello CS764!</title></head>
<body>
<h1>Hello CS764!</h1>
</body>
</html>
Client - Server - DB
Trigger Servlet, JSP
(Request)

Client Through Web server


internet (Apache, JWS)
(browser)
Return html file Request
(Response) data
JDBC,
intranet
Return
data

Database
server (DB2)
Life Cycle of Servlet
servlet

GenericServlet HttpServlet

init(ServletConfig);

service(ServletRequest, doGet(HttpServletRequest,
ServletResponse); HttpServletResponse);

doPost(HttpServletRequest,
destroy(); HttpServletResponse);
…….
Interaction with Client
• HttpServletRequest
– String getParameter(String)
– Enumeration getParameters(String[])
• HttpServletResponse
– Writer getWriter()
– ServletOutputStream getOutputStream()
• Handling GET and POST Requests
Assignment 2:
Get Stock Price
[Link]
<html><head></head>
<body>
<form action="../servlet/Ass2Servlet"
method=POST>
<h2>Stock Symbol name:
<input type=text name="stockSymbol"></h2><br>
<input type="submit" value = "get price">
</form>
</body></html>
Client Side
Ass2Servlet
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;

public class Ass2Servlet extends HttpServlet {


public void doPost(HttpServletRequest request,
HttpServletResponse res)
throws IOException, ServletException
{
[Link]("text/html");
PrintWriter out = [Link]();

String stockSymb = [Link]("stockSymbol");

StockGrabber sg = new StockGrabber();


[Link](stockSymb); // Set the stock symbol as “input”
String stockPrice = [Link]();// Get the price of stock

[Link]("After [Link] --"+stockPrice);// Debug


[Link]("<html><head></head><body><br><br>");
[Link](stockSymb + " -- " + stockPrice);
[Link]("<hr>");
[Link]("<form action=\"../servlet/Ass2Servlet\" method=POST>");
[Link]("<h3>Stock Symbol name: <input type=text name=\"stockSymbol\"></h3>");
[Link]("<input type=submit value=\"get price\">");
[Link]("</form>");
[Link]("</body></html>");
}
}
Java Server Pages (JSP)

Client’s [Link] requests HTML


Computer
7. Server sends HTML
back to browser Server

6. Java Engine sends HTML to server


servlet
servlet 2. Server sends requests to Java Engine

class [Link] servlet


runs and 3. If needed, the Java Engine
Bean generates reads the .jsp file
HTML
4. The JSP is turned into a
Java Engine servlet, compiled, and loaded
A First JSP
<html>
<head></head>
<body>
<p>Enter two numbers and click the
‘calculate’ button.</p>

<form action=“[Link]” method=“get”>


<input type=text name=value1><br> [Link]
<input type=text name=value2 ><br>
<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 %> [Link]
<%-- A simple calculator --%>
<h2>The sum of your two numbers is:</h2>
<%= [Link]([Link]("value1")) +
[Link]([Link]("value2")) %>
</body>
</html>
JSP Tags
• Comments <%-- …...text…... --%>
• Declaration <%! int i; %>
<%! int numOfStudents(arg1,..) {} %>
• Expression <%= 1+1 %>
• Scriptlets <% … java code … %>
• include file <%@ include file=“*.jsp” %>
• …...
Using Java Bean
Declaration
1. <jsp:useBean id=“bean1” class=“Bean1”/>
2. <jsp:useBean id=“bean1” class=“Bean1” name=“serBean” type=“SerBean1”/>

Setting property
1. <jsp:setProperty name=“bean1” property=“color” value=“red”/>
2. <jsp:setProperty name=“bean1” property=“color”/>
3. <jsp:setProperty name=“bean1” property=“color” param=“bgColor”/>
4. <jsp:setProperty name=“bean1” property=“*”/>

Getting property
1. <jsp:getProperty name=“bean1” property=“color”/>
2. <%=[Link]() %>
Assg2
example
<html>
<head></head>
<body>
<center>
<table border = 0>
<form action=[Link] method = POST>
<tr><td><font color=blue>choose a stock market:</font></td>
<td><select name="stockMarket">
<option value="Waterhouse">Waterhouse</option>
<option value="Yahoo">Yahoo</option>
<option value="ChicagoStockex">Chicago Stockex</option>
<option value="Reuters">Reuters</option>
</select></td>
</tr>
<tr><td><font color = blue>input a stock symbol:</font></td>
Client side
<td><input type="edit" name="stockSymbol" size=15></td> [Link]
</tr>
<tr><td></td><td><input type="submit" value = "get price"></td></tr>
</table>
</form></center>
</body></html>
[Link]
<html><head>
<jsp:useBean id="ass2" scope="session" class="[Link]" />
<jsp:setProperty name="ass2" property="*" />
</head>
<body><h2><%
[Link](); <jsp:setProperty name=“ass2” property=“stockSymbol”/>
[Link](); <jsp:setProperty name=“ass2” property=“stockMarket”/>
%>
<center><table border=5>
<tr><td># of data</td> <td>Stock Market</td> <td>Stock Symbol</td> <td>Stock Price </td>
</tr><%
String[] stockMarkets = [Link]();
String[] symbols = [Link]();
String[] prices = [Link]();
for(int i=0; i<[Link]; i++){
%>
<tr><td> <%= i+1 %> </td>
<td> <%= stockMarkets[i] %> </td>
<td> <%= symbols[i] %> </td>
<td><font color=red><%= prices[i] %></font></td>
</tr><%
}
%> Server side
</table>
</center>
</h2>
<hr><%@include file="[Link]" %></html>
Without using JDBC
Public class StockGrabber {
...
public void processInput(){
if([Link]("Waterhouse")==0){
setPrePriceString("<!--Last-->");
setPostPriceString("</FONT>");
setUrlPrefix("[Link]
waterhouse/[Link]?ticker=");
}
else if([Link]("Yahoo")==0){
setPrePriceString("<td nowrap><b>");
setPostPriceString("</b></td>");
setUrlPrefix("[Link]
}
...
else if(...){}
...
else{...}
}
...
}
Using JDBC --> Database
import [Link].*;
Public class StockGrabber {
...
public void processInput(){
try {
[Link]("[Link]");
String sourceURL="jdbc:odbc:stockInfo";
Connection databaseConnection=[Link](sourceURL);
Statement statement=[Link]();
ResultSet info =[Link](
"select tPrePriceStr, tPostPriceStr, tUrlPrefix
from stockMarketData
where tStockMarket = stockMarket”);
while([Link]())
{
prePriceString = [Link](”tPrePriceStr");
postPriceString = [Link](“tPostPriceStr”);
urlPrefix = [Link](“tUrlPrefix”);
}
}
catch(SQLException e){ ... }
...
}
}

You might also like