import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SomeServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Use "request" to read incoming HTTP headers (e.g. cookies)
// and HTML form data (e.g. data the user entered and submitted)
// Use "response" to specify the HTTP response line and headers
// (e.g. specifying the content type, setting cookies).
PrintWriter out = response.getWriter();
// Use "out" to send content to browser
}
}
bin
directory contains Unix shell and DOS batch files
to start a server on port 8088
servlet.jar
in the classpath
webapps/jan/WEB-INF/classes
If a request is made to the server, it returns
telnet localhost 8088
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /jan/servlet/HelloWorld HTTP/1.1
HTTP/1.0 200 OK
Servlet-Engine: Tomcat Web Server/3.2.1 (JSP 1.1; Servlet 2.2; Java 1.4.2; Linux 2.6.10-1.12_FC2custom i386; java.vendor=Sun Microsystems Inc.)
Hello World
Connection closed by foreign host.
telnet localhost 8088
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /jan/servlet/HelloWWW HTTP/1.1
HTTP/1.0 200 OK
Content-Type: text/html
Servlet-Engine: Tomcat Web Server/3.2.1 (JSP 1.1; Servlet 2.2; Java 1.4.2; Linux 2.6.10-1.12_FC2custom i386; java.vendor=Sun Microsystems Inc.)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>Hello WWW</TITLE></HEAD>
<BODY>
<H1>Hello WWW</H1>
</BODY></HTML>
Connection closed by foreign host.
webapps
jan
jan/jsp
jan/WEB-INF
jan/WEB-INF/classes
jan/WEB-INF/classes
jan/jsp
HelloWWW
is referenced by
http://jan.newmarch.name:8080/jan/servlet/helloWWW
hello.jsp
is referenced by
http://jan.newmarch.name:8080/jan/jsp/hello.jsp
jan/WEB-INF/web.xml
to link servlet URLs and classes
HTTP headers can be found by
The output from a request by Firefox with language set to French, English US and then English is
Cache-Control max-age=0
Host localhost:8088
Accept text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
User-Agent Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.6) Gecko/20050225 Firefox/1.0.1
Keep-Alive 300
Accept-Language fr,en-us;q=0.7,en;q=0.3
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection keep-alive
The following program uses the Accept-Language
field to find
possible locales:
The following program checks if there is a properties file matching any of the desired locales
request.getParameter(String name)
package hall;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class ThreeParams extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Reading Three Request Parameters";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY>\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>\n" +
"<UL>\n" +
" <LI>param1: "
+ request.getParameter("param1") + "\n" +
" <LI>param2: "
+ request.getParameter("param2") + "\n" +
" <LI>param3: "
+ request.getParameter("param3") + "\n" +
"</UL>\n" +
"</BODY></HTML>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Cookie cookie = new Cookie("sessionID", "1234");
response.addCookie(cookie);
Cookie[] cookies = request.getCookies();
HttpSession session = request.getSession(true);
This creates a session object if one did not exist before,
or returns one if it did exist before
session.putValue(key, value);
session.getValue(key);
response.setContentType("text/html; charset=Big5");
Writer out = response.getWriter();
Accept-Charset
HTTP parameter from the browser
Form
, you use the
input method for the browser in its locale
a.jsp:
This is what you will get depending on the character encoding that page a.jsp has been marked with (for details on the character encoding for a page see the beginning of this document):
character encoding of a.jsp | result of submitting the query |
---|---|
KOI8-R | b.jsp?n=a%C1 |
windows-1251 | b.jsp?n=a%E0 |
UTF-8 | b.jsp?n=a%D0%B0 |
String original = request.getParameter(...);
String converted = new String(original.getBytes("ISO-8859-1"),
"windows-1251:);