Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
05.09.2008
The (HTTP) request class/object
Objectives of this lesson
• You will learn how to:
–
–
–
–
–
Identify characteristics of the client
Identify characteristics of the message
Retrieve information in HTTP-headers
Look at the content of the HTTP-request body
Retrieve unknown HTML-form parameters
1
05.09.2008
API methods we will explore
Method
Method
getAuthType()
getContentLength()
getCookies()
getDateHeader()
getHeader()
getHeaders()
getContentType()
getInputStream()
getParameter()
getParameterNames()
getHeaderNames()
getIntHeader()
getParameterValues()
getMethod()
getProtocol()
getPathInfo()
getReader()
getPathTranslated()
getRemoteAddr()
getRemoteHost()
getQueryString()
getScheme()
getRemoteUser()
getServerName()
getRequestURI()
getServerPort()
getServletPath()
isSecure()
HTTPServletRequest
ServletRequest (generic)
Obtaining information about
clients (the borwser)
2
05.09.2008
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class clientId extends HttpServlet
{
public void service(HttpServletRequest
request,
HttpServletResponse response)
throws ServletException,
java.io.IOException
{
response.setContentType("text/html");
PrintWriter out
=
response.getWriter();
out.print("<html><head></head><body>");
out.print("Klientidentifikasjon:"
+
+
+
+
request.getRemoteUser()
+ request.getRequestURI()
IP:" + request.getRemoteAddr()
name:" + request.getRemoteHost());
"<br/>Username:"
"<br/>ULI:"
"<br/>client
"<br/>client
+
out.print("</body></html>");
out.close();
}
}
3
05.09.2008
Obtaining information about the
message
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class messageInfo extends HttpServlet
{
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
java.io.IOException
{
response.setContentType("text/html");
PrintWriter out
=
response.getWriter();
out.print("<html><head></head><body>");
out.print("Client info:"
+
+
+
+
+
+
+
+
+
+
"<br/>Scheme:"
"<br/>Secure:"
"<br/>Port:"
+
+
+
request.getScheme()
request.isSecure()
request.getServerPort()
+
"<br/>Server name:"
request.getServerName()
"<br/>Authentication type:"
"<br/>Method:"
+
"<br/>Protocol:"
"<br/>Path:"
+
+
request.getAuthType()
request.getMethod()
+
request.getProtocol()
request.getPathInfo()
"<br/>Translated path:"
request.getPathTranslated());
out.print("</body></html>");
out.close();
}
}
4
05.09.2008
Retrieving header
GET /index.html HTTP/1.0
ContentType: text/plain
ContentLength: 100
WheelsUnderTheCar: 4
String str = requestl.getHeader(”WheelsUnderTheCar”);
int no = request.getIntHeader(”WheelsUnderTheCar”);
5
05.09.2008
Retrieving timestamp headers
java.util.Date dato;
long tidsstempel = spørsmål.getDateHeader(”Date”);
if (tidsstempel != -1)
{
dato = new java.util.Date(tidsstempel);
}
Retrieving repeated headers
GET /index.html HTTP/1.0
Accept-Language: en
Accept-Language: no
6
05.09.2008
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet. http.*;
public class multiHead extends HttpServlet
{
public void service(HttpServletRequest
request,
HttpServletResponse response)
throws ServletException,
java.io. IOException
{
response.setContentType( "text/html");
PrintWriter out
=
response.getWriter();
out.print("<html><head></head><body>");
out.print("Values of Accept-Language");
Enumeration list
=
request.getHeaders(
"Accept-Language");
String value;
while
(list.hasMoreElements())
{
value
= (String)list.nextElement();
+ value);
out.print("<br/>"
}
out.print("</body></html>");
out.close();
}
}
7
05.09.2008
Retrieving unknown headers
import java .io .*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class headers extends HttpServlet
{
public void service(HttpServletRequest
request,
HttpServletResponse response)
throws ServletException,
IOException
{
response.setContentType("text/html");
PrintWriter out
=
response.getWriter();
out.print("<html><head></head><body>");
out.print("Headers<br/>" );
Enumeration list
=
request.getHeaderNames ();
String name , value;
while
(list.hasMoreElements())
{
= (String)list.nextElement();
= request.getHeader(name);
out.print ("<br/>(" + name + "," + value +")" );
}
out.print("</body></html>");
out.close();
}
name
value
}
8
05.09.2008
Manually determining the HTTP
method
9
05.09.2008
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class redirect extends HttpServlet
{
public void service(HttpServletRequest
request,
HttpServletResponse response)
throws ServletException,
IOException
{
= request.getMethod();
(met.equalsIgnoreCase("GET"))
{
myGet(request, response);
}
else if (met.equalsIgnoreCase("POST"))
{
myPost(request, response);
}
else if (met.equalsIgnoreCase("HEAD"))
{
myHead(request, response);
}
}
String met
if
public void myGet(HttpServletRequest
request,
HttpServletResponse response)
throws ServletException,
IOException
{
// my implementation
}
public void myPost(HttpServletRequest
request,
HttpServletResponse response)
throws ServletException,
IOException
{
// my implementation
}
public void myHead(HttpServletRequest
request,
HttpServletResponse response)
throws ServletException,
IOException
{
// my implementation
}
}
10
05.09.2008
Serving multiple platforms
One servlet – multiple platforms
11
05.09.2008
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class serveThePeople extends HttpServlet
{
protected void doGet(HttpServletRequest request,
HttpServletResponse response
)
throws ServletException,
IOException
{
response.setHeader( "Cache-Control",
"no-cache, must-revalidate"
response.setHeader( "Pragma", "no-cache"
String browser
PrintWriter out
if
(
{
);
);
= request.getHeader( "User-Agent" );
= response.getWriter();
browser.indexOf("MSIE")
!= -1 )
response.setContentType("text/html");
out.println(
"<html>"
+ "<head>"
"<title>PC</title>"
+
+ "</head>"
+ "<body>"
"<h1>Hi PC user – This is me!</h1><br/>"
+
"<img src=\"../frode.jpg\" alt=\"me....\">"
+
+ "</body>"
+"</html>");
}
12
05.09.2008
if
(
{
browser.indexOf("UP")
!= -1 )
response.setContentType("text/vnd.wap.wml");
out.println(
"<?xml version=\"1.0\"?>"
+"<!DOCTYPE wml PUBLIC "
+"\"-//WAPFORUM//DTD WML 1.1//EN\" "
+"\"http://www.wapforum.org/DTD/wml_1.1.xml\">"
+"<wml>"
+ "<card>"
"<p>"
+
"Hi WAP user – This is me"
+
"</p>"
+
"<p>"
+
"<img src=\"../frode.wbmp\" alt=\"me...\"/>"
+
"</p>"
+
+ "</card>"
+"</wml>");
}
if
(
{
browser.indexOf("Pixo")
!= -1 )
response.setContentType("text/html");
out.println(
"<html>"
+ "<head>"
"<title>Velcome i-mode user</title>"
+
"</head>"
+
+ "<body>"
"Hi i-mode user – This is me!<br/>"
+
"<img src=\"../frode.gif\" alt=\"me....\"/>"
+
+ "</body>"
+"</html>");
}
}
}
13
05.09.2008
Reading the body of a HTTP
request
14
05.09.2008
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class readBody extends HttpServlet
{
public void doGet(HttpServletRequest
request,
HttpServletResponse response)
throws ServletException,
IOException
{
response.setContentType("text/html");
PrintWriter out
=
response.getWriter();
out.print("<html><head></head><body>"
+"Select a file<br/>"
+"<form enctype=\"multipart/form-data\""
method=\"post\">"
+"
+"<input type=\"file\""
name=\"filenavn\"/>"
+"
+"<input type=\"submit\""
value=\"send\"/>"
+"
+"</form></body></html>");
out.close();
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
IOException
{
response.setContentType("text/html");
PrintWriter out
=
BufferedReader in
response.getWriter();
=
request.getReader();
out.print("<html><head></head><body>");
out.print("This is the body of the message<br/>");
= in.readLine();
(line != null)
String line
while
{
out.print(line+"<br/>");
line
=
in.readLine();
}
out.print("</body></html>");
out.close();
}
}
15
05.09.2008
Retrieving checkbox parameters
16
05.09.2008
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class eatingFruit extends HttpServlet
{
public void doGet(HttpServletRequest
request,
HttpServletResponse response)
throws ServletException,
IOException
{
response.setContentType("text/html");
PrintWriter out
=
response.getWriter();
out.print("<html><head></head><body>"
+
+
+
+
+
+
+
+
+
+
+
+
"<form method=\”post"\">"
"<input type=\”checkbox\" name=\”orange\">"
"orange<br/>"
"<input type=\”checkbox\" name=\”apple\">"
"eple<br/>"
"<input type=\”checkbox\" name=\”banana\">"
"banan<br/>"
"<input type=\”checkbox\" name=\”kiwi\">"
"kiwi<br/>"
"<input type=\”checkbox\" name=\”grape\">"
"druer<br/>"
"<input type=\”submit\" value=\”purchase\">"
+
"</form></body></html>");
out.close();
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
IOException
{
response.setContentType("text/html");
PrintWriter out
=
response.getWriter();
out.print("<html><head></head><body>");
out.print("You selected the following<br/>");
Enumeration list
=
request.getParameterNames();
String fruit;
while
(list.hasMoreElements())
{
fruit
= (String)list.nextElement();
+ "<br/>");
out.print(fruit
}
out.print("</body></html>");
out.close();
}
}
17