0% found this document useful (0 votes)
5 views

Lecture Jspslides

Uploaded by

Nur Edy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture Jspslides

Uploaded by

Nur Edy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Java Servlets

• Server side scripting for dynamic content


generation
• Servlet engine sits behind (or replaces) web
server
• Based around HTTP, handling (mainly)
GET and POST requests
• Support for sessions and cookies
• More efficient than CGI because
thread-based rather than process-based
• Servlets remain in memory unless they are
not used for a long time, or resources are
exhausted.

1
Hello World as a Servlet
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class helloworld extends HttpServlet
{
public void doGet(final HttpServletRequest request,
final HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
final PrintWriter tResponsePrintWriter =
response.getWriter();
StringBuffer tStringBuffer = new StringBuffer();
tStringBuffer.append("<html>\n" );
tStringBuffer.append("<body>\n" );
tStringBuffer.append("<h1>Hello World</h1>");
tStringBuffer.append("</body>\n" );
tStringBuffer.append("</html>\n" );
tResponsePrintWriter.println(tStringBuffer);
tResponsePrintWriter.close();
}
}

2
• This only handles GET - provide doPost as
well
• Extends HttpServlet from J2EE
• response is used to set headers (e.g. set
cookies) and print HTML
• All HTML has to be printed
• Needs to be compiled with careful
attention to paths and destinations
• Can also be used to handle forms

3
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class form extends HttpServlet
{
public void doPost(final HttpServletRequest request,
final HttpServletResponse response)
throws ServletException, IOException
{
final String name = request.getParameter("name");
response.setContentType("text/html");
final PrintWriter responsePrintWriter =
response.getWriter();
StringBuffer content = new StringBuffer();
content.append("<html>\n" );
content.append("<head>\n" );
content.append("<title>Hello"+ name + "</title>\n" );
content.append("</head>\n" );
content.append("<body>\n" );
content.append("<h1>Hello " + name + "</h1>");
content.append("</body>\n" );
content.append("</html>\n" );
responsePrintWriter.println(content);
responsePrintWriter.close();
}

public void doGet(final HttpServletRequest request,


final HttpServletResponse response)
throws ServletException, IOException
{
doPost(request,response);
}
}

4
5
• This uses same method to do GET and
POST
• request is used to get things like form
variables, cookie values
• Needs an HTML form page

6
JSP
• Combination of static part (HTML) and
dynamic part (JAVA)
• Compiled by server the first time it is
loaded via web page or after any change
• HTML turned into println commands,
then whole page run as a servlet.
• Pre-defined variables include response
request and out
• Commands (scriptlets) enclosed in
<% ... %> tags
• Expressions enclosed in <%= ... %> tags
• Declarations enclosed in <%! ... %> tags

7
<%@page language="java" import="java.io.*"%>
<html>
<head>
<title>Footballer Search</title>
</head>
<body>
<h1>Famous Footballers</h1>
<%
String surname=request.getParameter("surname");
out.println(surname);
%>
</body>
</html>

8
<%@page language="java" import="java.io.*"%>
<html>
<head>
<title>Footballer Search</title>
</head>
<body>
<h1>Famous Footballers</h1>
<%
String surname=request.getParameter("surname");
if(surname!=null){
out.println(surname);
}
else{
%>
<form action="<%= request.getRequestURI() %>" method=post>
<p>Enter surname:
<input name="surname"></p>
</form>
<%
}
%>
</body>
</html>

9
<%@page language="java" import="java.sql.*,java.io.*"%>
<html>
<head>
<title>Footballer Search</title>
</head>
<body>
<h1>Famous Footballers</h1>
<%
String surname=request.getParameter("surname");
if(surname!=null){
final Driver driver =
(Driver)Class.forName
("org.gjt.mm.mysql.Driver").newInstance();
final Connection connection =
DriverManager.getConnection(
"jdbc:mysql://mysql.dur.ac.uk/Pdcs0spb_football","","");
Statement statement = connection.createStatement();
String query = "SELECT FirstName,SecondName,SUM(Scored) as S"
+ " FROM Appearances WHERE SecondName=’" + surname + "’"
+ " GROUP BY FirstName, SecondName "
+ " ORDER BY SecondName,FirstName";
ResultSet resultSet = statement.executeQuery(query);
%>
<table border=1>
<tr> <th>Player</th> <th>Total goals</th> </tr>
<%
while (resultSet.next())
{
%>
<tr>
<td>
<%= resultSet.getString("FirstName") %>
<%= resultSet.getString("SecondName") %>
</td>

10
<td>
<%= resultSet.getInt("S") %>
</td>
</tr>
<%
}
out.println("</table>");
resultSet.close();
connection.close();
}
%>
<form action="<%= request.getRequestURI()%>" method=post>
<p>Enter surname:
<input name="surname"<% if(surname!=null)
out.println(" value=\"" + surname + "\""); %>></p>
</form>
</body>
</html>

11

You might also like