|
|
| |||||||
| |||||||||
|
| 4.4 InternationalizationThe JDK 1.0.2 (and consequently also the JSDK 1.0 which is built
on top of it) introduced Streams for byte-based data. Servlets
have a The JDK 1.1 added support for other character encodings with Readers and Writers which use char to byte and byte to char converter classes that can be selected by specifying an encoding name.
[API 2.0] In version 2.0+ of the Servlet API Streams are still used for
byte-based binary data (as shown in section 2.4).
Text should be read from a
Reader which is returned by
ServletRequest.getReader() and written to a
PrintWriter which is returned by
ServletResponse.getWriter(). The getReader
method returns a Reader which uses the correct character
encoding as specified in the request message's Content-Type
header. The getWriter() method uses the character encoding
which was specified in the response message before requesting the
PrintWriter. If no encoding was specified, the Servlet
engine may guess an approriate encoding automatically. Even if the
character encoding can be guessed it is advisable to specify it
explicitly to avoid unnecessary caching of the response body.
Example. The following code sends the greek alphabet (which cannot be represented in ISO-8859-1) in a text/html body using the Unicode-2-0 character encoding:
res.setContentType("text/html;charset=Unicode-2-0");
PrintWriter out = res.getWriter();
for(char c='\u0391'; c<='\u03A9'; c++) out.print(c);
Here is a snapshot of the output:
4.5 User Authentication via SessionsWe've already seen sessions that were created implicitly in the
Sessions can also be used for authentication. In contrast to HTTP Basic Authentication a session can be invalidated which enables users to log out without quitting the Web Browser (which is required with Basic Authentication because there is no way to force a browser to delete the authentication credentials). The following 1: import java.io.*; 2: import javax.servlet.*; 3: import javax.servlet.http.*; 4: 5: public final class SessionAuthServlet extends HttpServlet 6: { 7: protected void doGet(HttpServletRequest req, HttpServletResponse res) 8: throws ServletException, IOException 9: { 10: sendPage(req, res, req.getSession(false)); 11: } 12: 13: protected void doPost(HttpServletRequest req, HttpServletResponse res) 14: throws ServletException, IOException 15: { 16: if(req.getParameter("login") != null) 17: { 18: HttpSession session = req.getSession(true); 19: String name = req.getParameter("name"); 20: if(name == null || name.length()==0) name = "Anonymous"; 21: session.putValue("name", name); 22: sendPage(req, res, session); 23: } 24: else 25: { 26: HttpSession session = req.getSession(false); 27: if(session != null) session.invalidate(); 28: sendPage(req, res, null); 29: } 30: } 31: 32: private void sendPage(HttpServletRequest req, HttpServletResponse res, 33: HttpSession session) 34: throws ServletException, IOException 35: { 36: res.setContentType("text/html"); 37: res.setHeader("pragma", "no-cache"); 38: PrintWriter o = res.getWriter(); 39: o.print("<HTML><HEAD><TITLE>SessionAuthServlet</TITLE></HEAD><BODY>"); 40: if(session == null) 41: o.print("<FORM METHOD=POST>Please enter your name: "+ 42: "<INPUT TYPE=TEXT NAME=\"name\">"+ 43: "<INPUT TYPE=SUBMIT NAME=\"login\" VALUE=\"Log in\">"+ 44: "</FORM></BODY></HTML>"); 45: else 46: o.print("Hi " + session.getValue("name") + 47: "<P><FORM METHOD=POST><INPUT TYPE=SUBMIT NAME=\"logout\" "+ 48: "VALUE=\"Log out\"></FORM></BODY></HTML>"); 49: o.close(); 50: } 51: } 4.6 Relative URLsWhen a Servlet creates a response which contains a relative URL, e.g. a hyperlink in an HTML document, you have to make sure that the URL points to the right directory. Example. The following document is mounted on a server
as <HTML><HEAD><TITLE>Form</TITLE></HEAD><BODY> <FORM ACTION="/servlet/PreviewServlet" METHOD=GET> ... </FORM> </BODY></HTML> There are also product images in the same directory, e.g.
The Servlet which is mounted as 4.7 LoggingA Web Server is usually running as a background process without
connected stdio streams. Even if the server is running in a
console, a Servlet can not expect to access that console with
the
Two convenience methods are implemented in
Exceptions which cause one of the Servlet lifecycle methods (or
a |
| ||||||
|
This page's content was last updated 1999-11-04. © 1997-2005 Stefan Zeiger (http://www.novocode.com/contact.html). All rights reserved. Page Location: http://www.novocode.com/doc/servlet-essentials/chapter4b.html |
| |||||