Professional Documents
Culture Documents
Java Development Kit (JDK) version 6 GlassFish server MySQL database server AffableBean project AffableBean project Notes: The NetBeans IDE requires the Java Development Kit (JDK) to run properly. If you do not have any of the resources listed above, the JDK should be the first item that you download and install. The NetBeans IDE Java Bundle includes Java Web and EE technologies, which are required for the application you build in this tutorial. The NetBeans IDE Java Bundle also includes the GlassFish server, which you require for this tutorial. You could download the GlassFish server independently, but the version provided with the NetBeans download has the added benefit of being automatically registered with the IDE. You can follow this tutorial unit without having completed previous units. To do so, see the setup instructions, which describe how to prepare the database and establish connectivity between the IDE, GlassFish, and MySQL. v3 or Open Source Edition 3.0.1 version 5.1 snapshot 8 snapshot 9
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes
1/25
24/08/13
m e a t s = m e a t s b a k e r y = b a k e r y
messages_cs.properties
m e a t s = m a s o b a k e r y = p e i v o
In the above example, 'm e s s a g e s ' represents the base name, and the locale identifier is the two-letter code which is appended using an underscore. (i.e., 'e n ' for English, 'c s ' for Czech). The two-letter codes are derived from the international ISO 639 standard, which lists codes that represent the names of languages. The ISO 639 standard is adopted by the W3C Internationalization Activity and is used by all major browsers (these are the codes understood in the A c c e p t L a n g u a g eHTTP header). It is also internalized in the j a v a . u t i l . L o c a l eclass.
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes
2/25
24/08/13
Note that the wizard provides a description for the selected file type: Creates a resource b undle (. p r o p e r t i e s ) file suitab le for internationalizing applications b y
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes
3/25
24/08/13
6. Click Finish. The m e s s a g e s . p r o p e r t i e sresource bundle is generated and opens in the editor. Note that the new m e s s a g e s . p r o p e r t i e sfile name does not have a language code appended to it, as was previously described. This is because this file will be used as the default resource bundle. The default resource bundle is applied when the Java runtime environment does not find a direct match for the requested locale. 7. Open the project's i n d e x . j s pfile in the editor and note that the following text is currently used: Greeting: W e l c o m et ot h eo n l i n eh o m eo ft h eA f f a b l eB e a nG r e e nG r o c e r . Introductory Message: E n j o yb r o w s i n ga n dl e a r n i n gm o r ea b o u to u ru n i q u eh o m e
d e l i v e r ys e r v i c eb r i n g i n gy o uf r e s ho r g a n i cp r o d u c e ,d a i r y ,m e a t s ,b r e a d sa n d o t h e rd e l i c i o u sa n dh e a l t h yi t e m st oy o u rd o o r s t e p .
Also, note that we'll need language-specific names for the four categories that display when i n d e x . j s prenders in the browser. Since these names are currently taken from the database, we can use them as keys in the resource bundle. Recall that one of the implementation details outlined above states that "if the b rowser's preferred language is neither English nor Czech, the site displays text in English." Therefore, the values that we apply to the
#w e l c o m ep a g e g r e e t i n g = W e l c o m et ot h eo n l i n eh o m eo ft h eA f f a b l eB e a nG r e e nG r o c e r . i n t r o T e x t = O u ru n i q u eh o m ed e l i v e r ys e r v i c eb r i n g sy o uf r e s ho r g a n i cp r o d u c e ,d a i r y , m e a t s ,b r e a d sa n do t h e rd e l i c i o u sa n dh e a l t h yi t e m sd i r e c tt oy o u rd o o r s t e p . #c a t e g o r i e s d a i r y = d a i r y
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes 4/25
24/08/13
m e a t s = m e a t s b a k e r y = b a k e r y f r u i t \& \v e g = f r u i t&v e g
Comments are added using a number sign ('# '). Also, because the f r u i t&v e gcategory name contains spaces, it is necessary to escape the space characters using a backslash ('\ ') in order to apply the name as a resource bundle key. We are now finished with the default resource bundle for the application's welcome page. Let's continue by creating resource bundles for the customer-specified languages. 9. In the Projects window, expand the Source Packages node, then right-click the r e s o u r c e s>
A locale can be defined by both a language and a geographic region. The optional country code which can be used to specify the region can be applied to define formatting for dates, time, numbers, and currency. For more information, see the technical article, Understanding Locale in the Java Platform. 11. Click the Add Locale button again, then enter 'c s ' in the Language Code combo box and click OK. The Customizer dialog displays as follows.
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes
5/25
24/08/13
12. Click Close. In the Projects window, note that your resource bundles look as follows. You can expand a resource bundle to view the keys it contains.
13. Right-click any of the three resource bundles and choose Open. The Properties editor opens, enabling you to view and edit key-value pairs for all locales in a table layout.
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes
6/25
24/08/13
o r g a n i c k c hp r o d u k t ,m l n c hv r o b k ,u z e n i n ,p e i v aad a l c hd e l i k a t e sa z d r a v c hv r o k a k ed v e m .
dairy: m l n v r o b k y meats: m a s o bakery: p e i v o fruit & veg: o v o c eaz e l e n i n y You can also add a comment to each key-value pair. Any text you enter into the Comment field in the Properties editor is added to the resource bundle text file above the key-value pair as a comment (i.e., following a '# ' sign). 15. Double-click the m e s s a g e s _ c s . p r o p e r t i e sfile node in the Projects window. Note that the text file has been updated according to your changes in the Properties editor.
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes
7/25
24/08/13
The L o c a l i z a t i o n C o n t e x tclass belongs to the j a v a x . s e r v l e t . j s p . j s t l . f m tpackage. You can verify this by viewing the JSTL 1.1 API Reference online. 4. Click OK. The new context parameter is added to the table of existing context parameters under the General tab. 5. Click the deployment descriptor's XML tab. Note that the following entry has been added to the file:
< c o n t e x t p a r a m > < p a r a m n a m e > j a v a x . s e r v l e t . j s p . j s t l . f m t . l o c a l i z a t i o n C o n t e x t < / p a r a m n a m e > < p a r a m v a l u e > r e s o u r c e s . m e s s a g e s < / p a r a m v a l u e > < / c o n t e x t p a r a m >
< d i vi d = " i n d e x L e f t C o l u m n " > < d i vi d = " w e l c o m e T e x t " > < ps t y l e = " f o n t s i z e :l a r g e r " > < f m t : m e s s a g ek e y = ' g r e e t i n g ' / > < / p > < p > < f m t : m e s s a g ek e y = ' i n t r o T e x t ' / > < / p > < / d i v > < / d i v >
3. Add < f m t : m e s s a g e >tags for the four category names, but use the $ { c a t e g o r y . n a m e }expression as the value for the k e yattribute. Since the category name is also used as the value for the < i m g >tag's a l tattribute, follow the same procedure. The page's right column will look as follows.
< d i vi d = " i n d e x R i g h t C o l u m n " > < c : f o r E a c hv a r = " c a t e g o r y "i t e m s = " $ { c a t e g o r i e s } " > < d i vc l a s s = " c a t e g o r y B o x " > < ah r e f = " < c : u r lv a l u e = ' c a t e g o r y ? $ { c a t e g o r y . i d } ' / > " > < s p a nc l a s s = " c a t e g o r y L a b e l " > < / s p a n > < s p a nc l a s s = " c a t e g o r y L a b e l T e x t " > < f m t : m e s s a g ek e y = ' $ { c a t e g o r y . n a m e } ' / >
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes 8/25
24/08/13
< / s p a n > < i m gs r c = " $ { i n i t P a r a m . c a t e g o r y I m a g e P a t h } $ { c a t e g o r y . n a m e } . j p g " a l t = " < f m t : m e s s a g ek e y = ' $ { c a t e g o r y . n a m e } ' / > " c l a s s = " c a t e g o r y I m a g e " > < / a > < / d i v > < / c : f o r E a c h > < / d i v >
4. Finally, ensure that you have the f m ttag library declared in the web page. Enter the following at the top of the file:
< f m t >tags elsewhere in the project, it may make more sense to remove the tag library declaration from
individual page views, and add it to the header (h e a d e r . j s p f ) file. This practice is adopted in snapshot 9 (and later snapshots). You've now completed the tasks necessary for providing bilingual support for the application's welcome page. The following step demonstrates how to test the language support in your browser.
5. Browser prefers Korean and English; Korean takes precedence English displays 6. Browser prefers Korean and English; English takes precedence English displays 7. Browser prefers Korean and Czech; Korean takes precedence 8. Browser prefers Korean and Czech; Czech takes precedence 9. Browser prefers English and Czech; English takes precedence 10. Browser prefers English and Czech; Czech takes precedence Czech displays Czech displays English displays Czech displays
11. Browser prefers, in the following order, English, Czech, Korean English displays 12. Browser prefers, in the following order, English, Korean, Czech English displays 13. Browser prefers, in the following order, Czech, English, Korean Czech displays 14. Browser prefers, in the following order, Czech, Korean, English Czech displays 15. Browser prefers, in the following order, Korean, English, Czech English displays 16. Browser prefers, in the following order, Korean, Czech, English Czech displays
Rather than stepping through all 16 scenarios, we'll demonstrate how to examine scenario 3 above, in which the browser's preferred language is Czech, using the Firefox browser. 1. In Firefox, choose Tools > Options (Firefox > Preferences on Mac). In the window that displays, click the Content tab.
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes
9/25
24/08/13
2. Under the Languages heading, click Choose. 3. Select any language that is currently listed in the provided text area, then click Remove. (You should remember your language list and reinstate languages after completing this tutorial.) 4. Click the 'Select Language to Add' drop-down and select C z e c h[ c s ] . Then click the Add button. The Czech language is added to the text area.
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes
10/25
24/08/13
Create Toggle Display and Synchronize with the Browser's Preferred Language
1. Use the Go to File dialog to open the h e a d e rJSP fragment in the editor. Press Alt-Shift-O (Ctrl-Shift-O on Mac), then type 'h ' in the dialog and click OK.
2. In the h e a d e r . j s p ffile, locate the first < d i vc l a s s = " h e a d e r W i d g e t " >tag (line 56), and replace the [
24/08/13
< d i vc l a s s = " h e a d e r W i d g e t " > < % -l a n g u a g es e l e c t i o nw i d g e t% > e n g l i s h|< d i vc l a s s = " b u b b l e " > < ah r e f = " c h o o s e L a n g u a g e ? l a n g u a g e = c s " > e s k y < / a > < / d i v > < / d i v >
This markup implements the language toggle's appearance when English is the displayed language. In other words, the toggle provides a link allowing the user to select the Czech (i.e., ' e s k y ') option. The link is used to send a request for c h o o s e L a n g u a g e , and creates a query string (? l a n g u a g e = c s ) that specifies the requested language code. Note: Recall that in Unit 5, Preparing the Page Views and Controller Servlet, you set the C o n t r o l l e r S e r v l e t to handle the / c h o o s e L a n g u a g eURL pattern. Snapshot 8 includes the jQuery JavaScript library and takes advantage of various UI effects to enhance the appearance and behavior of the website. Aside from a jQuery plugin for client-side validation (discussed in the previous tutorial unit), the snapshot implements an easing effect for category headings in the welcome page, as well as for category buttons in the category page. Configuration is included in h e a d e r . j s p fof the project snapshot. Rounded corners are implemented using CSS3's border-radius property (applied in
a f f a b l e b e a n . c s s ).
3. Run the project ( ) to see what the toggle looks like in the browser.
Currently, the language toggle appears as in the above image regardless of what language the page displays in. In the next step, you integrate JSTL logic into the toggle so that it renders according to the language displayed on the page. 4. Modify the toggle implementation as follows.
< d i vc l a s s = " h e a d e r W i d g e t " > < % -l a n g u a g es e l e c t i o nw i d g e t% > < c : c h o o s e > < c : w h e nt e s t = " $ { p a g e C o n t e x t . r e q u e s t . l o c a l e . l a n g u a g en e' c s ' } " > e n g l i s h < / c : w h e n > < c : o t h e r w i s e > < c : u r lv a r = " u r l "v a l u e = " c h o o s e L a n g u a g e " > < c : p a r a mn a m e = " l a n g u a g e "v a l u e = " e n " / > < / c : u r l > < d i vc l a s s = " b u b b l e " > < ah r e f = " $ { u r l } " > e n g l i s h < / a > < / d i v > < / c : o t h e r w i s e > < / c : c h o o s e >| < c : c h o o s e > < c : w h e nt e s t = " $ { p a g e C o n t e x t . r e q u e s t . l o c a l e . l a n g u a g ee q' c s ' } " > e s k y < / c : w h e n > < c : o t h e r w i s e > < c : u r lv a r = " u r l "v a l u e = " c h o o s e L a n g u a g e " > < c : p a r a mn a m e = " l a n g u a g e "v a l u e = " c s " / >
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes 12/25
24/08/13
< / c : u r l > < d i vc l a s s = " b u b b l e " > < ah r e f = " $ { u r l } " > e s k y < / a > < / d i v > < / c : o t h e r w i s e > < / c : c h o o s e > < / d i v >
In the above implementation, you rely on conditional tags from JSTL's c o r etag library to display the left and right portions of the toggle according to the language used by the request locale. What is the "language used by the request locale"? When a request is made, the browser passes a list of preferred locales in the A c c e p t L a n g u a g eHTTP header. The Java runtime environment on the server reads the list and determines the best match based on the locales defined by the application's resource bundles. This match is then recorded in the S e r v l e t R e q u e s tobject, and can be accessed using the g e t L o c a l emethod. For example, you could access the preferred locale from a servlet with the following statement.
r e q u e s t . g e t L o c a l e ( ) ;
You can use the IDE's HTTP Monitor (Window > Debugging > HTTP Server Monitor) to examine HTTP headers for client requests. In order to use the HTTP Monitor, you need to first activate it for the server you are using. Unit 8, Managing Sessions provides a demonstration under the sub-section, Examining Client-Server Communication with the HTTP Monitor. To determine the language of the preferred locale, you use the L o c a l eclass' g e t L a n g u a g emethod. Again, from a servlet you could access the language of the client request's preferred locale with the following.
r e q u e s t . g e t L o c a l e ( ) . g e t L a n g u a g e ( ) ;
Returning to the code you just added to the h e a d e r . j s p ffragment, you utilize the p a g e C o n t e x t . r e q u e s t implicit object to access the S e r v l e t R e q u e s tfor the given client request. Using dot notation, you then proceed to call the same methods as you would from a servlet. In the above example, accessing the "language used by the request locale" is as simple as:
$ { p a g e C o n t e x t . r e q u e s t . l o c a l e . l a n g u a g e }
Note: The above implementation uses < c : u r l >tags to set up the toggle link. This is done in order to properly encode the request URL in the event that URL rewriting is used as a means for session tracking. Unit 8, Managing Sessions provides a brief explanation of how the < c : u r l >tags can be used. 5. Add a basic language test to the h e a d e r . j s p ffile. This will enable us to check whether the toggle is properly rendering according to the client request's preferred language. Enter the following after the page's < b o d y >tag.
< b o d y > < % -L a n g u a g et e s t% > < ps t y l e = " t e x t a l i g n :l e f t ; " > < s t r o n g > t e s t s : < / s t r o n g > < b r > < c o d e > \ $ { p a g e C o n t e x t . r e q u e s t . l o c a l e . l a n g u a g e } < / c o d e > : $ { p a g e C o n t e x t . r e q u e s t . l o c a l e . l a n g u a g e } < / p > < d i vi d = " m a i n " >
6. Ensure that you have set Czech as your browser's preferred language. (If you are following this tutorial unit sequentially, you've already done this. If not, refer to the steps outlined above in Test Supported Languages.) 7. Run the project ( ) and examine the application welcome page in the browser.
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes
13/25
24/08/13
If your browser's preferred language is set to Czech, you can note the following: The test that we introduced in the previous step indicates that 'c s ' is the preferred language. Czech text is displayed in the page. The language toggle provides a link enabling the user to select English.
< f m t : s e t L o c a l e >tag we can manually switch the language used in the page display. We'll also modify the language
toggle so that if the l a n g u a g eattribute has been set, the toggle's appearance is determined according to the l a n g u a g e attribute's value. 1. Open the C o n t r o l l e r S e r v l e tin the editor. Use the Go To File dialog - press Alt-Shift-O (Ctrl-Shift-O on Mac), then type 'c o n t r o l l e r ' and click OK. In the opened file, locate the portion of the d o G e tmethod that handles the
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes
14/25
24/08/13
< % @ t a g l i bp r e f i x = " c "u r i = " h t t p : / / j a v a . s u n . c o m / j s p / j s t l / c o r e "% > < % @ t a g l i bp r e f i x = " f n "u r i = " h t t p : / / j a v a . s u n . c o m / j s p / j s t l / f u n c t i o n s "% > < % @ t a g l i bp r e f i x = " f m t "u r i = " h t t p : / / j a v a . s u n . c o m / j s p / j s t l / f m t "% >
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes 15/25
24/08/13
< % -S e tl a n g u a g eb a s e do nu s e r ' sc h o i c e% > < c : i ft e s t = " $ { ! e m p t yl a n g u a g e } " > < f m t : s e t L o c a l ev a l u e = " $ { l a n g u a g e } "s c o p e = " s e s s i o n "/ > < / c : i f >
< % @ p a g ec o n t e n t T y p e = " t e x t / h t m l ;c h a r s e t = U T F 8 "p a g e E n c o d i n g = " U T F 8 " % > < ! D O C T Y P EH T M LP U B L I C" / / W 3 C / / D T DH T M L4 . 0 1T r a n s i t i o n a l / / E N " " h t t p : / / w w w . w 3 . o r g / T R / h t m l 4 / l o o s e . d t d " >
Since the l a n g u a g evariable is only created when the user clicks the link in the language toggle, you perform a test using < c : i f >tags to determine whether the variable exists before attempting to set the language. When applying the
< f m t : s e t L o c a l e >tag, you set its scope to s e s s i o nas you want the user-selected language to take precedence
for the remainder of his or her session on the website. Also, since this is the first time the f m tlibrary is used in the header, you declare the tag library. You can read the EL expression $ { ! e m p t yl a n g u a g e }as, "False if the l a n g u a g evariable is null or an empty string." See the Java EE 5 Tutorial: Examples of EL Expressions for other available examples. 6. Modify the language toggle implementation so that if a value has been set by the < f m t : s e t L o c a l e >tag, the toggle displays according to the language specified by that value. (You can determine this value using the
< d i vc l a s s = " h e a d e r W i d g e t " > < % -l a n g u a g es e l e c t i o nw i d g e t% > < c : c h o o s e > < % -W h e nu s e rh a s n ' te x p l i c i t l ys e tl a n g u a g e , r e n d e rt o g g l ea c c o r d i n gt ob r o w s e r ' sp r e f e r r e dl o c a l e% > < c : w h e nt e s t = " $ { e m p t y s e s s i o n S c o p e [ ' j a v a x . s e r v l e t . j s p . j s t l . f m t . l o c a l e . s e s s i o n ' ] } " > < c : c h o o s e > < c : w h e nt e s t = " $ { p a g e C o n t e x t . r e q u e s t . l o c a l e . l a n g u a g en e' c s ' } " > e n g l i s h < / c : w h e n > < c : o t h e r w i s e > < c : u r lv a r = " u r l "v a l u e = " c h o o s e L a n g u a g e " > < c : p a r a mn a m e = " l a n g u a g e "v a l u e = " e n " / > < / c : u r l > < d i vc l a s s = " b u b b l e " > < ah r e f = " $ { u r l } " > e n g l i s h < / a > < / d i v > < / c : o t h e r w i s e > < / c : c h o o s e >| < c : c h o o s e > < c : w h e nt e s t = " $ { p a g e C o n t e x t . r e q u e s t . l o c a l e . l a n g u a g ee q' c s ' } " > e s k y < / c : w h e n > < c : o t h e r w i s e > < c : u r lv a r = " u r l "v a l u e = " c h o o s e L a n g u a g e " > < c : p a r a mn a m e = " l a n g u a g e "v a l u e = " c s " / > < / c : u r l > < d i vc l a s s = " b u b b l e " > < ah r e f = " $ { u r l } " > e s k y < / a > < / d i v > < / c : o t h e r w i s e >
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes 16/25
24/08/13
< / c : c h o o s e > < / c : w h e n > < % -O t h e r w i s e ,r e n d e rw i d g e ta c c o r d i n gt ot h es e tl o c a l e% > < c : o t h e r w i s e > < c : c h o o s e > < c : w h e nt e s t = " $ { s e s s i o n S c o p e [ ' j a v a x . s e r v l e t . j s p . j s t l . f m t . l o c a l e . s e s s i o n ' ]n e ' c s ' } " > e n g l i s h < / c : w h e n > < c : o t h e r w i s e > < c : u r lv a r = " u r l "v a l u e = " c h o o s e L a n g u a g e " > < c : p a r a mn a m e = " l a n g u a g e "v a l u e = " e n " / > < / c : u r l > < d i vc l a s s = " b u b b l e " > < ah r e f = " $ { u r l } " > e n g l i s h < / a > < / d i v > < / c : o t h e r w i s e > < / c : c h o o s e >| < c : c h o o s e > < c : w h e nt e s t = " $ { s e s s i o n S c o p e [ ' j a v a x . s e r v l e t . j s p . j s t l . f m t . l o c a l e . s e s s i o n ' ]e q ' c s ' } " > e s k y < / c : w h e n > < c : o t h e r w i s e > < c : u r lv a r = " u r l "v a l u e = " c h o o s e L a n g u a g e " > < c : p a r a mn a m e = " l a n g u a g e "v a l u e = " c s " / > < / c : u r l > < d i vc l a s s = " b u b b l e " > < ah r e f = " $ { u r l } " > e s k y < / a > < / d i v > < / c : o t h e r w i s e > < / c : c h o o s e > < / c : o t h e r w i s e > < / c : c h o o s e > < / d i v >
7. Before examining the project in a browser, add another test that displays the value set by the < f m t : s e t L o c a l e > tag. Add the following code beneath the test you created earlier.
< ps t y l e = " t e x t a l i g n :l e f t ; " > < s t r o n g > t e s t s : < / s t r o n g > < b r > < c o d e > \ $ { p a g e C o n t e x t . r e q u e s t . l o c a l e . l a n g u a g e } < / c o d e > : $ { p a g e C o n t e x t . r e q u e s t . l o c a l e . l a n g u a g e } < b r > < c o d e > \ $ { s e s s i o n S c o p e [ ' j a v a x . s e r v l e t . j s p . j s t l . f m t . l o c a l e . s e s s i o n ' ] } < / c o d e > : $ { s e s s i o n S c o p e [ ' j a v a x . s e r v l e t . j s p . j s t l . f m t . l o c a l e . s e s s i o n ' ] } < / p > j a v a x . s e r v l e t . j s p . j s t l . f m t . l o c a l e . s e s s i o nis the string literal key for the L o c a l eset by the < f m t : s e t L o c a l e >tag. You can verify this by clicking in the editor's left margin to set a breakpoint (
new test, then running the debugger ( the breakpoint. ) on the ) on the project. When you click the toggle link to change languages in
the browser, examine the Variables window (Alt-Shift-1; Ctrl-Shift-1 on Mac) when the debugger suspends on
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes
17/25
24/08/13
EL expressions presented in this tutorial primarily use dot (. ) notation. The format depicted in the expression above is known as b racket ([ ] ) notation whereby you enter the string literal key within quotes in order to extract the object's value:
$ { s e s s i o n S c o p e [ ' j a v a x . s e r v l e t . j s p . j s t l . f m t . l o c a l e . s e s s i o n ' ] }
Numerous EL resolver classes exist for the purpose of resolving expressions. For example, when the above expression is encountered at runtime, the I m p l i c i t O b j e c t R e s o l v e rfirst returns a M a pthat maps session-scoped attribute names to their values. (In the above image of the Variables window, you can verify that session attributes are maintained in a C o n c u r r e n t H a s h M a p .) In order to resolve the remainder of the expression, the M a p E L R e s o l v e ris used to get the value of the key named 'j a v a x . s e r v l e t . j s p . j s t l . f m t . l o c a l e . s e s s i o n '. For more information, refer to the Java EE 5 Tutorial: Unified Expression Language: Resolving Expressions. 8. Run the project ( ) and examine the application welcome page in the browser.
In the above image, the server identifies Czech (c s ) as the browser's preferred language from the A c c e p t -
L a n g u a g eHTTP header. This is indicated from the first test. The page displays in Czech, and the language toggle
enables the user to choose English. The second test remains blank as the < f m t : s e t L o c a l e >tag has not yet been called. 9. Click the toggle link for English.
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes
18/25
24/08/13
When clicking the toggle link, the default Czech language is overridden by means of the < f m t : s e t L o c a l e >tag implemented in the h e a d e r . j s p ffile. Although the browser's preferred language remains Czech, you see that the page now displays according to the new language made available by the language toggle. 10. Click the toggle link for Czech.
Changing the language back to the browser's preferred language works as expected, however note that the deciding factor is no longer the language detected from the A c c e p t L a n g u a g eHTTP header, but is the language specified from the < f m t : s e t L o c a l e >tag. 11. Before continuing, remove the tests you added to the h e a d e r . j s p ffile. (Deleted code in strike-through text.)
< b o d y > < % -L a n g u a g et e s t s% > < ps t y l e = " t e x t a l i g n :l e f t ; " > < s t r o n g > t e s t s : < / s t r o n g > < b r > < c o d e > \ $ { p a g e C o n t e x t . r e q u e s t . l o c a l e . l a n g u a g e } < / c o d e > : $ { p a g e C o n t e x t . r e q u e s t . l o c a l e . l a n g u a g e } < b r > < c o d e > \ $ { s e s s i o n S c o p e [ ' j a v a x . s e r v l e t . j s p . j s t l . f m t . l o c a l e . s e s s i o n ' ] } < / c o d e > : $ { s e s s i o n S c o p e [ ' j a v a x . s e r v l e t . j s p . j s t l . f m t . l o c a l e . s e s s i o n ' ] }
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes 19/25
24/08/13
1. Open snapshot 9 in the IDE. Click the Open Project ( computer where you downloaded the project. 2. Click the Run Project (
) button to run the project. When navigating through the site, note that when you click the
language toggle from any of the page views, you are returned to the application's welcome page. If you receive an error when running the project, revisit the setup instructions, which describe how to prepare the database and establish connectivity between the IDE, GlassFish, and MySQL. 3. Use < c : s e t >tags to set a session-scoped v i e wattribute for each of the page views. Open each of the page views in the editor and add the following code to the top of each file.
index.jsp
< % -S e ts e s s i o n s c o p e dv a r i a b l et ot r a c kt h ev i e wu s e ri sc o m i n gf r o m . T h i si su s e db yt h el a n g u a g em e c h a n i s mi nt h eC o n t r o l l e rs ot h a t u s e r sv i e wt h es a m ep a g ew h e ns w i t c h i n gb e t w e e nE n g l i s ha n dC z e c h .% > < c : s e tv a r = ' v i e w 'v a l u e = ' / i n d e x 's c o p e = ' s e s s i o n '/ >
category.jsp
< % -S e ts e s s i o n s c o p e dv a r i a b l et ot r a c kt h ev i e wu s e ri sc o m i n gf r o m . T h i si su s e db yt h el a n g u a g em e c h a n i s mi nt h eC o n t r o l l e rs ot h a t u s e r sv i e wt h es a m ep a g ew h e ns w i t c h i n gb e t w e e nE n g l i s ha n dC z e c h .% > < c : s e tv a r = ' v i e w 'v a l u e = ' / c a t e g o r y 's c o p e = ' s e s s i o n '/ >
cart.jsp
< % -S e ts e s s i o n s c o p e dv a r i a b l et ot r a c kt h ev i e wu s e ri sc o m i n gf r o m . T h i si su s e db yt h el a n g u a g em e c h a n i s mi nt h eC o n t r o l l e rs ot h a t u s e r sv i e wt h es a m ep a g ew h e ns w i t c h i n gb e t w e e nE n g l i s ha n dC z e c h .% > < c : s e tv a r = ' v i e w 'v a l u e = ' / c a r t 's c o p e = ' s e s s i o n '/ >
checkout.jsp
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes 20/25
24/08/13
< % -S e ts e s s i o n s c o p e dv a r i a b l et ot r a c kt h ev i e wu s e ri sc o m i n gf r o m . T h i si su s e db yt h el a n g u a g em e c h a n i s mi nt h eC o n t r o l l e rs ot h a t u s e r sv i e wt h es a m ep a g ew h e ns w i t c h i n gb e t w e e nE n g l i s ha n dC z e c h .% > < c : s e tv a r = ' v i e w 'v a l u e = ' / c h e c k o u t 's c o p e = ' s e s s i o n '/ >
Based on customer-agreed implementation details, we do not need to provide a means of switching languages on the confirmation page view. From a usability perspective, a user will have already selected his or her preferred language prior to checkout. From an implementation perspective, recall that we destroy the user session upon a successfully completed order. (Refer back to the final paragraph in Managing Sessions, which describes how to apply the
i n v a l i d a t emethod to explicitly terminate a user session.) If the Affable Bean staff were to insist on allowing
customers to view their orders bilingually, you would need to consider the following scenarios, dependent on whether you destroy the user session upon displaying the confirmation page: 1. Session destroyed: Would be necessary to take extra measures to ensure that a c h o o s e L a n g u a g erequest from the confirmation page refers to the appropriate order, and can display customer-sensitive details in a secure fashion. 2. Session maintained: Would risk enabling users to mistakenly place double orders on their shopping cart. Also, by not terminating user sessions when they are no longer needed, an unnecessary load may be placed on the server. 4. Open the C o n t r o l l e r S e r v l e tin the editor. (If already opened, press Ctrl-Tab and choose the file.) In the opened file, locate the portion of the d o G e tmethod that handles the c h o o s e L a n g u a g erequest (line 126). Note that currently c h o o s e L a n g u a g erequests are forwarded to the i n d e x . j s pwelcome page.
/ /i fu s e rs w i t c h e sl a n g u a g e }e l s ei f( u s e r P a t h . e q u a l s ( " / c h o o s e L a n g u a g e " ) ){ / /g e tl a n g u a g ec h o i c e S t r i n gl a n g u a g e=r e q u e s t . g e t P a r a m e t e r ( " l a n g u a g e " ) ; / /p l a c ei nr e q u e s ts c o p e r e q u e s t . s e t A t t r i b u t e ( " l a n g u a g e " ,l a n g u a g e ) ; S t r i n gu s e r V i e w=( S t r i n g )s e s s i o n . g e t A t t r i b u t e ( " v i e w " ) ;
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes
21/25
24/08/13
/ /i n d e x . j s pe x i s t so u t s i d e' v i e w 'f o l d e r / /s om u s tb ef o r w a r d e ds e p a r a t e l y
/ /i fp r e v i o u sv i e wi si n d e xo rc a n n o tb ed e t e r m i n e d ,s e n du s e rt ow e l c o m e p a g e t r y{ r e q u e s t . g e t R e q u e s t D i s p a t c h e r ( " / i n d e x . j s p " ) . f o r w a r d ( r e q u e s t ,r e s p o n s e ) ; }c a t c h( E x c e p t i o ne x ){ e x . p r i n t S t a c k T r a c e ( ) ; } r e t u r n ; } }
In the above implementation, you extract the value of the v i e wattribute and, provided that the view: can be identified (i.e., the value is not null), does not originate from the welcome page (i n d e x . j s pdoes not reside in the same location as other page views, and therefore cannot be resolved using the d o G e tmethod's way of forwarding requests) ...you set it to the d o G e tmethod's u s e r P a t hvariable, and forward the request using the method's existing
) to test it in the browser. When you navigate to the category, cart or checkout pages, switch
languages using the language toggle. When you do so, you now remain within the same page view. 7. In the browser, complete an order so that the application forwards you to the confirmation page. When you click the language toggle from the confirmation page, note that you are sent back to the website's welcome page. Implementation-wise, you may consider this to be sufficient. However, the Affable Bean staff have explicitly asked you to remove the language toggle from this page view. One way to accomplish this is to perform a test to determine whether the request servlet path contains '/ c o n f i r m a t i o n '. Switch to the h e a d e r . j s p ffile in the editor and surround the language toggle with the following test. You can use JSTL's functions (i.e., f n ) library to perform string operations.
< d i vc l a s s = " h e a d e r W i d g e t " > < % -I fs e r v l e tp a t hc o n t a i n s' / c o n f i r m a t i o n ' ,d on o td i s p l a yl a n g u a g et o g g l e% > < c : i ft e s t = " $ { ! f n : c o n t a i n s ( p a g e C o n t e x t . r e q u e s t . s e r v l e t P a t h , ' / c o n f i r m a t i o n ' ) } " > < % -l a n g u a g es e l e c t i o nw i d g e t% > < c : c h o o s e > . . . < / c : c h o o s e >
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes 22/25
24/08/13
/ W E B I N F / v i e w / c o n f i r m a t i o n . j s p
Using the p a g e C o n t e x t . r e q u e s t . s e r v l e t P a t hEL expression is comparable to calling
r e q u e s t . g e t S e r v l e t P a t h ( )from a servlet.
The f n : c o n t a i n s ( )function allows you to test if an input string contains the specified substring. The f ntag library has already been declared for you at the top of in the h e a d e r . j s p ffile in snapshot 9:
9. In the browser, step through to the confirmation page but switch languages once along the way using the language toggle. Note that when you complete an order, the confirmation page inadvertently switches back to the originally displayed language. You may rightly identify the cause: upon a successfully completed order, the
C o n t r o l l e r S e r v l e tdestroys the user session and consequently the session-scoped locale that was set using
the < f m t : s e t L o c a l e >tag is also lost.
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes
23/25
24/08/13
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes
24/25
24/08/13
See Also
NetBeans Resources
Introduction to Java EE Technology Getting Started with Java EE 6 Applications Keyboard Shortcuts & Code Templates Card Java EE & Java Web Learning Trail
External Resources
The Java Tutorials: Internationalization Java EE 5 Tutorial: Internationalizing and Localizing Web Applications Developing Multilingual Web Applications Using JavaServer Pages Technology Internationalization: Understanding Locale in the Java Platform Java Internationalization: Localization with ResourceBundles A JSTL primer, Part 3: Presentation is everything Java Internationalization [Technology Homepage] Internationalization and localization [Wikipedia] ISO 639-2 Language Code List [Library of Congress] W3C Internationalization Activity: Articles, best practices & tutorials: Language jQuery
https://netbeans.org/kb/docs/javaee/ecommerce/language.html?print=yes
25/25