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
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
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.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWWW extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n" +
"<HTML>\n" +
"<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n" +
"<BODY>\n" +
"<H1>Hello WWW</H1>\n" +
"</BODY></HTML>");
}
}
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
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Servlet 2.4 Examples</display-name>
<description>
Servlet 2.4 Examples.
</description>
<!-- Define servlets that are included in the example application -->
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>jan.HelloWorld</servlet-class>
</servlet>
<servlet>
<servlet-name>LocaleServlet</servlet-name>
<servlet-class>jan.LocaleServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>HelloWWW</servlet-name>
<servlet-class>HelloWWW</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LocaleServlet</servlet-name>
<url-pattern>/servlet/LocaleServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/servlet/HelloWorld</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>HelloWWW</servlet-name>
<url-pattern>/servlet/HelloWWW</url-pattern>
</servlet-mapping>
</web-app>
HTTP headers can be found by
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Enumeration;
public class Headers extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Enumeration enumm = request.getHeaderNames();
while (enumm.hasMoreElements()) {
String header = (String) enumm.nextElement();
out.println(header + " " + request.getHeader(header) + "<br>");
}
}
}
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:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Enumeration;
import java.util.ResourceBundle;
import java.util.Locale;
import java.util.MissingResourceException;
public class GetLocales extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n" +
"<HTML>\n" +
"<HEAD><TITLE>First Screen</TITLE></HEAD>\n" +
"<BODY>\n" +
"<H1>User Locales</H1>\n <P>");
String acceptLang = request.getHeader("Accept-Language");
out.print("Accept-Language: " + acceptLang + "<br>");
Locale preferredLocale = request.getLocale();
Enumeration allLocales = request.getLocales();
out.print("Preferred locale: " + preferredLocale + "<br>");
while (allLocales.hasMoreElements()) {
Locale next = (Locale) allLocales.nextElement();
out.print(" " + next.toString() + "<br>");
}
}
}
The following program checks if there is a properties file matching any of the desired locales
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Enumeration;
import java.util.ResourceBundle;
import java.util.Locale;
import java.util.MissingResourceException;
public class FirstScreen extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n" +
"<HTML>\n" +
"<HEAD><TITLE>First Screen</TITLE></HEAD>\n" +
"<BODY>\n" +
"<H1>First Screen</H1>\n <P>");
Enumeration allLocales = request.getLocales();
ResourceBundle bundle = null;
while (allLocales.hasMoreElements()) {
Locale loc = (Locale) allLocales.nextElement();
bundle = getBundle(loc);
if (bundle != null) {
break;
}
}
if (bundle != null) {
String text = bundle.getString("Language");
out.print("Language: " + text);
} else {
out.print("No bundle found");
}
}
private ResourceBundle getBundle(Locale loc) {
ResourceBundle bundle = null;
try {
bundle = ResourceBundle.getBundle("FirstScreen", loc);
} catch (MissingResourceException e) {
return null;
}
// the bundle lookup mechanism can find bundles for
// other "related" locales. Discard them
if (! bundle.getLocale().equals(loc)) {
return null;
}
return bundle;
}
}
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:);